Counting Rows In Mysql And Printing The Values in PHP [closed]
I want to Count the duplicate rows and print the result in php. This is my query:
$q1 = mysql_query("SELECT COUNT(*) as cnt
FROM sur_ques
WHERE sur_id = '$chek'");
echo $q1;
But instead of the actual result I am getting the following out put:
Resource Id #4
What's wrong in this query?
you have to rewrite
$q1 = mysql_query("select count(*) as cnt from sur_ques where sur_id = '$chek'"); echo $q1;
as
<?php
// Query to select an int column
$query = "select count(*) as cnt from sur_ques where sur_id = '$chek'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo $row[0]; // your row count
?>
This will help you! Cheers!
Let's start off with the query it's self.
"select count(*) as cnt from sur_ques where sur_id = '$chek'";
From a standards point of view, we could do a little bit better. For example, it is considered standard convention to capitlized keywords within an SQL query. So things like SELECT
, AS
, FROM
, WHERE
, should all be capitalized. You also have a function in there COUNT
that is best also capitalized. You have three fields that should also be surrounded by tidals as that makes your query safer over all. This just makes it easier to read over all as you know what is what within your query making the whole thing less ambiguous. Looking at a larger query a year from now it might not make sense to you even tho you wrote it. All consider the readability of your code, and your queries.
"SELECT COUNT(*) AS `cnt` FROM `sur_ques` WHERE `sur_id` = '$chek'";
You should also try to use descriptive names for your database tables, and their fields. Is is apparent that cnt
is the Count, but sur_ques
is more ambiguous. I think the second part is questions, but I don't know what the first part is without some context. Consider that if a third party can't tell what your trying to do with the code, then you might not know what your trying to do with the code years later should you need to change it.
Now we get to talk about the mysql_query function.
resource &mysql_query ( string $query [, resource *$link_identifier* ] )
The first thing that should pop out at you is it's return type is resource. This means that, what it is giving you a special variable. Resources are the special handlers to what could be an opened file, an image canvas areas, or as with out case a database connection. This also means that it can't be directly echoed, or printed. You must use the resources associated functions to use this data.
In our case, that function is known as mysql_fetch_row.
array mysql_fetch_row ( resource $result )
Now we are getting some where! Unlike mysql_query, this function returns an array not a resource. So it's something that we can handle right away within PHP without having to use special functions. As you can see, the function expects the resource datatype as the result of our mysql_query function. This will provide us the data in a useful state.
Putting all of this together, we get the following ...
$result = mysql_query("SELECT COUNT(*) AS `cnt` FROM `sur_ques` WHERE `sur_id` = '$chek'");
$array = mysql_fetch_row($result);
echo $array[0];
Now, I did not explain the last line yet, $array[0]
. Why is it there, what does it do? As we have seen the mysql_fetch_row function returns an array. Your query only will return one row. Arrays start their numbers from zero, so the first and only row will be identified by a 0 index. This will then provide you with the data you are looking for.
I do hope this provides much more in-depth information about why your doing something. Happy programming!
精彩评论