【发布时间】:2017-04-18 05:24:07
【问题描述】:
dbclass.php
<?php
class DB{
public static function connect(){
$conn = mysqli_connect("localhost","root","yash","sample");
return $conn;
}
}
$dbb = new DB;
$dbb->connect();
?>
classone.php
<?php
include('dbclass.php');
class Books {
private $title;
private $price;
private $conn;
function __construct($title,$price){
$this->title = $title;
$this->price = $price;
}
function getDetails(){
echo $this->title."</br>";
echo $this->price;
}
public function insertbook(){
$conn = DB::connect();
$q1 = "INSERT INTO sbook (title,price) VALUES ($this->title,$this->price)";
$run = mysqli_query($conn,$q1);
}
}
$physics = new Books("physics",20);
$physics->getDetails();
$physics->insertbook();
?>
即使在mysqli_query 中传递了$conn 变量后,我也无法在数据库中插入值。
无法弄清楚,发生了什么。
【问题讨论】:
-
和准备好的陈述
-
我已经检查过了,print_r($run) 什么也没给我,但 print_r($q1) 给了我正确的确切查询。
-
db类中需要
$dbb = new DB; $dbb->connect();吗?