A weird mysql_fetch_(assoc,array,object) error [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php warning mysql_fetch_assoc
I have a weird problem about my script. It returns always error for mysql_fetch_array or mysql_fetch_assoc. I have used mysql_fetch many times in my project and I checked for this error many times but I am blind about what is happening. Is there something wrong about my script?
My functions aim is learning the biggest value of specified mysql field.
Here is the function:
function nextIncrement($table,$field) {
$sql = mysql_query("SELECT '$field' FROM '$table' ORDER BY '$field' DESC LIMIT 0,1");
while($row = mysql_fetch_assoc($sql)) {
$next = $row[$field];
}
$next = (int)$next;
return $next;
}
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource ...

Most likely, your mysql_query() returned false for some reason.
See the manual for a list of possible values that mysql_query() can return.
Do a echo mysql_error(); to see what's wrong.

Check to see that the query actually succeeds before proceeding to fetch results.

There may be an error in your SQL statement, or maybe you don't have an open database connection?

Related

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

This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 8 years ago.
I don't understand why I am getting this error:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL
result resource
My code is:
$alreadyMember = mysql_fetch_assoc(mysql_query("SELECT id FROM members WHERE emailAddress='{$_SESSION['register']['email']}'"));
I am also getting the error here:
$alreadyRegistered = mysql_fetch_assoc(mysql_query("SELECT confirm_code FROM memberstemp WHERE emailAddress='{$_SESSION['register']['email']}'"));
If mysql_query() fails, it returns FALSE instead of a MySQL result resource.
So basically mysql_query() is failing and you are passing FALSE to mysql_fetch_assoc() instead of the resource you are supposed to.
You have to run mysql_query() separately and check if it returns FALSE before proceeding, in which case you print mysql_error() to learn what went wrong.
It is best to separate query and the fetching of result for easier debugging and prevent unnecessary error, like:
$query = "SELECT id FROM members WHERE emailAddress='{$_SESSION['register']['email']}'";
$result = mysql_query($query) or die("Error : ".mysql_error.__LINE__);
if ($result) // Test if result has no error
{
$alreadyMember = mysql_fetch_assoc($result);
}
However, it is highly recommended to use prepared statements using PDO to prevent sql injection instead of simple mysql() extensions.

Why is this a bad MYSQL query in PHP?

Is this a bad mysql query i used in php?
$tablenamep = $_POST["tablenamep"];
$res = mysqli_query($con, "SELECT * FROM `$tablenamep` WHERE number=9");
So when i try to fetch the result using:
while ($row = mysqli_fetch_assoc($res))
There is an sql injection error :
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given
I have read several questions and answers regarding this error, but my question is why is the query returning a boolean, when i have even added a value to $tablenamep variable. I added the value to the variable from my android app using this code :
nameValuePairs.add(new BasicNameValuePair("tablenamep", msg));
The codes are working and there aren't any errors, but my android app is crashing when i try to get the result of the php. How can i solve this! (NOTE : there is nothing wrong in my android app, i've thoroughly checked it)
Why is this a bad query? What can i do for the Query to not return a boolean, and return the actual value?
I guess your $res = mysqli_query($con, "SELECT * FROM $tablenamep WHERE number=9");
returns fails.
As what stated here in TECHNICAL DETAILS TABLE
For successful SELECT, SHOW, DESCRIBE, or EXPLAIN queries it will
return a mysqli_result object. For other successful queries it will
return TRUE. FALSE on failure
mysqli_fetch_assoc() function needs mysqli_result but the query fails that why it returns boolean instead of object.
You will find that your query has 'failed'
Insert the bit of code below
if($result === FALSE) {
die(mysql_error());
}
just above this line
while ($row = mysqli_fetch_assoc($res))
and you will find it dies at the script there.
Odds are that your posted value has a problem with it - echo out your posted value and see if it contains any ' or " etc. etc.

