3

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
  • Yes and could you show us how you are setting up $db? Commented May 20, 2010 at 3:34

1 Answer 1

1

You should be setting up $db something like this:

$db = new mysqli("db_server", "db_username", "db_password", "db_name");
Sign up to request clarification or add additional context in comments.

3 Comments

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?
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?
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.