Jump to content

mysql_num_rows() returns 1 always


otuatail

Recommended Posts

Hi have a query that returns one even if you through crap at it. Copy the query into a database directly and it gives the correct result.

 

$Paul = '8d90afbc4c3fa30e81eff74460c2b42e';
$Thread = 'd1bc454d4050cb37f0f9bab21d3c0062';

// crap data
$Paul = 'MickeyMouse';
$Thread = 'DonnaldDuck';

$sql = "select Count(*) FROM Replies WHERE Submitted = '$Paul' AND thread = '$Thread';";
$query = mysql_query ($sql) or die ("E0105");
$total = mysql_num_rows($query); // or die ("E1105");

echo $sql . "<br>";
echo $total;

 

Strange!

 

Desmond.

 

Link to comment
https://forums.phpfreaks.com/topic/245788-mysql_num_rows-returns-1-always/
Share on other sites

The only way around it seems is

 

$sql = "select Count(*) As val FROM Replies WHERE Submitted = '$Paul' AND thread = '$Thread';";

$query = mysql_query ($sql) or die ("E0105");

$rs = mysql_fetch_array($query) or die ("E1104");

echo $rs['val'];

 

is this the best way?

 

 

The result of the count will be available like any other value you query the database for. There isn't any need to even use mysql_num_rows() with a COUNT() query.

 

$query = "SELECT COUNT(pk_id) FROM table WHERE field = 'some_value'";
$result = mysql_query( $query );
$array = mysql_fetch_row( $result );
$matching_records = $array[0];

Ok thanks for this. I need to do this query within another query. Not ideal situation but for speed of sql query is there any difference between

 

$sql = "select Count(*) As val FROM Replies WHERE Submitted = '$Paul' AND thread = '$Thread'"

 

$sql = "select Count(id) As val FROM Replies WHERE Submitted = '$Paul' AND thread = '$Thread'"

 

As * would imply returning all fields in the table?

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.