SlideShare a Scribd company logo
More PHP & MYSQL
Connecting to MYSQL mysql_connect(servername,username,password); <?php  $con=mysql_connect('localhost','root','');  if (!$con) {  die('Could not connect: ' . mysql_error());  } ?>
Creating a MYSQL Database CREATE DATABASE database_name   <?php  $con = mysql_connect('localhost', 'root', '');  if (!$con) { die('Could not connect: ' . mysql_error()); } if (mysql_query('CREATE DATABASE my_db',$con)) {  echo &quot;Database 'my_db' created&quot;; }  else {  echo &quot;Error creating database: &quot; . mysql_error();  }  mysql_close($con);  ?>
Creating a MYSQL Table CREATE TABLE table_name   ( column_name1 data_type,   column_name2 data_type,  .......  )  <?php  $con = mysql_connect('localhost', 'root', '');  if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db('my_db', $con);  $sql = 'CREATE TABLE person  ( FirstName varchar(15), LastName varchar(15), Age int )';  mysql_query($sql,$con); mysql_close($con);  ?>
Insert Using PHP Inserting data to MySQL is done by using  mysql_query()  to execute  INSERT  command.  Note that the query string  should not  end with a semicolon.  Below is an example of adding a new MySQL person by inserting a new row into table person in database “my_db”   <?php  $con = mysql_connect('localhost', 'root', '');  if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db('my_db', $con);  $query = 'INSERT INTO person (Firstname, Lastname, Age)  VALUES (&quot;Jesus&quot;, &quot;Christ&quot;, 33)'; mysql_query($query) or die('Error, insert query failed'); mysql_close($con);  ?>
Insert Using FORMS and PHP <?php if(isset($_POST['add'])){ $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'my_db'; mysql_select_db($dbname); $CFName = $_POST['CFName']; $CSName = $_POST['CSName']; $CAge = $_POST['CAge']; $query = $query = &quot;INSERT INTO person (Firstname, Lastname, Age) VALUES ('$CFName', '$CSName', $CAge)&quot;; mysql_query($query) or die('Error, insert query failed'); mysql_close($conn); echo &quot;person added to database&quot;; } else{ ?>
FORM Insert cont. <form method=&quot;post&quot;> <table width=&quot;400&quot; border=&quot;0&quot; cellspacing=&quot;1&quot; cellpadding=&quot;2&quot;> <tr>  <td width=&quot;100&quot;>first name</td> <td><input name=&quot;CFName&quot; type=&quot;text&quot; id=&quot;CFName&quot;></td> </tr> <tr>  <td width=&quot;100&quot;>second name</td> <td><input name=&quot;CSName&quot; type=&quot;text&quot; id=&quot;CSName&quot;></td> </tr> <tr>  <td width=&quot;100&quot;>age</td> <td><input name=&quot;CAge&quot; type=&quot;text&quot; id=&quot;CAge&quot;></td> </tr> <tr>  <td width=&quot;100&quot;>&nbsp;</td> <td><input name=&quot;add&quot; type=&quot;submit&quot; id=&quot;add&quot; value=&quot;Add  New Contact&quot;></td> </tr> </table> </form> <?php } ?>
Data Update Using PHP Use  mysql_query()  to execute the UPDATE or DELETE statement. For instance, to update the  age  in a MYSQL table for  name  “Jesus”, execute an UPDATE statement with  mysql_query()  like this: <?php  $con = mysql_connect('localhost', 'root', '');  if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db('my_db', $con);  $query = 'UPDATE person SET Age = (55) WHERE Firstname = &quot;Jesus&quot;'; mysql_query($query) or die('Error, insert query failed'); mysql_close($con);  ?>
Good Websites http://www.php-mysql-tutorial.com/ DMX Zone

More Related Content

More Php

  • 1. More PHP & MYSQL
  • 2. Connecting to MYSQL mysql_connect(servername,username,password); <?php $con=mysql_connect('localhost','root',''); if (!$con) { die('Could not connect: ' . mysql_error()); } ?>
  • 3. Creating a MYSQL Database CREATE DATABASE database_name <?php $con = mysql_connect('localhost', 'root', ''); if (!$con) { die('Could not connect: ' . mysql_error()); } if (mysql_query('CREATE DATABASE my_db',$con)) { echo &quot;Database 'my_db' created&quot;; } else { echo &quot;Error creating database: &quot; . mysql_error(); } mysql_close($con); ?>
  • 4. Creating a MYSQL Table CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, ....... ) <?php $con = mysql_connect('localhost', 'root', ''); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db('my_db', $con); $sql = 'CREATE TABLE person  ( FirstName varchar(15), LastName varchar(15), Age int )'; mysql_query($sql,$con); mysql_close($con); ?>
  • 5. Insert Using PHP Inserting data to MySQL is done by using mysql_query() to execute INSERT command. Note that the query string should not end with a semicolon. Below is an example of adding a new MySQL person by inserting a new row into table person in database “my_db” <?php $con = mysql_connect('localhost', 'root', ''); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db('my_db', $con); $query = 'INSERT INTO person (Firstname, Lastname, Age) VALUES (&quot;Jesus&quot;, &quot;Christ&quot;, 33)'; mysql_query($query) or die('Error, insert query failed'); mysql_close($con); ?>
  • 6. Insert Using FORMS and PHP <?php if(isset($_POST['add'])){ $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'my_db'; mysql_select_db($dbname); $CFName = $_POST['CFName']; $CSName = $_POST['CSName']; $CAge = $_POST['CAge']; $query = $query = &quot;INSERT INTO person (Firstname, Lastname, Age) VALUES ('$CFName', '$CSName', $CAge)&quot;; mysql_query($query) or die('Error, insert query failed'); mysql_close($conn); echo &quot;person added to database&quot;; } else{ ?>
  • 7. FORM Insert cont. <form method=&quot;post&quot;> <table width=&quot;400&quot; border=&quot;0&quot; cellspacing=&quot;1&quot; cellpadding=&quot;2&quot;> <tr> <td width=&quot;100&quot;>first name</td> <td><input name=&quot;CFName&quot; type=&quot;text&quot; id=&quot;CFName&quot;></td> </tr> <tr> <td width=&quot;100&quot;>second name</td> <td><input name=&quot;CSName&quot; type=&quot;text&quot; id=&quot;CSName&quot;></td> </tr> <tr> <td width=&quot;100&quot;>age</td> <td><input name=&quot;CAge&quot; type=&quot;text&quot; id=&quot;CAge&quot;></td> </tr> <tr> <td width=&quot;100&quot;>&nbsp;</td> <td><input name=&quot;add&quot; type=&quot;submit&quot; id=&quot;add&quot; value=&quot;Add New Contact&quot;></td> </tr> </table> </form> <?php } ?>
  • 8. Data Update Using PHP Use mysql_query() to execute the UPDATE or DELETE statement. For instance, to update the age in a MYSQL table for name “Jesus”, execute an UPDATE statement with mysql_query() like this: <?php $con = mysql_connect('localhost', 'root', ''); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db('my_db', $con); $query = 'UPDATE person SET Age = (55) WHERE Firstname = &quot;Jesus&quot;'; mysql_query($query) or die('Error, insert query failed'); mysql_close($con); ?>