PHP mysqli: sqlstate() function
mysqli_sqlstate() function / mysqli::$sqlstate
The mysqli_sqlstate() function / mysqli::$sqlstate returns the SQLSTATE error code for the last error.
The error code consists of five characters. "00000" indicates no error. The values are specified by ANSI SQL and ODBC.
Syntax:
Object oriented style
string $mysqli->sqlstate;
Procedural style
string mysqli_sqlstate ( mysqli $link )
Parameter:
Name | Description | Required/Optional | |
---|---|---|---|
link | A link identifier returned by mysqli_connect() or mysqli_init() | Required for procedural style only and Optional for Object oriented style |
Usage: Procedural style
mysqli_sqlstate(connection);
Parameter:
Name | Description | Required/Optional |
---|---|---|
connection | Specifies the MySQL connection to use | Required |
Return value:
Returns a string containing the SQLSTATE error code for the last error. The error code consists of five characters. '00000' means no error.
Version: PHP 5, PHP 7
Example of object oriented style:
<?php
$mysqli = new mysqli("localhost", "user1", "datasoft123", "hr");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* Table City already exists, so we should get an error */
if (!$mysqli->query("CREATE TABLE City (ID INT, Name VARCHAR(30))")) {
printf("Error - SQLSTATE %s.\n", $mysqli->sqlstate);
}
$mysqli->close();
?>
Output:
Error - SQLSTATE 42S01.
Example of the Procedural style
<?php
$link = mysqli_connect("localhost", "user1", "datasoft123", "hr");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* Table City already exists, so we should get an error */
if (!mysqli_query($link, "CREATE TABLE City (ID INT, Name VARCHAR(30))")) {
printf("Error - SQLSTATE %s.\n", mysqli_sqlstate($link));
}
mysqli_close($link);
?>
Output:
Error - SQLSTATE 42S01.
Example:
<?php
$con=mysqli_connect("localhost","user1","datasoft123","hr");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Table Persons already exists, so we should get an error
$sql="CREATE TABLE Persons (Firstname VARCHAR(30),Lastname VARCHAR(30),Age INT)"
if (!mysqli_query($con,$sql));
{
echo "SQLSTATE error: ". mysqli_sqlstate($con);
}
// Close connection
mysqli_close($con);
?>
See also
Previous: set_local_infile_handler
Next: ssl_set
PHP: Tips of the Day
PHP: How to generate a random, unique, alphanumeric string for use in a secret link?
Security Notice: This solution should not be used in situations where the quality of your randomness can affect the security of an application. In particular, rand() and uniqid() are not cryptographically secure random number generators. See Scott's answer for a secure alternative.
If you do not need it to be absolutely unique over time:
md5(uniqid(rand(), true))
Otherwise (given you have already determined a unique login for your user):
md5(uniqid($your_user_login, true))
Ref : https://bit.ly/31fd9wa
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook