tags:

views:

85

answers:

1

I would like to access a MySQL database using PHP.

Can someone please explain the basic steps involved in doing that?

+4  A: 

I would suggest that you use the mysqli extension as opposed to the old, insecure mysql extension.

First get familiar with database design, SQL and MySQL, then follow this link.

Still want an example? Here you go:

<?php
$host = 'YOUR HOSTNAME HERE';
$user = 'YOUR USERNAME HERE';
$pass = 'YOUR PASSWORD HERE';
$db_name = 'YOUR DATABASE NAME HERE';

$db = new mysqli( $host, $user, $password, $db_name );

if( $db->errno ) {
  // an error occurred.
  exit();
}
...
$sql = 'SELECT * FROM some_table;';    

$result_set = $db->query( $sql );

...fetch results...

while( $obj = $result_set->fetch_object() ) {
   $value = $obj->some_property;
   ...do something with $value...
}

$db->close();

?>

Nuff said.

Good luck!

Jacob Relkin
thank you your answer was very useful
casperghost
thanks! Please upvote/accept in order that this question doesn't stay in the "unanswered" section.
Jacob Relkin
how can i upvote/accept
casperghost
lol theres a up arrow and a checkmark, you can click on both.
Jacob Relkin
on the left side at the top of this answer box.
Jacob Relkin
Be aware that you probably don't want anyone else see your database errors. `echo $db->error;` is only a good idea while you're debugging.
middus
@middus, good point.
Jacob Relkin