tags:

views:

58

answers:

1

I am reading the PHP and mySQL web development book and so far been doing all the PHP and mysql using procedural. But then it 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 it defines what query and fetch_assoc does? Or what?

+1  A: 

You should be setting up $db something like this:

$db = new mysqli("db_server", "db_username", "db_password", "db_name");
John Rasch
doing it OOP doesn't require me to write a class does it? Am I to assume that $result=$db->query($query); will know what -> does?
jpjp
The `mysqli` class should already exist, and it should connect when constructed. It looks like your syntax is correct, which version of PHP are you running?
John Rasch
I'm running 5.3.1. It seems when I use fetch_row($result) it throws an error, but when I use fetch_assoc() it works. Btw, thanks for your help!
jpjp