PHP-在null上调用成员函数query()-错误
我有以下php代码用于连接到我的数据库:PHP-在null上调用成员函数query()-错误,php,mysql,null,call,member,Php,Mysql,Null,Call,Member,我有以下php代码用于连接到我的数据库: <?php class MY_SQL{ private $username; private $password; private $conn; public function __construct($SERVERNAME){ $this->username = "username"; $this->password = "password"; if(
<?php
class MY_SQL{
private $username;
private $password;
private $conn;
public function __construct($SERVERNAME){
$this->username = "username";
$this->password = "password";
if($SERVERNAME == "data_"){
$server = "Servername";
}
else {
$server = $SERVERNAME;
}
// Create connection
$conn = new mysqli($server, $this->username, $this->password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
}
public function SQLCommand($cmd) {
if ( $this->conn->query($cmd) === TRUE ) {
echo "New record created successfully";
} else {
echo "Error: " . $cmd . "<br>" . $conn->error;
}
}
}
$sql = "INSERT INTO _test(test1, test2) VALUES ('hello','hi');";
$database = new MY_SQL("Servername");
$database->SQLCommand($sql);
?>
我建议你用这个例子改进你的课堂(摘自)
final class My\u SQLi
{
私人连接;
公共函数构造($hostname、$username、$password、$database、$port='3306')
{
$this->connection=new\mysqli($hostname、$username、$password、$database、$port);
如果($this->connection->connect\u错误){
抛出新的\异常('Error:'.$this->connection->Error.
错误号:'.$this->connection->errno);
}
$this->connection->set_字符集(“utf8”);
$this->connection->query(“设置SQL_模式=”);
}
公共函数查询($sql)
{
$query=$this->connection->query($sql);
如果(!$this->connection->errno){
if($query instanceof\mysqli\u result){
$data=array();
而($row=$query->fetch\u assoc()){
$data[]=$row;
}
$result=new\stdClass();
$result->num\u rows=$query->num\u rows;
$result->row=isset($data[0])?$data[0]:数组();
$result->rows=$data;
$query->close();
返回$result;
}否则{
返回true;
}
}否则{
抛出新的\异常('Error:'.$this->connection->Error.
错误号:'.$this->connection->errno.
.$sql);
}
}
公共功能转义($value)
{
返回$this->connection->real\u escape\u字符串($value);
}
公共职能受影响()
{
返回$this->connection->受影响的\u行;
}
公共函数getLastId()
{
返回$this->connection->insert\u id;
}
公共功能已连接()
{
返回$this->connection->ping();
}
公共函数_udestruct()
{
$this->connection->close();
}
}
$sql=“插入_测试(test1,test2)值('hello','hi');”;
$mysql=newmyu-SQLi('host','user','password','db');
$result=$mysql->query($sql);
忘记选择数据库@萨蒂,你说得对!这就成功了!谢谢谢谢,我来看看!
$this->conn = $conn; in __construct()
final class My_SQLi
{
private $connection;
public function __construct($hostname, $username, $password, $database, $port = '3306')
{
$this->connection = new \mysqli($hostname, $username, $password, $database, $port);
if ($this->connection->connect_error) {
throw new \Exception('Error: ' . $this->connection->error . '<br />Error No: ' . $this->connection->errno);
}
$this->connection->set_charset("utf8");
$this->connection->query("SET SQL_MODE = ''");
}
public function query($sql)
{
$query = $this->connection->query($sql);
if (!$this->connection->errno) {
if ($query instanceof \mysqli_result) {
$data = array();
while ($row = $query->fetch_assoc()) {
$data[] = $row;
}
$result = new \stdClass();
$result->num_rows = $query->num_rows;
$result->row = isset($data[0]) ? $data[0] : array();
$result->rows = $data;
$query->close();
return $result;
} else {
return true;
}
} else {
throw new \Exception('Error: ' . $this->connection->error . '<br />Error No: ' . $this->connection->errno . '<br />' . $sql);
}
}
public function escape($value)
{
return $this->connection->real_escape_string($value);
}
public function countAffected()
{
return $this->connection->affected_rows;
}
public function getLastId()
{
return $this->connection->insert_id;
}
public function isConnected()
{
return $this->connection->ping();
}
public function __destruct()
{
$this->connection->close();
}
}
$sql = "INSERT INTO _test(test1, test2) VALUES ('hello','hi');";
$mysql = new My_SQLi('host', 'user', 'password', 'db');
$result = $mysql->query($sql);