简体   繁体   English

初学者关于使用OOP访问mysql的问题

[英]Beginner's Question about accessing mysql using OOP

I am reading the PHP and mySQL web development book and so far been doing all the PHP and mysql using procedural. 我正在阅读PHP和mySQL Web开发书,到目前为止,我一直在使用过程来完成所有的PHP和mysql。 But then it talks about accessing mysql with objects. 但后来它讨论了使用对象访问mysql。

This works for me:
//I define $db so can connect
$query="select * FROM testing";
$result=mysqli_query($db,$query);
while($row=mysqli_fetch_array($result)){
  //echo the data
}

But when I try to do it with classes, it doesn't
@ $db=new mysqli('localhost','root','','test');
if(mysqli_connect_errno()){
echo "ERROR:";
exit;
}
$query="select * FROM testing";
$result=$db->query($query);
$row=$result->fetch_assoc();

Do I have to write my own class so it defines what query and fetch_assoc does? 我是否必须编写自己的类,以便定义查询和fetch_assoc的作用? Or what? 要不然是啥?

你应该设置这样的$db

$db = new mysqli("db_server", "db_username", "db_password", "db_name");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM