for some reason i can not understand im having troubles with mysql_num_rows.
Heres the script:
$notquery = 'SELECT * FROM notification WHERE uid = 1 AND read = 0
AND tipo = post
OR tipo = subpost OR tipo = logros';
$notQuery = (mysql_query($notquery));
$num_rows = mysql_num_rows($notQuery);
error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
This is probably because you have an error in the query execution.
Try adding or die(mysql_error()) to debug what's going wrong...
update the code to :
$notQuery = mysql_query($notquery) or die(mysql_error());
The sql is full with syntax error, try this
SELECT * FROM notification
WHERE
uid = 1 AND
`read` = 0 AND
tipo in('post', 'subpost', 'logros');
You input a string into mysql_num_rows =)
You use $notquery and $notQuery mixed... That must be the silliest thing I've ever seen.
Try this:
$sql = 'SELECT .....';
$result = mysql_query($sql);
echo mysql_error()."\n";
$numRows = mysql_num_rows($result);
Obviously, this might produce the same error, since you don't check for errors :) $result might not be a MySQL resource (it might be FALSE).
Related
if (isset($_GET["val"])) {
$val = $_GET["val"];
$sql1 = "select count(*) as count from staff_log l where l.time_in is null and l.time_out is not null and l.date_today = curdate() and l.staff_id = ".$val.";";
$result1 = mysql_num_rows($sql1);
}
add this line...
$result = mysql_query( $sql1);
$result1 = mysql_num_rows($result);
you must define query in mysql_query() function;
mysql_num_rows() returns, as shown in the PHP manual:
The number of rows in a result set on success or FALSE on failure.
This means that there was an error executing it, which is apparently because you didn't run any query (mysql_query()).
Note that the php manual also has the following warning at the top of the page:
This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.
first execute the query and then num_rows
The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database. Instead, use the MYSQLi or PDO_MySQL extensions.
try this
if (isset($_GET["val"]))
{
$val = $_GET["val"];
$sql1 = "select count(*) as count from staff_log l where l.time_in is null and l.time_out is not null and l.date_today = curdate() and l.staff_id = ".$val.";";
$result = mysql_query($sql1);
$count = mysql_num_rows($result);
}
You are almost right, please view the below code. You need to use mysql_query(), The mysql_query() is used to execute query of the default database.
if (isset($_GET["val"]))
{
$val = $_GET["val"];
$sql1 = "select count(*) as count from staff_log l where l.time_in is null and l.time_out is not null and l.date_today = curdate() and l.staff_id ='$val'";
$sql2 = mysql_query($sql1); // Execute the query first
$result1 = mysql_num_rows($sql2); // Get the number of rows from executed query result.
echo "The count is $result1";
}
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How do i “echo” a “Resource id #6” from a MySql response in PHP?
Hey guys, I got an error when I try to run my code in PHP.
It displays resource id #53 in my screen. All I want is to count only the total of one of my field but I'm stuck with this error. Here's my code below:
$last_points = mysql_insert_id();
//echo $last_points , display like 12... no error
$fkid = $last_points; // no error....
$sql = "SELECT COUNT(*) FROM downline WHERE fkmember = {$fkid}";
$execute = mysql_query($sql) or die (mysql_error());
echo $execute; //display error why?
Help me guys please. I think it's my query.
First off, resource id #53 is not an error. You are displaying a resource, not the output of the query.
To show the output, use:
$last_points = mysql_insert_id();
//echo $last_points , display like 12... no error
$fkid = $last_points; // no error....
$sql = "SELECT COUNT(*) FROM downline WHERE fkmember = {$fkid}";
$execute = mysql_query($sql) or die (mysql_error());
print_r(mysql_fetch_array($execute)); //display error why?
Secondly, the mysql_* functions are deprecated. You should look into learning and utilising the mysqli or PDO libraries accordingly.
Instead of trying to echo a resultset(as received because of mysql_query) do this:
print_r( mysql_fetch_array($execute) );
$execute is an array so you need to print it among echoing it
print_r($execute);
By codeigniter way
In Model:
function getCount($fkid)
{
$Qry = "SELECT * FROM downline WHERE fkmember = $fkid};
$query = $this->db->query($Qry);
return $query->num_rows();
}
In controller:
echo $Count = $this->modelname->getCount($id);
I am trying to show on my page the total Registered Users. I am using this:
<?php
//connect to db
require_once('connect.php');
$usrcnt = mysql_query("SELECT COUNT(DISTINCT ID) FROM members");
$res = mysql_num_rows($usrcnt);
$cnt_mbrs = mysql_fetch_array($res);
?>
And then I call $cnt_mbrs in my page but I get errors like:
mysql_num_rows(): supplied argument is not a valid MySQL result resource...
Is it correct what I am doing?
$usrcnt = mysql_query("SELECT COUNT(*) as cnt FROM members");
$res = mysql_fetch_array($usrcnt);
$cnt_mbrs = $res ['cnt']
Is more correct.
mysql_num_rows returns the number of rows in the result set. Change to
$cnt_mbrs = mysql_fetch_array($usercnt);
Perhaps for performance considerations it might be better to just keep a running total.
Yes I know that the database will not be normalised
Alright, PHP is throwing this error at me (in the log) when I run the code mentioned below:
Error
mysql_num_rows() expects parameter 1 to be resource, string given in (place) on line 10
Line 9-11
$queryFP = ("SELECT * FROM db");
$countFP = mysql_num_rows($queryFP);
$aID = rand(1, $countFP);
I think it has something to do with the $queryFP's syntax, but I'm not completely sure how to fix it since $queryFP's syntax is the simplest query I've ever seen.
You need to query the database first.
$queryFP = ("SELECT * FROM db");
Should be:
$queryFP = mysql_query("SELECT * FROM db");
You are missing the mysql_query function, it should be like this:
$queryFP = "SELECT * FROM table_name_here";
$queryFP = mysql_query($queryFP) or die(mysql_error());
$countFP = mysql_num_rows($queryFP);
$aID = rand(1, $countFP);
As it been said, you're missing mysql_query function.
Though whole approach is wrong. You shouldn't select whole load of ata if you need only number of rows.
So, it must be
$sql = "SELECT count(*) FROM db";
$res = mysql_query($sql) or trigger_error(mysql_error().$sql);
$row = mysql_fetch_row($res);
$countFP = $row[0];
$aID = rand(1, $countFP);
And I hope you won't use $aID for any database related action
I'm running the following query, expecting it to return an INTEGER, but it returns "Resource id #3"
What am I doing wrong?
$queryPlans = mysql_query("SELECT count(*) FROM infostash.rooms");
echo $queryPlans;
There are actually 15 rows in this table, and I would like to return the number 15.
Any suggestions?
mysql_query will return a php resource(see: http://www.php.net/manual/en/language.types.resource.php).
The returned resource should then be passed to mysql_fetch_assoc or similar.
Since you are only getting the count, you can use the following:
$queryPlans = mysql_query("SELECT count(*) FROM infostash.rooms");
$count = mysql_result($queryPlans,0,0);
echo $count;
You need:
$queryPlans = mysql_query("SELECT count(*) FROM infostash.rooms");
$row = mysql_fetch_array($queryPlans);
echo $row[0];
mysql_query() isn't returning the result. It's returning a resource you can loop across and interrogate for rows (as above).
This is actually expected behavior according to the documentation:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
It's a regular select that returns one row with one column and should be treated as such. You can call mysql_fetch_array on the result:
$row = mysql_fetch_array($resource);
$count = $row[0];
mysql_query() returns a result resource. You need another function the get "valuable information" from that resource. In this case mysql_fetch_array()/mysql_fetch_row()/mysql_fetch_object as cletus pointed out. Or (since it's only a single value) mysql_result().
Any sql query may fail for various reasons. You should always check the return value of mysql_query(). If it's FALSE something went wrong and mysql_error() can tell you more about it.
$mysql = mysql_connect(...) or die(mysql_error());
mysql_selecT_db(.., $mysql) or die(mysql_error($mysql));
$query = "SELECT count(*) FROM infostash.rooms";
$queryPlans = mysql_query($query, $mysql) or die(mysql_error($mysql));
$cRows = mysql_result($queryPlans, 0);
echo $cRows;
If you are planning on using the full query later (e.g. select , rather than count()), you can save yourself a database hit by using mysql_num_rows() on the full query. Example:
$queryPlans = mysql_query("SELECT * FROM infostash.rooms");
$results = mysql_fetch_array($queryPlans);
echo "There were " . mysql_num_rows($queryPlans) . " results";
while($row = mysql_fetch_assoc($queryPlans)){
// Do stuff here
}
$queryPlans = mysql_query("SELECT count(*) FROM infostash.rooms");
mysql_num_rows($queryPlans);
http://us.php.net/manual/en/function.mysql-num-rows.php