MySQL is the preferred database to use when dealing with PHP. It's an old system that works quite well still and doesn't look to be replaced anytime soon. We are going to connect to a database, select a table, select some data then print the data back out.
<?php
mysql_connect("127.0.0.1","root","") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
?>
After connecting and selecting a database, select some data from the tables.
<?php
mysql_connect("127.0.0.1","root","") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$query = mysql_query("select * from users");
$get_rows = mysql_fetch_array($query);
print_r($get_rows);
?>
Below is the sample database we used for this post.
CREATE TABLE IF NOT EXISTS `users` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`username` varchar(32) NOT NULL,
`password` varchar(32) NOT NULL,
`email` varchar(80) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
INSERT INTO `users` (`id`, `username`, `password`, `email`) VALUES
(1, 'admin', 'password', 'admin@unix.com'),
(2, 'codershed', 'admin', 'cs@codershed.us'),
(3, 'cs', 'cs', 'worker2@codershed.us');
No comments:
Post a Comment