casperghost
casperghost

Reputation: 13

Using PHP to access a MySQL database

I would like to access a MySQL database using PHP.

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

Upvotes: 1

Views: 222

Answers (1)

Jacob Relkin
Jacob Relkin

Reputation: 163228

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!

Upvotes: 5

Related Questions