I get the mysql result using
$back = mysql_result($query, 0);
but when nothing is found, I get this error:
Warning: mysql_result() [<a href='function.mysql-result'>function.mysql-result</a>]: Unable to jump to row 0 on MySQL result index 2 in
I still get the error even if I use something like if (!empty($back))
How can I avoid the error? Any alternative methods are welcome
$sql = 'some query...';
$result = mysql_query($q);
if($result === false){
throw new Exception(mysql_error($connection));
}
if (mysql_num_rows($result) > 0) {
//Process Result
$back = mysql_result($query, 0);
} else {
// empty Result Set
}
Note the check on (! $result) -- if your $result is a boolean, it's certainly false, and it means there was a database error, meaning error is your query
Your error is caused by the fact that the query returned no rows - e.g. nothing matched.
I would do the mysql_query() first and check if anything was returned (if $result) before calling mysql_result.
BTW you should be using mysqli_query, etc. these days the earlier version is insecure...
Related
I am having a strange issue with mysqli_num_rows. Searching for this issue, I've only found people having issues where NULL is returned no matter what. I also checked the official documentation for the function, and it says it returns an integer of the number of rows returned by the query. Whenever my query returns 1 row (it never should return more), it behaves as I expect. When the query returns 0 rows, I expect the function to return 0, but it returns NULL. Why doesn't it return 0?
I know that my database connection is good and my query works correctly, because when I look for a record that's in the database, I get an integer back. I just can't figure out why this is returning NULL rather than 0.
function getArtistID($name) {
global $conn;
$query = "SELECT artist_id FROM artist WHERE artist_name LIKE '${name}'";
$result = mysqli_query($conn, $query);
if ($result->num_rows) {
$row = mysqli_fetch_assoc($result);
return $row['artist_id'];
} else {
return 0;
}
}
Here's some code that I used to reproduce a case where num_rows seems to be NULL:
<?php
error_reporting(E_ALL);
$conn = new mysqli('127.0.0.1', 'root', null, 'test');
$sql = "SELECT * FROM dual";
$result = $conn->query($sql);
if ($result === false) {
print "Error: {$conn->error}\n";
}
$n = $result->num_rows;
echo "Dump the num_rows property: ";
var_dump($n);
Output:
Error: No tables used
Notice: Trying to get property of non-object in /Users/bkarwin/Documents/SO/my.php on line 14
Dump the num_rows property: NULL
The notice is because it's invalid to access an object-oriented property of a variable that is not an object. This is a frequent source of confusion for PHP developers, and it's a byproduct of the fact that PHP is a loosely typed language, and functions like query() can return either a result object, or a boolean scalar.
The query() function actually returned a false as $result because of some error. In my code, I checked for this error, and you didn't.
When you run mysqli::query() or mysqli::prepare() or mysqli_stmt::execute(), you must check for error conditions every time.
Something about your query caused an error. It's up to you to check for the error and report it.
Update: I edited some text above to make the explanation better, but it might make some comments below seem out of place.
I just can't figure out why this is returning NULL rather than 0.
We can only guess without seeing the log output; but, it is likely the return value is null because it raised an error instead.
You need to ensure that errors are handled when calling a function, before attempting to use the return value.
I'm trying to check if a specific link is already contained in a database,
but I keep getting an error stating "mysql_num_rows() expects parameter 1 to be resource"
I've tried changing a lot of things, but nothing seems to work. Can anybody help?
$result = mysqli_query($con, "SELECT * FROM `songs` WHERE `link` = '$link'");
if($result == False){
"echo f3";
return False;
}
$count =mysql_num_rows($result);
if($count > 0){
echo "f4", $count;
return False;
}
mysqli_* is not the same as mysql_*. You can't use resource from one in another.
Use mysqli_num_rows() to get number of rows from mysqli resource.
I am trying to stop the mysql_query error from being output onto my form. Currently if there is no location found, I receive the error
"Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 11"
I am trying to catch this error, and instead assign the $location variable to not found. My code for attempting this is below, what am I doing wrong?
Thanks!
$query3 = "SELECT `location` FROM location WHERE vin = '$vin' LIMIT 1";
$result3 = mysql_query($query3);
if (!mysql_result($result3,0)) {
$location = "Not found";
} else $location = mysql_result($result3,0,0);
mysql_result() generally shouldn't be used. You'd be better off with something like:
$result3 = mysql_query($query3) or die(mysql_error());
if (mysql_numrows($result3) == 0) then
$location = "not found";
} else {
$row = mysql_fetch_array($result3);
$location = $row[0];
}
Your error is caused by the fact that the query returned no rows - e.g. nothing matched. You then tried to retrieve the first field in the first row of that result set, a row which doesn't exist. Checking the number of returned rows with mysql_numrows() is safer, as that works whether the query found nothing or a bajillion rows.
You should look into how to set your error and warning levels in php ini - usually you want a s little output on prod as possible.
http://www.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting
However, here, the code that would generate that error is:
$result3 = mysql_query($query3);
That is the line you should be writing your if or "or die" statements around:
$result3 = mysql_query($query3)or die($location = "not found");
You should look into using OOP; using a database class to handle interaction with your DB.
But, basically you want to check if there are any rows, before trying to bring back the results.
Try checking with "mysql_num_rows" in your "if" statement:
if (!mysql_num_rows($result3)) {
First, you can add #:
if (!#mysql_result($result3,0)) {
$location = "Not found";
} else $location = mysql_result($result3,0,0);
Second, you can check mysql_num_rows(result3) before mysql_result call.
Mysql_query returns false if nothing is found so a simple :
$result3 = mysql_query($query3);
if (mysql_affected_rows($result3) == 0) {
$location = "Not found";
} else $location = mysql_result($result3,0,0);
Should do it.
This might seem ridiculously simple, but I've been getting all kinds of error depending on how I handle a query that returns nothing.
$query = "SELECT * FROM messages WHERE id > ".$messageId;
$result =mysql_query($query);
$time = time();
while(time()-$time<60 && $result==false)
{
$result = mysql_query($query);
}
if(result != false)
//Encode response
else
//return nothing
How do I check whether my mysql_query() returned anything?
You can check the number of returned rows using mysql_num_rows().
Presuming your loop is to query something until it gets a result, it would be
while(time()-$time<60 && $num_rows == 0)
{
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
(not sure whether what you're doing here is a really good idea, as it is likely to put a terrible burden on the database server, but that's a different issue)
mysql_query() will return false only on "real" errors, e.g. a misspelled query or lost connection.
I am creating a new login script/members directory.
I am creating it from scratch without any frameworks (advice on this matter would also be appreciated).
The situation:
// Look up the username and password in the database
$query = "SELECT admin_id, username FROM admin WHERE adminname = '$admin_user' AND password = SHA1('$admin_pass')";
$data = mysqli_query($dbc, $query);
if (mysqli_num_rows($data) == 1) {
This bit of code keeps giving me an error (the last line in particular):
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /home8/craighoo/public_html/employees/security/dir_admin.php on line 20
When echoing the query I get:
SELECT admin_id, adminname FROM admin WHERE adminname = 'admin' AND password = SHA1('password')
EDIT:
Thanks to everyone. The problem was in my Database column names and the column names I was referencing.
Your query execution is failing. When that happens mysqli_query returns false (boolean value) and when is passed to mysqli_num_rows, you get this error.
Print the query just before executing and check for correctness.
Considering that mysqli_query returns false on failure, and that $data is a boolean, here, I suppose there is an error occuring during the execution of your SQL query.
You could try using mysqli_error to find out what this error is :
$data = mysqli_query($dbc, $query);
if ($data !== false) {
// Do whatever you want with $data
if (mysqli_num_rows($data) == 1) {
//
}
} else {
echo mysqli_error($dbc);
die;
}
Note : echoing the error message and dying, like I did here, is OK while developping your script ; but you should not do that in production.
Instead, in production, you should :
Log the error to a file
Display a nice message to the user
When you have a critical query, it's best to add a die to it like so:
mysqli_query($dbc, $query) or die('Critical error on line #'. __LINE__ .' when attempting to login ...<br>'. mysql_error());
Have you tried running that same query manually thru phpmyadmin or the console? What result do you get?