0% found this document useful (0 votes)
85 views1 page

Connect to MySQL with PHP Guide

The document discusses connecting to a MySQL database from PHP. It shows code examples for connecting using mysql_connect(), selecting a database with mysql_select_db(), issuing queries with mysql_query(), retrieving results with mysql_fetch_assoc(), and closing the connection with mysql_close(). It then provides examples of inserting and updating data in a MySQL database from PHP.

Uploaded by

msodhani
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views1 page

Connect to MySQL with PHP Guide

The document discusses connecting to a MySQL database from PHP. It shows code examples for connecting using mysql_connect(), selecting a database with mysql_select_db(), issuing queries with mysql_query(), retrieving results with mysql_fetch_assoc(), and closing the connection with mysql_close(). It then provides examples of inserting and updating data in a MySQL database from PHP.

Uploaded by

msodhani
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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

You might also like