Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a MySQL query using the legacy API was successful?

Tags:

php

mysql

How do I check if a MySQL query is successful other than using die()

I'm trying to achieve...

mysql_query($query);

if(success){
//move file
}
else if(fail){
//display error
}
like image 396
JohnSmith Avatar asked Aug 12 '12 00:08

JohnSmith


People also ask

How do I add commas in numbers in MySQL?

MySQL FORMAT function examples As you see in the result, the de_DE locale use dot (.) for grouping thousand and comma (,) for decimal mark. Let's take a look at the products table in the sample database.


3 Answers

This is the first example in the manual page for mysql_query:

$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

If you wish to use something other than die, then I'd suggest trigger_error.

like image 157
Mark Byers Avatar answered Oct 19 '22 03:10

Mark Byers


You can use mysql_errno() for this too.

$result = mysql_query($query);

if(mysql_errno()){
    echo "MySQL error ".mysql_errno().": "
         .mysql_error()."\n<br>When executing <br>\n$query\n<br>";
}
like image 11
Taha Paksu Avatar answered Oct 19 '22 03:10

Taha Paksu


If your query failed, you'll receive a FALSE return value. Otherwise you'll receive a resource/TRUE.

$result = mysql_query($query);

if(!$result){
    /* check for error, die, etc */
}

Basically as long as it's not false, you're fine. Afterwards, you can continue your code.

if(!$result)

This part of the code actually runs your query.

like image 4
ಠ_ಠ Avatar answered Oct 19 '22 05:10

ಠ_ಠ