简体   繁体   中英

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?

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM