All,
If I run a query like the following:
$qry = "Select wrong_column from table_name";
$result = mysql_query($qry);
If wrong_column doesn't exist then I'll get a mySQL error. In PHP, how can I determine if there was an error from mySQL? If there was an error I'd like it to stop further processing but if there wasn't an error I'd like to get the mySQL results like this:
$resultset = mysql_fetch_array($result);
Would doing something like this work?
if(!mysql_error()){
$resultset = mysql_fetch_array($result);
}
Any advice on how to do this would be appreciated. Thanks in advance!
how can I determine if there was an error from mySQL?
To see if an error occured you should test the result of mysql_query:
$result = mysql_query($qry);
if (!$result) {
$error = mysql_error();
// Handle the error.
}
From the documentation:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
Emphasis mine.
mysql_query("SELECT foo FROM bar") or exit(mysql_error()); is your best friend =)
Related
session_start();
$user=$_SESSION['username'];
$con=mysqli_connect("$host","$username","$password","$db_name");
$sql1="SELECT UserID FROM User WHERE username='$user'";
$query=mysqli_query($con,$sql1);
echo $query;
Im not sure what's the problem with this, I've tried searching around here and tried different ways of writing the syntax and checking for errors in parameter but still no output for the command. What am i missing here?
UPDATE
$sql1="SELECT UserID FROM User WHERE username='$user'";
$query=mysqli_query($sql1,$con);
$row=mysqli_fetch_array($query);
$uid=$row[0];
echo $uid;
As answered by the others, I tried doing this. There is still no output or an output of 0 was given. Where if I tried the query in sql, it shows the right one.
UPDATE 2
It tried the var_dump() command and it returns NULL. Does that mean it can't read the database?
mysqli_query gives no output. A successfully query returns a resource. You have to handle the resource with mysqli_fetch_object, mysqli_fetch_assoc or mysqli_fetch_array.
Also you gave $con parameter before $sql1 which is wrong. You have to put the connection first, then the query variable.
Example:
<?php
session_start();
$user = $_SESSION['username'];
$con = mysqli_connect("$host", "$username", "$password", "$db_name");
$sql1 = "SELECT UserID FROM User WHERE username='$user'";
$query = mysqli_query($con, $sql1);
$fetch = mysqli_fetch_assoc($query);
var_dump($fetch);
?>
$query will be a "Resource". You can use for example $result = mysqli_fetch_array($query); and then echo $result[0];May there's another way, but that's a simple one!
I was able to do some workaround with my problem using mysql syntax. Still not able to determine the problem with mysqli but able to finished my task. Thanks for answering guys.
I have never used JOINs or have worked with multiple table before.
This error is popping up.
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in [pathway to this mysql_query line].
What am I doing wrong here?
Thank you.
$group_id= 4;
$result = mysql_query("SELECT table1.user_facebook_id AS user_facebook_id
FROM table1 JOIN table2
ON table1.user_id = table2.user_id
WHERE table2.group_id = $group_id");
$row = mysql_fetch_assoc($result);
You should test the value of $result to see if the query failed. If so, print the error for debugging purposes:
$sql = "SELECT ...";
$result = mysql_query($sql);
if (!$result) {
trigger_error(mysql_error());
}
You may also want to try running the SQL query in the MySQL workbench to see if it works there.
I am encountering an error. My log is as follows:
PHP Warning: mysql_num_rows(): supplied argument is not
a valid MySQL result resource in
/home/domain/public_html/index.php on line 96
My code is as followsL
line 96:
mysql_num_rows(mysql_query("SELECT * FROM `table` WHERE
UNIX_TIMESTAMP(`date`)>$user_last_visit"));
What could cause this error?
You put two functions into each other directly:
mysql_num_rows(mysql_query(...));
Technically this is possible to do (function 1 returns something that becomes the parameter of function 2), however, in case function 1 returns something that function 2 can not deal with, you get errors.
That happens in your case. Instead store the return value into a variable and do error handling your own:
$result = mysql_query(...);
if (!$result) {
throw new Exception(sprintf('Database query failed: %s', mysql_error()));
}
$num_rows = mysql_num_rows($result);
Proper error handling is crucial for solid programming, so take care about return values. When in doubt double-check for the function in question with the PHP manual which is really a great resource. See mysql_query.
I would try rearranging the code
$sql = "SELECT * FROM `table` WHERE UNIX_TIMESTAMP(`date`) > $user_last_visit";
$result = mysql_query($sql) or die(mysql_error());
echo mysql_num_rows($result);
I can't tell from your code but you are creating a connection to the mysql server?
If you only want to count the rows, use the COUNT(*) keyword in the query; otherwise, the data base will prepare the whole resulting rows for output although you don't need them.
SELECT COUNT(*)
FROM `table`
WHERE UNIX_TIMESTAMP(`date`) > $user_last_visit
Then, simple execute the query:
$result = mysql_query($query);
list($count) = mysql_fetch_array($result);
Anyway, your query throws an error; there should be another warning showing the error. You can use mysql_error() to find out the error otherwise.
According to http://php.net/manual/en/function.mysql-query.php mysql_query returns "For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error."
So try doing
$query = "SELECT * FROM `table` WHERE UNIX_TIMESTAMP(`date`)>$user_last_visit"
$resource = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);
mysql_num_rows($resource);
And then see what happens.
Everything in my new website is working just fine, except that I can't get this piece of code to work:
$query = mysql_query("SELECT * FROM members WHERE useremail = '$useremail'");
$row = mysql_fetch_assoc($query);
$activation = $row['activation'];
if ($activation == '0') {
die('Your account is not yet active. Please check your email!');
exit();
}
In the database, the type is enum('0', '1') and the field name is activation.
Here is the error message I am getting:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result
Could someone please give me a hint?
Thank you.
The error message suggests that your query is invalid:
$query = mysql_query("SELECT * FROM members WHERE useremail = '$useremail'");
Are you doing any input sanitization for $usermail?
Are you sure your database contains that table and that the table contains that column.
Try doing a little debugging:
$query = "SELECT * FROM members WHERE useremail = '$useremail'";
echo $query;
and try running the content of $query directly in your database (from phpMyAdmin or something).
Everything in my new website is working just fine
...until something goes wrong.
You have to learn how to handle errors.
run all your queries at least this way.
$query = "SELECT * FROM members WHERE useremail = '$useremail'"
$result = mysql_query() or trigger_error(mysql_error()." ".$query);
and you always be notified of any error and it's reason
or implement any other way of error notifications, employing mysql_error() function which is the only thing in the world that can tell you where the actual problem is.
Change your first line to:
$query = mysql_query("SELECT * FROM members WHERE useremail = '$useremail'") or die(mysql_error());
And it will spit out the mysql error that is causing the Warning =)
You should not be presuming the call to mysql_query returns a valid result; It's good practice to always test the result first, e.g.
$r=mysql_query("SELECT * YADYADA");
if($r && mysql_num_rows($r)>0){
$row=mysql_fetch_assoc($r);
}else{
//Uh oh, something's not right~~~ throw an exception maybe?
}
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' really stuck on this , I'm gettiing this error:
mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in "filename"
Here is the code:
$sql = "SELECT * FROM $tbl_name WHERE....
$result=mysql_query($sql);
$row = mysql_fetch_assoc($result);
The wierd thing is that I've used the exact same code before and it worked fine
Any ideas??
That means the query failed. Usually it's a SQL syntax error. To find out, just insert this right before the _fetch_assoc line:
print mysql_error();
To prevent the error message, structure your code like this to check the $result beforehand:
$sql = "SELECT * FROM $tbl_name WHERE....";
if ($result = mysql_query($sql)) {
$row = mysql_fetch_assoc($result);
}
else print mysql_error();
Always run all your queries this way
$sql = "SELECT * FROM $tbl_name WHERE....";
$res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
$row = mysql_fetch_assoc($result);
And you will be notified of the cause of error.
But never print or let die() output any errors, as it's security flaw.
This error usually occurs because no data is returned from the query. Make sure the data is being returned by going into something like PHPMyAdmin and making sure the query returns some rows. You should also add the
or die(mysql_error());
At the end of your query.