Why would real_connect return null?
For some weird reason, the following code is returning null, while the manual states it should either return true or false. There is also no information in the mysqli object.
Code
// Initialize MySQLi
$this->mysqli = new mysqli();
// Connect to the server
var_dump($this->mysqli);
var_dump($this->mysqli->real_connect($host, $username, $password, $database));
var_dump($this->mysqli);
Output
object(mysqli)#2 (17) { ["affected_rows"]=> NULL ["client_info"]=> NULL ["client_version"]=> int(50141) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> NULL ["error"]=> NULL ["field_count"]=> NULL ["host_info"]=> NULL ["info"]=> NULL ["insert_id"]=> NULL ["server_info"]=> NULL ["server_version"]=> NULL ["sqlstate"]=> NULL ["protocol_version"]=> NULL ["thread_id"]=> NULL ["warning_count"]=> NULL }
NULL
object(mysqli)#2 (17) { ["affected_rows"]=> NULL ["client_info"]=> NULL ["client_version"]=> int(50141) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> NULL ["error"]=> NULL ["field_count"]=> NULL ["host_info"]=> NULL ["info"]=> NULL ["insert_id"]=> NULL ["server_info"]=> NULL ["server_version"]=> NULL ["sqlstate"]=> NULL ["protoc开发者_StackOverflow中文版ol_version"]=> NULL ["thread_id"]=> NULL ["warning_count"]=> NULL }
You are not using it correctly.
You need to create the object using mysqli_init()
.
Or you put the connection info in the constructor and don't call real_connect
.
I wonder what the context is for your code? I just worked on a project where I set this in a config.php file..
<?php
$h='localhost';$u='username';$pw='PW';$db='test';
$sqli = new mysqli($h,$u,$pw,$db); ?>
then in a functions file this...
<?php
include_once 'include/config.php';
class User {
public function __construct($sqli) {
$this->sqli=$sqli;
} //etc.
} ?>
The connection works well and you call your connection within Class User functions with $this->sqli -> query("SELECT... for example. Pass the connection when you create a user ... $user = new User($sqli);
精彩评论