PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 




mysql_error() Function in PHP


Tutorials »Php »

Topic

What is mysql_error() function in PHP?
How does mysql_error() works?




Explanation

mysql_error() function returns the text error message from previous MySQL operation.

Syntax
string mysql_error ( [resource link_identifier])

Returns the error text from the last MySQL function, or '' (the empty string) if no error occurred.

mysql_error() function is used to retrieve the error text from the most recently executed MySQL function

Example:


<?php

//Attempt to connect to the default database server
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");

//select non existing database
if (!mysql_select_db("my_database", $link)) {

//prints error message on selecting non existing database
echo mysql_errno($link) . ": " . mysql_error($link). "\n";
}

//Attempt to select database
mysql_select_db("my_database", $link);

//Execute query
if (!mysql_query("SELECT * FROM nonexistenttable", $link)) {

//Display error report
echo mysql_errno($link) . ": " . mysql_error($link). "\n";
}

mysql_close($link);

?>


In the above example, the query fails due to non existance of database 'my_database' and table 'nonexistenttable'. Here 'mysql_errno' function prints the occured error number and 'mysql_error' prints the text error message from the executed query.

RESULT:

1049: Unknown database 'nonexistentdb'
1146: Table 'my_database.nonexistenttable' doesn't exist

See also: mysql_errno() and MySQL error messages





Other Links

web hosting