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
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?

 

 

Link to comment
Share on other sites

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];

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.