-5

I want to count the result which coming from query and display it for users the $queryrun variable holds the query run and mentioned below of my code in my page but I receive this massage:

Your Query Found Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\ss.com\spd\index.php on line 145 Properties in Your Selected Location please here is the code:

<?php 
mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('test') or die(mysql_error());

$count=mysql_query('SELECT COUNT($queryrun) FROM properties'); 
?>

<div>Your Query Found <strong><?php echo $Result=mysql_result($count, 0)?></strong> 
     Properties in Your Selected Location</div><br>
Antony
  • 14,670
  • 10
  • 42
  • 73

5 Answers5

1

Try your query in this way.

$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);

$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);

echo "$num_rows Rows\n";

php manual for mysql

Dieepak
  • 556
  • 2
  • 7
0

This query is failing

$count=mysql_query('SELECT COUNT($queryrun) FROM properties'); 

In order for your variable to be interpolated inside quotes, it has to be in double quotes and not single ones.

$count=mysql_query("SELECT COUNT($queryrun) FROM properties"); 

Note: This assumes that PHP variable $queryrun has been defined before this line. If you meant that queryrun is a field of your table then you could do

$count=mysql_query("SELECT COUNT(queryrun) FROM properties"); 

That is why mysql_result is not getting what it expects

Hanky Panky
  • 45,969
  • 8
  • 69
  • 95
0

first of all write query in " " instead of '', $variable does not work in single quotes

René Höhle
  • 25,733
  • 22
  • 69
  • 78
Prateek Shukla
  • 583
  • 2
  • 7
  • 26
0

If $queryrun is php string variable which is supposed to be subtituted, then your syntax is wrong, as it doesn't get expanded. Try

"SELECT COUNT($fieldname) FROM properties"

or

'SELECT COUNT(' . $fieldname . ') FROM properties'

instead of

'SELECT COUNT($fieldname) FROM properties'

Compare your query to:

"select count(*) from ( select 3 ) as q1;" 
"select count(Db) from mysql.db;"
"select count(blah) as bzz from ( select 3 as blah ) as q1;"

each of these "work", while your doesn't. Analyze difference, you will see why :)

Piotr Wadas
  • 1,802
  • 1
  • 10
  • 12
  • NOT WORKING : Notice: Undefined variable: queryrun in C:\xampp\htdocs\ss.com\spd\index.php on line 44 Your Query Found Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\ss.com\spd\index.php on line 45 Properties in Your Selected Location – Tariq Hashemee Mar 28 '13 at 08:20
  • The other codes are defined below of current code – Tariq Hashemee Mar 28 '13 at 08:24
  • Note, that $queryrun needs to be COLUMN_NAME or TABLE_NAME ( name of relation, to be exact ). If you want to return number of rows of particular SQL expression, then use SELECT COUNT( SELECT something FROM somepropertiestable ) , otherwise SELECT COUNT( * ) FROM properties_table . As all rows in sql tables have obviously same number of fields, that's why 'SELECT COUNT(somecolumn) FROM sometable' usually is the same as 'SELECT COUNT(*) FROM sometable' – Piotr Wadas Mar 28 '13 at 08:25
  • SELECT somefield,someotherfield,COUNT(*) as howmany,someotherotherfield FROM sometable - this will result same numeric value of howmany column in all returned rows. – Piotr Wadas Mar 28 '13 at 08:31
  • SELECT COUNT( SELECT something FROM somepropertiestable) also not working. this is queryrun :$queryrun=mysql_query($query); and i want that the searched numbers that query found display that there is ie. 34 result for your search. – Tariq Hashemee Mar 28 '13 at 09:07
  • compare your query to: "select count(*) from ( select 3 ) as q1;" or to this: "select count(Db) from mysql.db;" or to this: "select count(blah) as bzz from ( select 3 as blah ) as q1;" - all of these "work" :) – Piotr Wadas Mar 28 '13 at 09:22
  • if i want to use this variable $count in the up part of page how can i call it. becuase its giving error massage that its undefined variable i dont know how to use global function its also not working – Tariq Hashemee Mar 28 '13 at 09:37
  • first of all, you need to make sure what string exactly is sent to SQL server as a query, use echo to see what's actually being sent, before calling mysql_* functions. Then, try what is REALLY sent directly in mysql> command line utility, you will also see syntax error messages and hints like "derived query must be named" or "unknown field in bzz", or similar. read my answer including last edit with examples.. determine 100% sure whether it's wrong query syntax or wrong php substitution.. good luck :) – Piotr Wadas Mar 28 '13 at 09:39
  • ya its working but how can i use this : $queryrun=mysql_query($query); $count=mysql_num_rows($queryrun); echo $count; in the top of the page inside another like this – Tariq Hashemee Mar 28 '13 at 09:51
0

The error message means your query did not succeed. mysql_query will return FALSE on failure. Check the result code for mysql_query for robustness and fix your query.

Bart Friederichs
  • 32,037
  • 14
  • 96
  • 185