简体   繁体   English

mysql 连接失败语句不起作用

[英]mysql failed connection statement is not working

I'm trying to check the connection with MySQL databse using the following code:我正在尝试使用以下代码检查与 MySQL 数据库的连接:

<?php
define('DBHOST','localhost');
define('DBUSER','root');
define('DBPASS','');
define('DBNAME','ts');


$link = mysqli_connect(DBHOST,DBUSER,DBPASS,DBNAME);

    if($link === false){
      header('Location: dberror.php');
    }else {
      echo "connected successfully";
    }

?>

When I try to give incorrect information to test the statement if the connection is not established like giving an incorrect database name, the header is not working and shows the following error:当我尝试提供不正确的信息来测试语句时,如果连接未建立(例如提供不正确的数据库名称),header 无法正常工作并显示以下错误:

Fatal error: Uncaught mysqli_sql_exception: Unknown database 'ts' in C:\xampp\htdocs\tst\dbcon.php:8 Stack trace: #0 C:\xampp\htdocs\tst\dbcon.php(8): mysqli_connect('localhost', 'root', '', 'ts') #1 {main} thrown in C:\xampp\htdocs\tst\dbcon.php on line 8 Fatal error: Uncaught mysqli_sql_exception: Unknown database 'ts' in C:\xampp\htdocs\tst\dbcon.php:8 Stack trace: #0 C:\xampp\htdocs\tst\dbcon.php(8): mysqli_connect(' localhost', 'root', '', 'ts') #1 {main} 在第 8 行的 C:\xampp\htdocs\tst\dbcon.php 中抛出

But when the connection is established the else statement is working.但是当建立连接时, else 语句正在工作。

So please how to make a user redirection if the connection failed?那么如果连接失败,请问如何进行用户重定向?

Return values for mysqli_connect() are an object or false . mysqli_connect()的返回值为objectfalse It only returns false if the connection fails.如果连接失败,它只返回false Try var_dump the result of mysqli_connect() to see if you're getting an object .尝试 var_dump mysqli_connect()的结果,看看你是否得到了object Remember the database field is optional because it can be selected later using mysqli_select_db() , so even if that's empty you will still get an object .请记住,数据库字段是可选的,因为稍后可以使用mysqli_select_db()选择它,所以即使它是空的,您仍然会得到object

Once the connection is made you can see errors using mysqli_error() .建立连接后,您可以使用mysqli_error()查看错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM