mysql_num_rows in the error log - php

I am encountering an error. My log is as follows:
PHP Warning: mysql_num_rows(): supplied argument is not
a valid MySQL result resource in
/home/domain/public_html/index.php on line 96
My code is as followsL
line 96:
mysql_num_rows(mysql_query("SELECT * FROM `table` WHERE
UNIX_TIMESTAMP(`date`)>$user_last_visit"));
What could cause this error?

You put two functions into each other directly:
mysql_num_rows(mysql_query(...));
Technically this is possible to do (function 1 returns something that becomes the parameter of function 2), however, in case function 1 returns something that function 2 can not deal with, you get errors.
That happens in your case. Instead store the return value into a variable and do error handling your own:
$result = mysql_query(...);
if (!$result) {
throw new Exception(sprintf('Database query failed: %s', mysql_error()));
}
$num_rows = mysql_num_rows($result);
Proper error handling is crucial for solid programming, so take care about return values. When in doubt double-check for the function in question with the PHP manual which is really a great resource. See mysql_query.

I would try rearranging the code
$sql = "SELECT * FROM `table` WHERE UNIX_TIMESTAMP(`date`) > $user_last_visit";
$result = mysql_query($sql) or die(mysql_error());
echo mysql_num_rows($result);
I can't tell from your code but you are creating a connection to the mysql server?

If you only want to count the rows, use the COUNT(*) keyword in the query; otherwise, the data base will prepare the whole resulting rows for output although you don't need them.
SELECT COUNT(*)
FROM `table`
WHERE UNIX_TIMESTAMP(`date`) > $user_last_visit
Then, simple execute the query:
$result = mysql_query($query);
list($count) = mysql_fetch_array($result);
Anyway, your query throws an error; there should be another warning showing the error. You can use mysql_error() to find out the error otherwise.

According to http://php.net/manual/en/function.mysql-query.php mysql_query returns "For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error."
So try doing
$query = "SELECT * FROM `table` WHERE UNIX_TIMESTAMP(`date`)>$user_last_visit"
$resource = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);
mysql_num_rows($resource);
And then see what happens.

Related

php trouble retrieveing a mysql blob

I am trying to retrieve a blob from a mysql database. However, my query always returns 1, and does not trigger an error. I've been trying the things I found here and there, but I can never get it to return a resource. I manually checked that the blob to be retrieved, so that does not seem to be the problem. Could someone please help me?
$id = 2;
$sqlFetch = "SELECT * FROM mallampati_images WHERE img_id = $id";
$sth = $wpdb->query($sqlFetch) or die ('query failed');
$a = mysql_num_rows($sth);
causes the error:
Warning: mysql_num_rows() expects parameter 1 to be resource, integer given in /home/clients/4ceaa5faab208e8af4350138684e6d6d/web/wp-content/themes/twentytwelve/functions.php on line 539
NULL
this will always return 1.
$sth = $wpdb->query($sqlFetch) or die ('query failed');
what you actually want is
if (false === ($sth = $wpdb->query($sqlFetch)) {
die ('query failed');
}
... rest of code
Here's what I did. It gets a single cell out of a single row, but there are other wordpress functions available, such as $wpdb->get_rowinstead of $wpdb->get_var. It is in fact really simple, but I did not understand the $wpdbAPI at all.
$sth = $wpdb->get_var( $wpdb->prepare( "SELECT img_blob FROM mallampati_images WHERE img_id = 1 ", OBJECT ) );
echo '<img src="data:image/jpeg;base64,'.base64_encode( $sth ).'"/>';

How do you see inside a mysqli_query object to trouble shoot?

I've come into this problem a few times and it's a pain. Right now the line if(mysqli_num_rows($result) != 1) gives the error mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given and $result is the result of mysqli_query(). I can't echo $result and I tried the following code to find out what's inside $result with no success
if (!mysqli_query($link, "SET #a:='this will not work'"))
printf("Error: %s\n", mysqli_error($link));
while ($row = mysqli_fetch_row($result))
printf("%s\n", $row[0]);
I'm trying to trouble shoot but it's like I come to a dead end because I can't see inside the result of mysqli_query().
How can I find the source of the problem? I'm guessing it's bad SQL syntax but I need more details.
Here is the PHP code where $result is defined
$result = mysqli_query($link, 'SELECT `password`
FROM `ajax_login`
WHERE userid = \''.$userName.'\' LIMIT 1');
Are you checking the error code/message? Something like:
if(mysqli_errno($link)){
die(mysqli_error($link));
}
(of course, you wouldn't use die in production)

Determine if there was a mySQL error with PHP

All,
If I run a query like the following:
$qry = "Select wrong_column from table_name";
$result = mysql_query($qry);
If wrong_column doesn't exist then I'll get a mySQL error. In PHP, how can I determine if there was an error from mySQL? If there was an error I'd like it to stop further processing but if there wasn't an error I'd like to get the mySQL results like this:
$resultset = mysql_fetch_array($result);
Would doing something like this work?
if(!mysql_error()){
$resultset = mysql_fetch_array($result);
}
Any advice on how to do this would be appreciated. Thanks in advance!
how can I determine if there was an error from mySQL?
To see if an error occured you should test the result of mysql_query:
$result = mysql_query($qry);
if (!$result) {
$error = mysql_error();
// Handle the error.
}
From the documentation:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
Emphasis mine.
mysql_query("SELECT foo FROM bar") or exit(mysql_error()); is your best friend =)

SQL Simple JOIN issue

I have never used JOINs or have worked with multiple table before.
This error is popping up.
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in [pathway to this mysql_query line].
What am I doing wrong here?
Thank you.
$group_id= 4;
$result = mysql_query("SELECT table1.user_facebook_id AS user_facebook_id
FROM table1 JOIN table2
ON table1.user_id = table2.user_id
WHERE table2.group_id = $group_id");
$row = mysql_fetch_assoc($result);
You should test the value of $result to see if the query failed. If so, print the error for debugging purposes:
$sql = "SELECT ...";
$result = mysql_query($sql);
if (!$result) {
trigger_error(mysql_error());
}
You may also want to try running the SQL query in the MySQL workbench to see if it works there.

mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
I' really stuck on this , I'm gettiing this error:
mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in "filename"
Here is the code:
$sql = "SELECT * FROM $tbl_name WHERE....
$result=mysql_query($sql);
$row = mysql_fetch_assoc($result);
The wierd thing is that I've used the exact same code before and it worked fine
Any ideas??
That means the query failed. Usually it's a SQL syntax error. To find out, just insert this right before the _fetch_assoc line:
print mysql_error();
To prevent the error message, structure your code like this to check the $result beforehand:
$sql = "SELECT * FROM $tbl_name WHERE....";
if ($result = mysql_query($sql)) {
$row = mysql_fetch_assoc($result);
}
else print mysql_error();
Always run all your queries this way
$sql = "SELECT * FROM $tbl_name WHERE....";
$res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
$row = mysql_fetch_assoc($result);
And you will be notified of the cause of error.
But never print or let die() output any errors, as it's security flaw.
This error usually occurs because no data is returned from the query. Make sure the data is being returned by going into something like PHPMyAdmin and making sure the query returns some rows. You should also add the
or die(mysql_error());
At the end of your query.

Categories