php-database-mysql-tutorial
PHP is an easy programming language and can quickly interact with the free relational
database, MySQL (for a little while until Oracle who bought Sun officially kill it)...
The first thing you'll want to do is make a connection to the database (hopefully you
already know how to create a database and a user?)
THIS IS THE LINUX COMMAND LINE, NOT PHP...
mysqladmin -u username -p create databasename
mysql -u username -p
THIS IS A MYSQL COMMAND FED INTO MYSQL SOMEHOW...
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER
ON databasename.*
TO 'username'@'localhost' IDENTIFIED BY 'password';
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ENOUGH SYS/WEBADMIN GEEKINESS, BACK TO PROGRAMMING!
Typically you will want to connect as a php function in an include file...
dbconnect.php
$dbhost = 'localhost';
$dbuser = 'example_dbuser';
$dbpass = 'password';
$dbname = 'example_database';
/* easy future maintenance by defining variables at the beginning! */
/* this php function connects using the above info */
$dbconn = mysql_connect( $dbhost, $dbuser, $dbpass );
if(!$dbconn)
{ die('Could not connect: ' . mysql_error()); }
else
{ echo "Connected to MySQL $dbhost<br>\n"; }
$db = mysql_select_db($dbname);
if(!$db)
{ die ('Can\'t connect to database : ' . mysql_error()); }
else{ echo "Connected to database $dbname<br>\n"; }
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
A forward looking example might just display everything, probably
from the "default", information_schema database?
display-my-mysql.php
echo "MySQL: " . mysql_get_server_info($dbconn);
echo "<br><br>\n";
echo "SHOW DATABASES<br>\n";
$response = mysql_query("SHOW DATABASES",$dbconn);
while ($row = mysql_fetch_assoc($response))
{ echo $row['Database'] . "<br>\n"; }
echo "<br>\n";
echo "SHOW TABLES<br>\n";
$response = mysql_query("SHOW TABLES",$dbconn);
while ($row = mysql_fetch_row($response))
{ echo $row[0] . "<br>\n"; }
echo "<br>\n";
$response = mysql_query("SHOW DATABASES",$dbconn);
print_r(mysql_fetch_array($response));
echo "<br>\n";
$sql = "SHOW TABLES";
$response = mysql_query($sql,$dbconn);
print_r(mysql_fetch_array($response));
echo "<br>\n";
mysql_close($dbconn);