Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display a MySQL error in PHP for a long query that depends on the user input? [duplicate]

Tags:

php

mysql

mysqli

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 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. Is there a way to display the error message that caused the failure on the web page?

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($this->db_link)); 
like image 391
Traveling Salesman Avatar asked Sep 01 '12 12:09

Traveling Salesman


People also ask

How can I get MySQL query error in php?

Description ¶ 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.

How do I display MySQL errors?

The SHOW COUNT(*) ERRORS statement displays the number of errors. You can also retrieve this number from the error_count variable: SHOW COUNT(*) ERRORS; SELECT @@error_count; SHOW ERRORS and error_count apply only to errors, not warnings or notes.

How do I show php errors?

Quickly Show All PHP Errors The quickest way to display all php errors and warnings is to add these lines to your PHP code file: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

Which command in php that identifies errors in database connection?

The connect_error / mysqli_connect_error() function returns the error description from the last connection error, if any.


2 Answers

Use this:

mysqli_query($this->db_link, $query) or die(mysqli_error($this->db_link));  # mysqli_query($link,$query) returns 0 if there's an error. # mysqli_error($link) returns a string with the last error message 

You can also use this to print the error code.

echo mysqli_errno($this->db_link); 

Take a look here and here

like image 186
Christian Avatar answered Oct 14 '22 16:10

Christian


I use the following to turn all error reporting on for MySQLi

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 

*NOTE: don't use this in a production environment.

like image 38
Arian Faurtosh Avatar answered Oct 14 '22 16:10

Arian Faurtosh