Error when using mysqli_select_db and mysqli_connect

為{幸葍}努か 提交于 2020-05-14 08:45:18

问题


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:

  1. Enable error reporting.
  2. Create instance of mysqli class.
  3. Set the correct charset.


来源:https://stackoverflow.com/questions/13103419/error-when-using-mysqli-select-db-and-mysqli-connect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!