问题
I am switching to mysqli, because mysql_*
functions are not supported anymore. When I try to connect to my database, I get an error. Here is my code.
<?php
//ob
//ob_start();
//session
session_start();
//connect to database
$error = "Could not connect to database";
mysqli_connect('','************','**********') or die($error);
mysqli_select_db('********************') or die($error);
$session_username = $_SESSION['username'];
$session_coin = $_SESSION['coins'];
?>
Error:
Could not connect to database
Is that correct when trying to use mysqli?
回答1:
No, this is not the correct way to connect using mysqli.
The correct way is this:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'username', 'password', 'test_db');
$mysqli->set_charset('utf8mb4'); // always set the charset
Three steps:
- Enable error reporting.
- Create instance of mysqli class.
- Set the correct charset.
来源:https://stackoverflow.com/questions/13103419/error-when-using-mysqli-select-db-and-mysqli-connect