connect to MySQL Syntax Ex
: : :
Ex
mysql_connect() function mysql_connect (host,username,password) $dbconn = mysql_connect(localhost,root,root); Or $host = localhost; $username = root; $password = root; $dbconn = mysql_connect($host, $username, $password); $dbconn = mysql_connect($host,$username,$password) or die (Cannot connect to database server); mysql_select_db() function mysql_query() function mysql_fetch_assoc() function mysql_close() function.
Interact with a specific database Statements are issued using Retrieving the results terminate the connection
: : : :
Ex:
<?php
$host = localhost; $username = root; $password = root; $dbconn = mysql_connect($host,$username,$password) or die (Cannot connect to database server); $db = mysql_select_db(mysql) or die (Could not connect to database\n); $result = mysql_query(select user,host from user); print <table border=1><P>\n; while ($row = mysql_fetch_assoc($result)) { print <tr>; while (list ($key,$value) = each($row)) { print <td>$value </td>; } while ($row = mysql_fetch_row($result)) { print <tr>; print <td>$row[0]</td>; print <td>$row[1]</td>; print </tr>; }
print </tr>; } print </table><P>\n; ?>
Inserting data in MySQL
<?php $dbconn = mysql_connect($host,$username,$password) or die (Cannot connect to database server); $db = mysql_select_db(ecommerce) or die (Could not connect to database\n); $result = mysql_query(INSERT INTO customer VALUES (Manish,Kota,5000)) or die (Could not execute insert\n); # Determine the number of inserted rows. $rows_inserted = mysql_affected_rows($dbconn); print Inserted $rows_inserted rows\n; ?>
Updating data in MySQL