$result=mysql_query("SELECT * FROM playerlocations WHERE player<>'0'");
$returntext="";
while($row=mysql_fetch_array($result))
{
if($returntext!=""){$returntext.="&";}
$returntext.=$row['player']."#".$row['locations'];
}
the error message claims that $result isn't a valid result set. I tested it in phpmyadmin, and it worked. I don't know why it won't work here, please help.
It says it isn't a valid result set. That doesn't necessarily mean it isn't a valid query. (Although != instead of <> would be nice.)
To figure out why it's not valid, output the result of calling mysql_error() after running the query:
echo mysql_error(); //most direct way to do this
It will tell you what MySQL reports as the error message.
One obvious thing to check: have you opened the connection (with mysql_connecst()) before running the query?
Related
I am writing a script that uses data from the user to create a page and create a database entry for the created page. Unfortunately, despite the fact that the query I am using was generated by phpmyadmin, the insert does not succeed.
I have attempted using both mysql and mysqli for all of the function calls(not at the same time obviously) but have received the same result regardless.
While I would love to understand why the insert is failing the more pressing issue is the fact that no error is being produced.
The error conditional is being entered but both mysql_error() and mysqli_error() end up empty.
This is the segment of code where the error occurs:
$result = mysqli_query($pvcon,"'INSERT INTO content
(content.post_type_id,
content.content_title,
content.content_description,
content.content_link,
content.user_id,
content.content_image_path,
content.date_posted)
values (\'".$post_type."\',
\'".$title."\',
\'".$description."\',
\'".$content_link."\',
\'".$user_id."\',
\'".urlencode($article_image)."\',
now())'");
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysqli_error();
exit;
}
I have found multiple questions that are similar to mine but none of those solutions have worked for me which leads me to believe that there is some simple step that I am overlooking.
$result = mysqli_query($pvcon,"'INSERT INTO content
^---
..... snip....
now())'");
^----
your entire query is wrong - you've enclosed it in '-quotes, making the ENTIRE query a monolithic string. And your error call never works, because you've forgotten to include the link identifier in the call:
echo 'MySQL Error: ' . mysqli_error($pvcon);
^^^^^^--missing.
Plus, given the variable names you're using, you're probably ALSO vulnerable to SQL injection attacks, so enjoy having your server pwn3d on top of all this.
I cant really figure out whats wrong with this. I used to write the exact same thing and got it working.
$check = mysql_query("SELECT encrypt FROM database WHERE word='$word'") or die(mysql_error());
Error returned is : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database WHERE word='asdaasdasdd'' at line 1
DATABASE is a mysql reserved word, eclose it with backticks ``
$check = mysql_query("SELECT encrypt FROM `database` WHERE word='$word'")
or die(mysql_error());
Try backquoting database. It's probably a reserved word.
Database or Databases is a keyword. See the following link for Reserve words
The or die() trick is a very poor choice for several reasons:
It's not a very nice way to present the user with an error message.
Using for instance the mysql_error() call with it, as many people do, exposes information that should never get output in a production environment
You cannot catch the error in any way.
You cannot log the error.
You cannot control whether it should be output to the screen or not. It's okay to do that in a development environment, but certainly not in a production environment.
It prevents you from doing any sort of cleanup. It just ends the script abruptly.
An easy way to implement is :
$result = mysql_query('SELECT foo FROM bar', $db) or trigger_error('Query failed: ' . mysql_error($db), E_USER_ERROR);
Encrypt is a function so, even tho' it is not causing the problem, I would avoid using it as a column name.
The exact error message is:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where rfflag='0'' at line 1
Hi,
I'm trying to get some php scripts working and it dies with the above error message. There are two locations where rfflag is used in the SQL query:
$_SESSION['lang']=$objTerm->my_get_one("select min(id) from "
.$objTerm->TABLE['languages']." where status='1' and rfflag='0'");
$rs_lang=$objTerm->execute_query("select id,language from "
.$objTerm->TABLE['languages']." where `status`='1' and `rfflag`='0'");
How do I determine which one is causing the problem? Or is the problem something else altogether?
Echo this:
"select id,language from ".$objTerm->TABLE['languages']." where status='1' and rfflag='0'"
and this:
"select min(id) from ".$objTerm->TABLE['languages']." where status='1' and rfflag='0'"
Then run execute each output in your favorite sql developer tool.
Errors will be displayed there.
How do I determine which one is causing the problem?
Remove one of the queries. See if it still happens.
On a secondary thought, I would suggest that you change your MySQL query code so, that it doesn't use die() to print out the error message. Use trigger_error or exceptions instead, this way you will automatically get a trace of which line caused it.
How do I determine which one is causing the problem?
use trigger_error() to output an error message.
I guess (I have to guess because you supply no code) that you are using die() to output an error.
if you change this bad practice function to trigger_error(), you will be able to see the line number, where error occurred.
If you add non only mysql_error() to it's output, but also query itself, you will be able to see the problem code too.
I guess $objTerm->TABLE['languages'] is undefined or does not have the value you’re expecting.
As sheeks06 has already suggested, just echo the query to see if everything is as expected:
$query = "select min(id) from "
.$objTerm->TABLE['languages']." where status='1' and rfflag='0'";
echo $query;
$_SESSION['lang']=$objTerm->my_get_one($query);
$query = "select id,language from "
.$objTerm->TABLE['languages']." where `status`='1' and `rfflag`='0'";
echo $query;
$rs_lang=$objTerm->execute_query($query);
Sometimes so happens that mysql_query() fails to INSERT data and I am unaware of it. So, the question is how do I know when it happens?
Quoting the documentation page of mysql_query :
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.
So, to detect whether there's been an error or not, you have to test the return value of your call to mysql_query :
$result = mysql_query('...');
if ($result === false) {
// And error has occured while executing
// the SQL query
}
Then, what can you do ?
Well, first, you can log the error to a file, that you can analyse later
For than, you can use mysql_error and mysql_errno.
And you can display a nice error message the user
i.e. some kind of "oops, an error occured page".
Remember, though : the user doesn't need to know (and will not understand) the technical error message -- so don't display it.
You can use mysql_error php.net/mysql_error to get a better explanation that you then either display or log it in a file.
One check that I usually do is like in the example
$result = mysql_query( $query );
if ( !empty( $error = mysql_error() ) )
{
echo 'Mysql error '. $error ."<br />\n";
}
else
{
// the query ran successfully
}
This is a good check for any kind of queries
suggest making a wrapper for mysql_query that detects failure and logs the query plus mysql_error somewhere
If you're using a sensible client library, it will throw an exception when a SQL command fails, and you can examine the error code programmatically to know what to do.
If you have no code handling that specific error path, you should probably run a standard error handler which logs the error, stack trace and some other debug info into a log.
you have to analyze your sql. did you escape your parameters? sounds like this could be the problem. you may paste your sql so we can help you.
for showing the error, you can eg.
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
.
If the cause of the error is deadlock, you might want to try again.
You can check how many rows MySQL has inserted/updated/deleted by doing this query right after your insert/update/delete query.
SELECT ROW_COUNT() as rows_affected
If this returns 0 your insert failed. If it returns 1 or more you've succeeded.
See: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_row-count
Probably you can check LAST_INSERT_ID() (mysql_insert_id() in PHP). Provided that your table has auto_increment column.
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.