我对PDO的执行命令是有原因的吗?
我刚刚开始通过YouTube上的视频了解php OOP,我正在关注构建网站应用程序。
正如那个人所解释的那样,我必须使用PDO连接到SQL数据库。我遵循了他所做的一切,但是我的PDO执行部分不起作用,我不知道这是什么问题。代码在下面。它不会带来任何错误,只是没有带回数据库中的任何数据。我在函数之间放了一些回声,以找出代码走的距离,这就是我发现它准备查询但从未执行的。请帮助我解决这个问题。谢谢。
class Database{
private function connect()
{
$string = DBDRIVER . ":host =" .DBHOST.";dbname =".DBNAME;
if ($conn = new PDO($string, DBUSER, DBPASS)) {
return $conn;
} else {
die("Could not connect to database");
}
}
上面的代码是与数据库的连接。据我所知,它正常工作。
下面的代码应该将数据库中的数据返回以使用view.php文件显示的数据库返回
public function query($query, $data = array(), $data_type = "object")
{
$con = $this->connect();
$stm = $con->prepare($query);
print_r($stm);
if ($stm) {
$check = $stm->execute($data); //right here is where I concluded that the code stops
print_r($check);//does not print anything
if ($check) {
if ($data_type == "object") {
$data = $stm->fetchAll(PDO::FETCH_OBJ);
} else {
$data = $stm->fetchAll(PDO::FETCH_ASSOC);
}
if (is_array($data) && count($data) > 0) {
return $data;
}
}
}
return false;
}
,这是控制器中编写的零件,将查询分配给数据库以执行。
function index(){
$db = new Database();
$data = $db->query("select * from users");
if($data){echo $data;} //echos nothing because $data is empty
$this -> view('home', ['rows' => $data]);
}
I am just beginning to learn about PHP OOP through videos on youtube and I am following along in building a website application.
I have to connect to the SQL database using PDO as the guy explained. I followed everything he did but my execution part for the PDO is not working and I don't know what is wrong with it. The code is below. it does not bring up any errors it just doesn't bring back any data from the database. I put some echos in between the function to find out how far the code is going and that is how I found out that it prepares the query but never executes it. Please help me figure this out. Thank you.
class Database{
private function connect()
{
$string = DBDRIVER . ":host =" .DBHOST.";dbname =".DBNAME;
if ($conn = new PDO($string, DBUSER, DBPASS)) {
return $conn;
} else {
die("Could not connect to database");
}
}
The code above is the connection to the database. it is working correctly as far as I can tell.
The code below is supposed to return the data from the database back to be displayed using a view.php file
public function query($query, $data = array(), $data_type = "object")
{
$con = $this->connect();
$stm = $con->prepare($query);
print_r($stm);
if ($stm) {
$check = $stm->execute($data); //right here is where I concluded that the code stops
print_r($check);//does not print anything
if ($check) {
if ($data_type == "object") {
$data = $stm->fetchAll(PDO::FETCH_OBJ);
} else {
$data = $stm->fetchAll(PDO::FETCH_ASSOC);
}
if (is_array($data) && count($data) > 0) {
return $data;
}
}
}
return false;
}
below is the part written in the controller to assign the query to the database to execute.
function index(){
$db = new Database();
$data = $db->query("select * from users");
if($data){echo $data;} //echos nothing because $data is empty
$this -> view('home', ['rows' => $data]);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经测试了您的代码,因此发生了错误,因为您在dbname和=之间有一个空间。 DBNAME之后的半隆(不需要)。
现在对我有用。
我很长一段时间以来使用此连接类,也许您想使用它。
您可以使用以下代码使用连接类:
i have tested your code the error occurs because u have a space between the dbname and the =. A semicolon after the DBNAME is also missing (it is not needed).
Now it works for me.
i am using this connection class for a long time, maybe you want to use it.
You can use the connection class using the following code: