1

I would like to access a MySQL database using PHP.

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

1

1 Answer 1

5

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!

Sign up to request clarification or add additional context in comments.

2 Comments

lol theres a up arrow and a checkmark, you can click on both.
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.

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.