I am testing the die () clause in the below code. When I enter an incorrect password and use the mysqli_connect_error() function, everything works as expected and I get the MySQL error message. Why does it not work when I use mysqli_error()? I was under the impression mysqli_error() should return the last error description, regardless of error type, for the most recent function call? Im using PHP 5.2.17
This works:
$conn = mysqli_connect("localhost","testUser","incorrectPassword","testDB")
or die(mysqli_connect_error());
This does not work:
$conn = mysqli_connect("localhost","testUser","incorrectPassword","testDB")
or die(mysqli_error());
Would like to understand why.
It needs the connection mysqli_error($link).
The mysqli_error() function is identical to the corresponding mysqli_errno() function in every way, except instead of returning an integer error code the mysqli_error() function will return a string representation of the last error to occur for the database connection represented by the link parameter. If no error has occured, this function will return an empty string.
http://php.net/manual/en/mysqli.error.php
One is for the connection the other 2 for query errors.
Related
I know when this statement is executed, it first check i.e. mysqli_connect is successful then another die statement is not executed.
What I want to know here is that if the first check i.e. mysqli_connect is successful, does it mean that it is same as boolean true? and if the mysqli_connect is unsuccessful, does it mean that it is same as boolean false??
The statement is returning an object, not any boolean values. Try to store it in a variable and var_dump to see what it's returning. It Returns an object representing the connection to the MySQL server.
In the case of not successful, the or statement will execute and halt the execute due to die()
Based on the manual mysqli::__construct
Return Values: Returns an object which represents the connection to a MySQL Server.
So it will return a databases connection object when connection happen successfully.
If not then or condition will execute with the message you wrote in it which is :- Error establishing connection
For a better practice always try to find what problem occur while connection, so that you can fix it. So write like below:-
mysqli_connect($mysql_host, $mysql_user, $mysql_password) or die(mysqli_connect_error());
Reference:- mysqli_connect_error()
I think mysqli_connect_error() will help you fixing this problem.
It return a string which describes the error.
https://www.php.net/manual/de/mysqli.connect-error.php
Since some time ago I have been programming in php with oracle, and I've always liked the function oci_error() because the function can give me the byte position of the error in the query (offset) and then I can make a function to show the error with a flag indicating where is the error in the query.
Well I'm making a similar function but with mysql:
I have the mysql_error() which gives me the "message".
I have the mysql_errno() which gives me the "code".
The "sqltext" I can get it with the function itself.
But, How can I get the offset value of the error in the query?
MySQL errors are usually of the type
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...
Following the "near" keyword is the part of the query where the error occurred. You could search for this string using strpos() in your query and use that as the offset.
Can anybody explain the difference between mysql_errno and mysql_error?
mysql_errno returns the error code, while mysql_error returns the error text...
Are you saying the functions of php ?
mysql_errno returns the number of the error,
and mysql_error returns the text of the error.
You can easily find the difference in http://www.php.net/manual/en/function.mysql-errno.php
string mysql_error ([ resource $link_identifier ] )
Returns the error text from the last MySQL function. Errors coming back from the MySQL database backend no longer issue warnings. Instead, use mysql_error() to retrieve the error text. Note that this function only returns the error text from the most recently executed MySQL function (not including mysql_error() and mysql_errno()), so if you want to use it, make sure you check the value before calling another MySQL function.
mysql_errno is the number of the error
mysql_error — Returns the text of the error message from previous MySQL operation
mysql_errno — Returns the numerical value of the error message from previous MySQL operation
I would add to the topic that MySQL-specific error numbers returned by mysql_errno() are not the SQL error numbers you could expect, they differ from SQLSTATE values (returned by mysql_sqlstate() or sqlstate property inside a mysqli_sql_exception). You can find a complete list of error messages and error numbers for your MySQL distribution here (in addition to the Docs/mysqld_error.txt file).
Source: MySQL Reference Manual
I'm troubleshooting a prepared statement but I'm getting nowhere. I think this is because PDO is not generating an error.
I've called:
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
And am trying to get it to insert as follows:
$stmt = $db->prepare23423("INSERT INTO ".DB_TABLE_PREFIX."quote (email,reference,started) VALUES (:email,:reference,:started)");
As you can see, I've added some random characters to the function name to try and get it to spit out an error (at the very least it should tell me that the function doesn't exist?) but it won't. Why is that?
You wouldn't expect a PDO error from this: you'd expect a PHP error... so what are your PHP error settings?
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.