How to output sql execution error message in php

藏色散人
Release: 2023-03-11 14:46:01
Original
3418 people have browsed it

How to output sql execution error information in php: first create a PHP sample file; then connect to the mysql database; finally output the sql execution error information through "echo mysql_error();".

How to output sql execution error message in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

How does php output sql execution error message?

PHP print database error message

$link = @mysql_connect("服务器", "账号", "密码") or die("自己的错误解释".mysql_error());   
echo mysql_error(); //打印数据错误信息
Copy after login

mysql_error() Function introduction:

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

This function returns the error text of the previous MySQL function, or '' (empty string) if there is no error.

Syntax

mysql_error(connection)
Copy after login

Parameters

connection Optional. Specifies the SQL connection identifier. If not specified, the last opened connection is used.

Example

In this example, we will try to log in to a MySQL server using an incorrect username and password:

<?php
$con = mysql_connect("localhost","wrong_user","wrong_pwd");
if (!$con)
  {
  die(mysql_error());
  }
mysql_close($con);
?>
Copy after login

The output will be similar to:

Access denied for user &#39;wrong_user&#39;@&#39;localhost&#39;
(using password: YES)
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to output sql execution error message in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template