Successfully reported this slideshow.

More Php

1,624 views

Published on

Published in: Technology
  • Be the first to comment

More Php

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

×