0

i want to count row and print php echo. But when i echo the query it show: Resource id #5. But it must show only number 5.

<?php 
$db=require "script_database/connect.php"; 
$query="select count(name) from selection where salary>2000 and job='teacher' ";
        $key=mysql_query($query);
        echo $key;
         if(key>0)
        {

        echo "no row found";

        }
 ?>
7
  • 1
    You want to echo your query or the records ? Also never use mysql_* as it is depreciated Commented May 26, 2015 at 5:17
  • 1
    you don't add a require to a variable.. Commented May 26, 2015 at 5:17
  • Do you want to display the count of rows affected or the count of names? Commented May 26, 2015 at 5:25
  • @sulthanallaudeen it's deprecated. I can't say what effect this has had on its intrinsic value Commented May 26, 2015 at 7:33
  • @Strawberry I believe we can't utilize on php 7, Isn't it ? Commented May 26, 2015 at 8:08

2 Answers 2

4

mysql_query will return the resource of the executed query, not the data. You have to fetch it -

    $query="select count(name) from selection where salary>2000 and job='teacher' ";
    $result=mysql_query($query);
    $key = mysql_fetch_array($result);
    echo $key['count(name)'];

mysql is deprecated now. Use mysqli or PDO instead.

Return

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.

Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.

Sign up to request clarification or add additional context in comments.

4 Comments

Would 'count(name)' be returned? Never tried, I would assign the count to a variable: select count(name) as count from selection where salary>2000 and job='teacher' .. Then echo $key['count'] -- Still doesn't get around he assigned $db to an include, and thinks that if the query returns > 0 then nothing was found..
If you dont use any alias then count(name) will be returned in this case.
Learn something new every day. I always have added an alias. Up-voted your answer for your extra efforts by adding the doc info. I still think he needs to fix his $db=require "script_database/connect.php"
but i cannot say your answer use full why??
-3

You may have to use mysql_num_rows

$query = "Select * from selection where salary>2000 and job='teacher'"

echo mysql_num_rows($query);

2 Comments

Selecting all fields for a simple count?
Down voted for 2 reasons. 1) Your answer was already answered by another member. 2) You shouldn't use SELECT *, especially since the OP specifically declares his columns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.