I am getting error in mysql_fetch_assoc() [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 am connected with my database and there seems to be an error appearing on this line of code:
while($br = mysql_fetch_assoc($brand))
and on my query I put this:
$brand = mysql_query("Select * from genratb");
The error says
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\videoshop\index.php on line 166
The first command is actually my line 166.
It looks like your query failed. The mysql_query call probably returned false, instead of the result resource.
$brand = mysql_query("Select * from genratb");
if (!$brand)
{
//error, query failed
}
else
{
while($br = mysql_fetch_assoc($brand))
{
//use row
}
}
If they query fails, then mysql_query() will return false. In that case, you need to look at mysql_error() to find out why the query failed.
The PHP documentation states:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
Most likely your query is failing for some reason, and setting $brand to FALSE. Are you sure your database connection is working, and your table exists?
You can add after your query line:
if (FALSE===$brand) { die(mysql_error()); }
This should tell you what is going wrong.

No results from MySQL query in PHP, but 1 result via phpMyAdmin [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
mysql_num_rows(): supplied argument is not a valid MySQL result resource
Here's the deal. I wanna make a Login form, but I keep recieving the Error Message:
mysql_num_rows(): supplied argument is not a valid MySQL result resource in Line 14
My code looks like this:
if($_POST){
ob_start();
$fusuario = $_POST['fusuario'];
$fsenha = md5($_POST['fsenha']);
$sql = "SELECT * FROM usuario WHERE login='$fusuario' and senha='$fsenha'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
$row=mysql_fetch_array($result);
if($count==1)
{
session_start();
$_SESSION['admin_user'] = $fusuario;
$_SESSION['admin_id'] = $row['id_usuario'];
header("location:index.php");
}
else { $erro = 1; }
ob_end_flush();
}
?>
When I execute the SELECT query from phpMyAdmin, it returns 1 row, like it should.
When I do it via PHP, no row is returned.
Any ideas?
It appears that your mysql_query() is running into an error. Check what the error message that mysql is returning:
$result=mysql_query($sql) or die(mysql_error());
$count=mysql_num_rows($result);
I'm guessing that there is a problem with your mysql connection... try explicity including the mysql connection identifier:
mysql_query($sql,[INCLUDE LINK IDENTIFIER HERE])
If you're not sure what this means, read this: http://php.net/manual/en/function.mysql-query.php
If mysql_num_rows() says that $result is not a valid resource, it's probably because the query failed, and returned FALSE instead of a resource.
You should always check for errors after running an SQL query.
See code examples that check if (!$result) ... at http://php.net/mysql_query
I see from your comment that you had no open connection to the database at the time you issued your query. That'll be a problem too. :-)

mysql_result(): supplied argument is not a valid MySQL result resource error? [duplicate]

This question already has answers here:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result [duplicate]
(6 answers)
Closed 9 years ago.
Creating a login system for something and am getting:
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in .../func/user.func.php on line 21
Here is my code:
function user_register($email, $name, $password) {
}
function user_exists($email) {
$email = mysql_real_escape_string($email);
$query = mysql_query("SELECT COUNT('user_id') FROM 'users' WHERE 'email' ='$email'");
//this is line 21:
return (mysql_result($query, 0) == 1) ? true : false;
}
Your SQL is full of syntax errors, single quotes are used to quote string literals, backticks (or double quotes in standard SQL) are used for identifiers. Try this:
$query = mysql_query("SELECT COUNT(user_id) FROM users WHERE email = '$email'");
You don't need to quote any of those identifiers so don't bother.
From the fine manual:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
You probably want to add some error checking after you've fixed your SQL syntax errors.
There is an error in your query. If you want to figure out what the error is, output it with mysql_error()
Your query has syntax errors. You're using single quotes on field and table names. That changes from them being field/table names to being ordinary strings. So your query boils down to count(some string) from someotherstring where yetanotherstring.
If your query call had been constructed something like this:
$result = mysql_query(...) or die(mysql_error());
you'd have been informed of the syntax error. As it stands now, your code assumes the query succeeds, which is a very bad thing to do. There's precisely ONE way for a query to succeed, and far too many ways for it to fail.
Your code has no error handling, so if anything goes wrong, it continues on blissfully unaware trying to make soup out of stones. Take a look at the examples in the manual, they all check for errors in the query before they try to analyze the result.

Categories