Consider this SQL query

$sql = "select count(*) as count from table where value='this'";

This query is going to return only one value in the field count.

Is there a way to get that value without using a fetch_array and pointing the first value of the returned array instead?


What exactly are you trying to do?

Written by RPM

Accepted Answer

mysql_result() will allow you to pull the value directly from the result set without having to use a fetch method. Here's a very simple example of how you can use it to get your single value:

mysql_select_db( "mysql", mysql_connect( "localhost", "root", "" ) );
$r = mysql_query( "SELECT count(name) as total FROM `help_keyword`" );
echo $r ? mysql_result( $r, 0 ) : mysql_error() ; // Outputs 450

There are three parameters for the mysql_result() function; first is the result set itself, second is the row index and third is the field index. I'm only using the first two parameters since the third one will default to 0, the first field returned.

Written by Jonathan Sampson
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the community.
It is licensed under cc-wiki