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!
精彩评论