Issue receiving a mysql_query - php

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.

Related

PHP mysqli_fetch_array supposedly getting a boolean

$result = mysqli_query($conn,"SELECT * FROM Players");
if ($result !== FALSE) {
while($row = mysqli_fetch_array($result)) {
$result = mysqli_query($conn,"UPDATE Players SET Score='$score' WHERE ID='$id'");
}
}
This works. That is, the databse is indeed updated and everything is all cool.
But it throws a warning:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result,
boolean given
If you search around, the explanation is that the query is failing - hence, it is returning FALSE, so you get the warning.
... But this doesn't make sense in my case. The query is not failing. When I run this script, my database is updated just fine. Besides, there is also a conditional checking if the result is a boolean before using mysqli_fetch_array, so technically this warning should never happen in the first place.
Whatever, the problem must be with $result. Let's do:
echo gettype($result);
Which results in
"object"
Well, this explains why is it passing the condition. However, this still won't explain why mysqli_fetch_array insists this is a boolean (because it isn't).
What is the problem, then?
Tested with PHP Version 5.3.24 and 5.4.19.
You're overwriting the resultset $result from your SELECT query with a new $result value from the UPDATE query inside your loop
while($row = mysqli_fetch_array($result)) {
$result2 = mysqli_query($conn,"UPDATE Players SET Score='$score' WHERE ID='$id'");
}
In while loop used Same variable name '$result' which are already used before. Change variable name inside loop.
while($row = mysqli_fetch_array($result)) {
$result = mysqli_query($conn,"UPDATE Players SET Score='$score' WHERE ID='$id'");
}
The $result array returns nothing when you are updating the table. That's why, you are getting this warning in the while loop when it is trying to fetch data from $result into $row.

Selecting a single field from a MySQL database for comparison of a variable

I need to get 'groupLeader' field from the table 'groups' in the database and store it in a variable and then compare to the current logged in username but I get the error:
Warning: mysql_numrows() expects parameter 1 to be resource, object given in /home/content/00/7923300/html/uber/tasks.php on line 31
The code I currently have is this:
$sq = "SELECT * FROM groups WHERE groupID='".$groupID."'";
$result=$db->query($sq);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$gLeader= mysql_result($result, $i, 'groupLeader');
$i++;
}
if($_COOKIE['$username'] == $gLeader) {
echo "User is leader.";
}
Forgive me if this seems like a rather simple request. I'm new to php and working with databases.
EDIT: Forgot to mention line 31 is the line that contains
$num=mysql_numrows($result);
You might need to use with PHP's mysql_query or your framework row number method.
$result = mysql_query($sq);
Or perhaps
$db->number_of_rows($sq);
The problem here is, the query is not executed properly for an unknown reason. That is why it is returning False to $result, which is really not a resource.
use:
$sq = "SELECT * FROM groups WHERE groupID='".$groupID."'";
$result = mysqli_query($connection, $sq);
or
$result = mysql_query($sq);
Most of that code is redundant and pointless. if all you want is a single field from a single row, then don't do SELECT *, and don't use a loop.
$sql = "SELECT groupLeader FROM ..."
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_assoc($reuslt);
if ($row['groupLeader'] == $_COOKIE['username']) {
...
}
}
However, note that the mysql_*() function complex is officially deprecated in PHP and should not be used anymore. Consider switching to mysqli instead, which is generally drop-in compatible, or better yet, use PDO instead.

Catching mysql_query error

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.

What will the $result be when MySQL returns nothing?

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.

PHP Error - Login Script

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?

Categories