简体   繁体   中英

Check if connected to database (right username/password, host)

I would love to check if i can connect to database using given username, password, and database host.

So far i was trying:

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$conndatabase = mysql_select_db($dbname);

if(!mysql_ping($conn) || !$conndatabase){ 
    //do something when cant connect 
} else { 
    //do something when you connected
}

It works when i give bad $dbname , cuz it cant select this database. But when i give wrong host, username or password, it will give me white page with errors. For example:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'rootx'@'localhost' (using password: YES) in C:\xampp\htdocs\strony\planer\config\opendb.php on line 6
Warning: mysql_ping() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\strony\planer\grupy.php on line 3

Question:
Is there a way to check if i can connect using given data without errors? It just gives errors before getting to mysql_ping()

EDIT
After trying Mike Brant solution, i think i could have explained it wrong. I am trying to connect to database, and if it lets me, it shows user page he wanted to access, but when it's impossible, it redirects him to the other page, to modify database information.

if(!$conndatabase){
    header('Location: index.php');
}

I am using that for checking if database exists. But if i try if(!$conn) or something similar, its too late, because i got errors displayed on $conn = mysql_connect($dbhost, $dbuser, $dbpass) . So i cant redirect, as it gives me

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost'~~
Warning: Cannot modify header information - headers already sent by~~

Yes. Just test the value of $conn . If it is false then the connection failed, and you shouldn't even try to proceed to the db selection step.

Your code might look like this:

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if (false === $conn) {
    throw new Exception('Could not connect to database: ' . mysql_error());
}
$db_select = mysql_select_db($dbname. $conn);
if (false === $db_select) {
    throw new Exception('Could not select database: ' . mysql_error($conn));
}

The PHP documentation is very clear on this: http://php.net/manual/en/function.mysql-connect.php

By the way, you should look at using mysqli or PDO as mysql_* functions are deprecated.

Try to debug your connection with something like

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if(!$link){
die('Could not connect: ' . mysql_error());
}

In this way you can easilly check if connection is estabilished otherwise you will print an error.

Then I would like to remember you that mysql_ functions are deprecated so i would advise you to switch to mysqli or PDO

When you look on my edit in question, you can see those answers didn't solve my problem completely. I still lacked some way to avoid errors. That's where i used error_reporting(0); . I am not sure if that's best solution, but it works like a charm for me. So the code for everything i tried to achieve looks like that:

opendb.php file

<?php
   $dbhost = 'localhost';
   $dbuser = 'root';
   $dbpass = 'password123';
   $dbname = 'databasename';
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   $conndatabase = mysql_select_db($dbname);
?>

And there on the site i am checking connection:

<?php
   error_reporting(0);
   include 'opendb.php';
   if(!mysql_ping($conn) || !$conndatabase){
        header('Location: index.php'); //or any other site where you can config your database connection information.
   }

So yeah, thats complete solution to my problem. I guess i could use !$conn instead of !mysql_ping($conn) with same result.

As has already been said, it's the first example on the PHP manual page for the mysql_connect function.

Secondly, you should be either using the mysqli_ functions or PDO , as the mysql_ functions are deprecated and currently being phased out.

To check a connection with PDO:

try {
    $db = new PDO('mysql:host=127.0.0.1;dbname=dbname', 'user', 'pass');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
    header('HTTP/1.1 500 Internal Server Error');
    die($e->getMessage());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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