Print Mysql Error Message Php
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings mysqli error php and policies of this site About Us Learn more about Stack Overflow php mysql error handling the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation or die mysql error Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it mysql_query error 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 printed this message whenever the query fails but I am having
Die(mysql_error())
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 " . $where; echo "wala 2eshiq"; $this->result = $this->db_link->query($query); if (!$this->result) { printf("Query failed: %s\n", mysqli_connect_error()); exit; } $r = mysqli_query($this->db_link, $query); if($r==false) printf("error: %s\n", mysqli_errno($
here for a quick overview of the site Help Center Detailed answers to any questions you might have
Mysql Get Last Error
Meta Discuss the workings and policies of this site About Us mysqli_query error Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with mysql get error message in stored procedure us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just http://stackoverflow.com/questions/12227626/how-to-display-mysql-error-in-php like you, helping each other. Join them; it only takes a minute: Sign up Display error message PHP Mysql up vote 1 down vote favorite 2 i am unable to get the last 2 echos to work, even if the update query fails it still displays success. If anyone has any suggestions on this code to http://stackoverflow.com/questions/13909391/display-error-message-php-mysql be improved on any line, please do! Success"; echo "
Your account password was successfully changed. Please click here to login.
"; } else { echo "Error
"; echo "Sorry, but a field was incorrect.
"; } } ?> Thanks in advance! php mysql share|improve this question edited Dec 17 '12 at 6:42 Sterling Archer 13.5k104173 asked Dec 17 '12 at 6:34 sparkones 26118 Please don't use mysql_query in new applications. It's terribly dangerous if not used perfectly which is an enormous nuisance to do, though I've seen you're at least trying here. You escaped twoLearn Bootstrap Learn Graphics Learn Icons Learn How To JavaScript Learn JavaScript Learn jQuery Learn jQueryMobile Learn AppML Learn AngularJS Learn JSON Learn AJAX http://www.w3schools.com/php/func_mysqli_error.asp Server Side Learn SQL Learn PHP Learn ASP Web Building Web Templates http://www.plus2net.com/sql_tutorial/mysql_error.php Web Statistics Web Certificates XML Learn XML Learn XML AJAX Learn XML DOM Learn XML DTD Learn XML Schema Learn XSLT Learn XPath Learn XQuery × HTML HTML Tag Reference HTML Event Reference HTML Color Reference HTML Attribute Reference HTML Canvas Reference HTML SVG Reference Google mysql error Maps Reference CSS CSS Reference CSS Selector Reference W3.CSS Reference Bootstrap Reference Icon Reference JavaScript JavaScript Reference HTML DOM Reference jQuery Reference jQuery Mobile Reference AngularJS Reference XML XML Reference XML Http Reference XSLT Reference XML Schema Reference Charsets HTML Character Sets HTML ASCII HTML ANSI HTML Windows-1252 HTML ISO-8859-1 HTML Symbols HTML UTF-8 Server Side PHP Reference print mysql error SQL Reference ASP Reference × HTML/CSS HTML Examples CSS Examples W3.CSS Examples Bootstrap Examples JavaScript JavaScript Examples HTML DOM Examples jQuery Examples jQuery Mobile Examples AngularJS Examples AJAX Examples XML XML Examples XSLT Examples XPath Examples XML Schema Examples SVG Examples Server Side PHP Examples ASP Examples Quizzes HTML Quiz CSS Quiz JavaScript Quiz Bootstrap Quiz jQuery Quiz PHP Quiz SQL Quiz XML Quiz × PHP Tutorial PHP HOME PHP Intro PHP Install PHP Syntax PHP Variables PHP Echo / Print PHP Data Types PHP Strings PHP Constants PHP Operators PHP If...Else...Elseif PHP Switch PHP While Loops PHP For Loops PHP Functions PHP Arrays PHP Sorting Arrays PHP Superglobals PHP Forms PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete PHP Advanced PHP Arrays Multi PHP Date and Time PHP Include PHP File Handling PHP File Open/Read PHP File Create/Write PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP Error Handling PHP Exception MySQL Database MySQL Database MySQL Connect MySQL Create DB
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 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 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 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. 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 di