Newbie Question about accessing mysql using OOP

I am reading PHP and mySQL web development book and so far have been doing all PHP and mysql using procedural. But then he talks about accessing mysql with objects.

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 that it determines what the request and fetch_assoc do? Or what?

+3
source share
1 answer

You should set up $dbsomething like this:

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

Source: https://habr.com/ru/post/1746308/


All Articles