How To Echo Mysql Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have php mysqli error Meta Discuss the workings and policies of this site About Us php mysql query error Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with php mysql error handling us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just or die mysql error like you, helping each other. Join them; it only takes a minute: Sign up how to display MySql error in php up vote 14 down vote favorite In PHP, I am trying to execute a long MySql query that depends on the user input. However, my query fails with the following message, "Query Failed". Actually I have
Mysql_query Error
printed this message whenever the query fails but I am having hard time looking for the reason behind this failure. Unfortunately, I couldn't find it because the error is not specified on the web page. My question is, is there any way to display the error message that caused the failure on the web page. Thank you. Here's my code, $from= "Findings"; $where=""; if($service!= null) { $from = $from . ", ServiceType_Lookup"; $where= "Findings.ServiceType_ID= ServiceType_Lookup.ServiceType_ID AND ServiceType_Name= ". $service; if($keyword!= null) $where= $where . " AND "; } if( $keyword != null) { $where= $where . "Finding_ID LIKE '%$keyword%' OR ServiceType_ID LIKE '%$keyword%' OR Title LIKE '%$keyword%' OR RootCause_ID LIKE '%$keyword%' OR RiskRating_ID LIKE '%$keyword%' OR Impact_ID LIKE '%$keyword%' OR Efforts_ID LIKE '%$keyword%' OR Likelihood_ID LIKE '%$keyword%' OR Finding LIKE '%$keyword%' OR Implication LIKE '%$keyword%' OR Recommendation LIKE '%$keyword%' OR Report_ID LIKE '%$keyword%'"; } $query = "SELECT Finding_ID, ServiceType_ID, Title, RootCause_ID, RiskRating_ID, Impact_ID, Efforts_ID, Likelihood_ID, Finding, Implication, Recommendation, Report_ID FROM ".$from . " WHERE " . $w
& Database Submit Sign UP Login × Login Username Password Login Cancel Not a member? Sign Up Forgot Password? Contact Us
Die(mysql_error())
PHP MySQL query with error printing How to write SQL mysql get last error using PHP to handle the data in MySQL database? In any database driven script we have to update, mysql get error message in stored procedure add, modify, data in the tables. By using PHP we can do all this using different functions available in PHP. We will start with very basic function, which will execute any http://stackoverflow.com/questions/12227626/how-to-display-mysql-error-in-php query written in sql and can be applied to MySQL database. SQL Structured Query Language or popularly known as SQL is an universal language to handle database. An introduction and different types of sql command like select, insert, update etc you will get in the sql section of this site. There are some advance SQL commands like left join, linking of http://www.plus2net.com/php_tutorial/php_mysql_query.php tables etc to study. If you are not comfortable with SQL any time you can refer the materials in sql section. There are three steps invoved in this process. Connection to database Build the query and execute Display the data First ensure that you have established your mysql connection through PHP. To get the full details on php mysql connection you can read the article here. If you are using PDO then start with PDO connection string here. PHP Functions & SQL Let us start with the function required to execute one query in PHP. Once you have connection established then we can execute sql command by using PHP function mysql_query(). Here is the syntax of the function. Let us first write the query and store in a variable. We will write a query to create table.$query="CREATE TABLE student ( id int(2) NOT NULL auto_increment, name varchar(50) NOT NULL default '', class varchar(10) NOT NULL default '', mark int(3) NOT NULL default '0', PRIMARY KEY (id) ) TYPE=MyISAM"; We have stored the sql create query in a variable $query and we will p
Login Cancel Not a member? Sign Up Forgot Password? Contact Us Error message displaying after execution of a query We can display error message in case of an error generated by mysql http://www.plus2net.com/sql_tutorial/mysql_error.php query. This meaning full error message gives idea one the problem or bugs in the script. We can print the error message by using mysql function mysql_error(). This function returns the error message associated with most http://php.about.com/od/phpwithmysql/f/mysql_error.htm recently executed query. So it is a good idea to check for error before going to next query. We can even add the error number returned by mysql to this error message. This way we can mysql error generate a meaningful detail on the bugs on the script. MySQL Error MySQL Error No Error Message Error Mail $querry = mysql_query("SELECT new_field FROM student"); echo "Error message = ".mysql_error(); In our student table there is no field as new_field. Here here is the message we will get Error message = Unknown column 'new_field' in 'field list' Note that mysql_error() is deprecated as of PHP 5.5.0 so better to avoid using this function. how to echo So if you are using PDO then we can use errorInfo() to display the returned error message from MySQL Here is a sample code in PHP using PDO to display record and on failure to display error message. require 'config-pdo.php'; // database connection string $pdo=$dbo->prepare('Select no_name from student'); if($pdo->execute()){ echo 'Success
'; $row = $pdo->fetch(PDO::FETCH_OBJ); echo "Name : $row->name "; }else{ print_r($pdo->errorInfo()); } In the above code there is an error in sql , there is no column by name no_name. The output will be Array ( [0] => 42S22 [1] => 1054 [2] => Unknown column 'no_name' in 'field list' ) So to get correct result change the sql part like this . $pdo=$dbo->prepare('Select * from student'); With this you will get desired output. Handling PDO errors If such error occurs what is to be done ? We have three options to handle in case of errors. We can stop the execution of the script. ( Fatal error : stop execution of code ) We can display warning message ( Warning only, display message and no stoppage of execution ) Remain silent ( continue to execute and display error message if required ) Setting the PDO error handling attribute. We can use setAttribute to tell how to handle the PDO errors. Here is a sample $dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WAR
3 Connecting to a MySQL database from PHP 4 The 12 Days of PHP 5 10 Cool Things To Do With PHP About.com About Tech PHP/MySQL PHP with MySQL How can report MySQL errors into PHP? Robert Daly/OJO Images/Getty Images By Angela Bradley PHP/MySQL Expert Share Pin Tweet Submit Stumble Post Share By Angela Bradley Question: How can report MySQL errors into PHP?How can I receive feedback from MySQL when my PHP encounters a problem?Answer: When you are working with PHP and MySQL and encounter an error, it is helpful to receive more detailed information regarding the problem to point you in the direction of fixing it. You can use the mysql_error ( ) to get feedback from MySQL to your PHP document if you are encountering problems. It is phrased as: mysql_error ( [link_identifier] )Here are some examples of how to utilize mysql_error ()mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()) This will return an error if there is a problem connecting to your MySQL database $value = mysql_query($your_query) or die("A MySQL error has occurred.
Your Query: " . $your_query . "
Error: (" . mysql_errno() . ") " . mysql_error()) When you encounter an error, this will return your custom message (A MySQL error has occurred.
) followed by a line number, and the actual error. Show Full Article Related How to close your MySQL connection from PHP Do you know how to close a MySQL connection? MySQL Connect Files in PHP How to Connect PHP Files to a MySQL Database More from the Web Powered By ZergNet Sign Up for Our Free Newsletters Thanks, You're in! About Today Electronics & Gadgets PHP/MySQL You might also enjoy: Healthy Eating Health Tip of the Day Sign up There was an error. Please try again. Please select a newsletter. Please enter a valid email address. Did you mean ? Thank you,,for signing up! PHP/MySQ