Linking PHP with mySQL problem [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home2/wizardso/public_html/tab1_content.php on line 6
this is the error that i am getting when i uploaded my db-driven webpage to my web-hosting. The same webpage was running smoothly on my localhost/ server. But its not working over the internet. Please HELP.
UPDATE
These are the contents of my file tab1_content.php
<?php
$query="select * from subcatagory where catagory_id='c001'";
$res=mysql_query($query,$con);
$query_cat="select * from catagory where catagory_id='c001'";
$res_cat=mysql_query($query_cat,$con);
$current_cat=mysql_fetch_assoc($res_cat);
?>

That error most likely results from the fact that there is something wrong with the mysql_query ("result resource") that you are calling mysql_fetch_assoc() on. Try running just the query and see what errors are returned.
All the error you posted tells you is that something went wrong with the query, it could be anything from a MySQL database connection problem to a typo in your script. Please show us your code so we can see what exactly is causing the error.
echo mysql_error();
Edit
Your code appears to look OK- are you sure that there is table within your database called "catagory" (then again- are you sure it's not "category"?), and that within that database is a record with a catagory_id of "c001"?
To test this, try echoing mysql_affected_rows(); , this gives the number of rows "affected" by the last query. If you get 0, then that means that there is no such record.

Whatever result set you're passing to mysql_fetch_assoc() is invalid because the query with mysql_query() did not complete. Check the mysql error with mysql_error()

Check your Database connectivity settings i hope that it may be one of the reason for the empty result set.
Thanks

Related

Program executes even with warning in php [duplicate]

This question already has answers here:
Mysql_fetch_assoc(): supplied argument is not a valid MySQL result … [duplicate]
(3 answers)
Closed 8 years ago.
I have a php code where I just query to insert rows into table via POST method every thing happens fine with WARNING ,I couldn't figure out why is that a warning message
WARNING MESSAGE IS:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/a266/public_html/poster.php on line 9
CODE IS:
include "db.php";
$sql=mysql_query("INSERT INTO tb1 (col1,col2) VALUES ('$_val1', '$_val2')");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
$result="inserted";
NOTE :Value is perfectly inserted into table and returns "inserted" message But why is that warning..?
INSERT just returns true or false.There is no result to fetch with INSERT query.

Errors when typing the correct username and password

I've been looking up these two different errors but I can't find a resolution to mine from the previous questions. I have a log in form and every time you type the correct username and password it throws these two errors...
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home4/jedicody/public_html/LR/core/functions/users.php on line 96
$data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM `users` WHERE `user_id` = $user_id"));
and
Warning: Cannot modify header information - headers already sent by (output started at /home4/jedicody/public_html/LR/core/functions/users.php:96) in /home4/jedicody/public_html/LR/core/init.php on line 17
header('Location: index.php');
These are the lines of code it doesn't like. I can give you any other piece of information you need from my code but was wondering if anyone could give me some insight on what I'm doing wrong. Thank you guys very much in advance for taking a look.
You can echo your sql sentence and execute it in phpmyadmin or mysql console to see if there's something wrong in the result.

PHP printing mysql data via json_encode() [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
I have tow mysql data bases. the first data base location is latin1_swedish_ci and the second data base is utf_unicode_ci.I am trying to read tha data bases with tha php code below.
<?php
mysql_connect("localhost","admin","***");
mysql_select_db("MyDB");
$sql=mysql_query("select * from menu where avail=1");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>
When I am runing this code via my php server the first data base the one with latin1_swedish_ci is readed but when I am trying to read the second one it displays the following messages:
"Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in...."
"Notice: Undefined variable: output in...."
I tried to give
mysql_query("SET NAMES utf8;");
in my code but it didnt worked
Can anyone tell me what is going wrong here?
First of all, you should add $output = array(); before the loop.
Then, fix your query so it doesn't fail. If mysql_query() returns a boolean (false), it means the query failed.
You can add an ugly construct to see why it failed:
$sql=mysql_query("select * from menu where avail=1") or die(mysql_error());
You had an error in your query. The error caused this line:
$sql=mysql_query("select * from menu where avail=1");
to return false. This false when fed to the next line:
while($row=mysql_fetch_assoc($sql))
raised the first error. The while was skipped hence this line was never executed:
$output[]=$row;
Hence the second warning.
Add a mysql_error() after your mysql_* statements like this and make this a habit:
mysql_connect("localhost","admin","***") or die(mysql_error());
.
.
.
mysql_select_db("MyDB") or die(mysql_error());
.
.
.
$sql=mysql_query("select * from menu where avail=1") or die(mysql_error());
These the die(mysql_error()) statement terminates the script after displaying the error message. While it may or may not be necessary to make the script die on mysql errors, most people keep it that way. Instead of using die statement, you can check the return value instead and terminate the script if necessary.
The error in your case seems to be at the database selection line. May be the database name you specify in mysql_select_db was incorrect or may be you do not have permission to read that database (establishing connection with a given user name/password does not mean that that user can read all databases). Post the error message (again).
Try mysql_fetch_array instead of mysql_fetch_assoc
Also define $output before you use it:
$output = array();
while($row=mysql_fetch_array($sql))
$output[]=$row;

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

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?

mysql query occasionally returns nothing

We have a function used within our PHP/MySQL application which returns basic configuration information, it contains a simple select query and looks like this:
public function getConfigurationValue($field)
{
$res = mysql_query("SELECT `cfg_value` FROM `ls_config` WHERE `cfg_name` = '".mysql_real_escape_string($field)."'");
$cfg = htmlspecialchars(mysql_result($res,0));
return $cfg;
}
This problem we are having is that occasionally, seemingly at random, this query throws a mysql error on mysql_result saying that "supplied argument is not a valid mysql result resource". In our debugging we have determined though that this is not because $field is not being passed. Essentially, for a reason we cannot determine a perfectly valid query fails and returns no results causing an empty result set and the subsequent error. If the error was due to the mysql connection failing the script would have died well before this. Also, this function may be called 50-100 times on some page loads but it only tends to fail once on each load.
Please let me know if you need any other information to work this out.
Thanks.
searching for php "supplied argument is not a valid mysql result resource" reveals that to get the actual error, you'd need to call mysql_error, and the error that you get is because the result of the query is FALSE - this value not being a valid mysql result resource.
i.e. in short you have something like:
$res = FALSE; # should contain the mysql result but does not, due to error.
$cfg = htmlspecialchars(mysql_result($res,0)); # the attempt to call mysql_result on invalid argument errors out.
So you'd want to use something like this:
$query = "SELECT * FROM cats WHERE id=$id";
$qr1 = mysql_query ($query)
or die ("Query failed: " . mysql_error() . " Actual query: " . $query);
You might want to give this a shot and see what the underlying error message says.
Given that the error is "MySQL server has gone away", There can be multitude of reasons for it - this article would be a good start to investigate. Searching suggests also some php-related and stack-specific bugs, so it looks like you might need to debug it with a closer attention.
Maybe try to duplicate the setup on another box and then start experimenting with the versions/settings, and see if any of the already reported scenarios match your case. Unfortunately, seems there's no single simple answer to this.

Categories