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.
Related
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.
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 9 years ago.
I have created an android application,by using this function in php i am creating a
a new user in database mysql.
function signUp($sName, $sMobile, $sAddress, $sEmail, $sPwd)
{
$sql = "insert into customers (name,mobile,address,email,pwd) values ('$sName','$sMobile','$sAddress','$sEmail','$sPwd')";
$run = $this->query($sql);
if ($this->result <= 0) {
return false;
} else {
return $this->json('DATA');
}
}
With the below function i am querying in database and returning the response in json format
function query($sql){
$query = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_assoc($query)){
$this->result[] = $row;
}
return $this;
}
but the response i am getting has an error i tried surpessing the warnings by using
#mysql_fetch_assoc($query)
It gave a proper response in browser but android gets null as response
Error:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL
result resource in
Warning: Cannot modify header information - headers already sent by
(output started at
Help required New to PHP. Thanks in advance
INSERT QUERY does not return RESULT SET
Only SELECT query returns result set
You can't get result by Insert Query but you can get id of current inserted user by mysql_insert_id() and then you can use "Select Query" against that id.
Well, the thing is that Your query is INSERT query and, according, to PHP Manual, in successful query it return true, not mysql object. So function "mysql_fetch_assoc($query)" cannot be ran because it requests mysql object as parameter not only TRUE.
Also, I recommend to start using MySQLi or PDO extensions, instead of mysql_* functions.
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning
resultset, mysql_query() returns a resource on success, or FALSE on
error.
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc,
mysql_query() returns TRUE on success or FALSE on error.
The returned result resource should be passed to mysql_fetch_array(),
and other functions for dealing with result tables, to access the
returned data.
Use mysql_num_rows() to find out how many rows were returned for a
SELECT statement or mysql_affected_rows() to find out how many rows
were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.
mysql_query() will also fail and return FALSE if the user does not
have permission to access the table(s) referenced by the query.
when i press register on my php form im getting this error:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean
given in /home/changj/public_html/register.php on line 26 No database
selected
Below is the register.php script line 26 but unsure how to fix the error
if(mysql_num_rows($sql)> 0 ) {
any ideas thanks.
I guess $sql is a string. Try this,
$result = mysql_query($sql);
if(mysql_num_rows($result)> 0 ) { .... }
but mysql_query() will be deprecated as of PHP 5.5.0. An alternative is to use, mysqli_query() or PDO::query()
You don't have a database selected: "No database selected"
Make sure that you have a mysql_connect() and a mysql_select_db() anywhere before your statement.
As answered by me here
From mysql_query() documentation:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
The query is wrong. Change it.
Did you set the database?
mysql_select_db("databaseName");
Or, if you're using "raw queries", you may be missing this line:
mysql_query("USE databaseName");
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?
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 tried everything I could think of, but I keep on getting this error.
Mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /url/ on line 41
if ( $_POST[submit] == "Submit" )
{
$sql="INSERT INTO table (`content`, `userid`, `ttime`) VALUES
('$_POST[content]', '".$user_id."', '".time()."')";
$res = mysql_query($sql,$link) or die(mysql_error());
/* (line 41 is the following)*/
while($result = mysql_fetch_assoc($res)) {
} }
I tried printing out the error (no error prints out just the warning), I tried changing the query, everything I could think of. The code works just fine - it does the insert on click, everything is fine, just that warning is appearing ._.'
Any ideas?
From php documentation on mysql_query()
Return Values
For SELECT, SHOW, DESCRIBE, EXPLAIN
and other statements returning
resultset, mysql_query() returns a
resource on success, or FALSE on
error.
For other type of SQL statements,
INSERT, UPDATE, DELETE, DROP, etc,
mysql_query() returns TRUE on success
or FALSE on error.
The returned result resource should be
passed to mysql_fetch_array(), and
other functions for dealing with
result tables, to access the returned
data.
Use mysql_num_rows() to find out how
many rows were returned for a SELECT
statement or mysql_affected_rows() to
find out how many rows were affected
by a DELETE, INSERT, REPLACE, or
UPDATE statement.
mysql_query() will also fail and
return FALSE if the user does not have
permission to access the table(s)
referenced by the query.
You can't fetch a result from an INSERT query.
You are attempting to obtain a data row from a query that is not a SELECT query. You can only fetch associated arrays from a result data set. An INSERT query just does its thing.