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

ODP
Maintaining your own branch of Drupal core
PDF
Secure Coding with WordPress - WordCamp SF 2008
PPT
Building an e:commerce site with PHP
ODP
MuseScore MusicHackDay Presentation
PDF
Cheap frontend tricks
PDF
Mojolicious
PPTX
SQL Injection Part 2
PPT
How to learn j query
Maintaining your own branch of Drupal core
Secure Coding with WordPress - WordCamp SF 2008
Building an e:commerce site with PHP
MuseScore MusicHackDay Presentation
Cheap frontend tricks
Mojolicious
SQL Injection Part 2
How to learn j query

What's hot (20)

PPTX
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
ODP
Mojolicious on Steroids
PDF
Functional testing with capybara
PDF
Make your own wp cli command in 10min
KEY
Introduction to jQuery - Barcamp London 9
KEY
DBIx::Skinnyと仲間たち
PDF
Embracing Capybara
PDF
WordPress Plugin & Theme Security - WordCamp Melbourne - February 2011
PDF
Php mail program
PDF
Mojolicious: what works and what doesn't
PDF
RESTful web services
ODP
Faking Data
PDF
Accelerated Native Mobile Development with the Ti gem
TXT
Getfilestruct zbksh(1)
PDF
WordPress Kitchen 2014 - Александр Стриха: Кеширование в WordPress
PDF
Shortcodes In-Depth
PDF
Mojolicious, real-time web framework
TXT
Mkscript sh
PPTX
Wp meetup custom post types
KEY
Mojolicious - A new hope
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Mojolicious on Steroids
Functional testing with capybara
Make your own wp cli command in 10min
Introduction to jQuery - Barcamp London 9
DBIx::Skinnyと仲間たち
Embracing Capybara
WordPress Plugin & Theme Security - WordCamp Melbourne - February 2011
Php mail program
Mojolicious: what works and what doesn't
RESTful web services
Faking Data
Accelerated Native Mobile Development with the Ti gem
Getfilestruct zbksh(1)
WordPress Kitchen 2014 - Александр Стриха: Кеширование в WordPress
Shortcodes In-Depth
Mojolicious, real-time web framework
Mkscript sh
Wp meetup custom post types
Mojolicious - A new hope
Ad

Viewers also liked (7)

PPT
tecnologias
PPT
Debunking The Myths About Online Learning
PDF
抵學分
PPT
4 Sportello Donna
PPT
PPT
1 Introduzione
tecnologias
Debunking The Myths About Online Learning
抵學分
4 Sportello Donna
1 Introduzione
Ad

Similar to More Php (20)

PPT
Php My Sql
PPT
Php Basic Security
PPT
Ubi comp27nov04
PPT
Drupal Lightning FAPI Jumpstart
PPT
Exploiting Php With Php
PPT
P H P Part I I, By Kian
ODP
Zendcon 2007 Features
PPTX
Mysql Script
ODP
Modern Perl
PPTX
Views notwithstanding
PPTX
Connecting_to_Database(MySQL)_in_PHP.pptx
ODP
Into to DBI with DBD::Oracle
PDF
PHP and Rich Internet Applications
PPT
Introduction To Moco
ODP
Ae internals
PPT
High-level Web Testing
ODP
HTML::FormHandler
PDF
SULTHAN's - PHP MySQL programs
Php My Sql
Php Basic Security
Ubi comp27nov04
Drupal Lightning FAPI Jumpstart
Exploiting Php With Php
P H P Part I I, By Kian
Zendcon 2007 Features
Mysql Script
Modern Perl
Views notwithstanding
Connecting_to_Database(MySQL)_in_PHP.pptx
Into to DBI with DBD::Oracle
PHP and Rich Internet Applications
Introduction To Moco
Ae internals
High-level Web Testing
HTML::FormHandler
SULTHAN's - PHP MySQL programs

More from Digital Insights - Digital Marketing Agency (20)

PPT
Diploma-GCD-ContentMarketing-Personas-Week2
PPT
DigitalInsights-DigitalMarketingStrategy&Planning
PPT
PPT
DBS-Week2-DigitalStrategySession
PPT
GCD-Measurement&Analytics-Week11
PPT
DBS-Week1-Introduction&OverviewDigitalMarketing
PPT
DCEB-DigitalStrategySession-Jan24th
PPTX
PPT
PPTX
Week12-DBS-ReviewAndPlanningSession
PPT
GCD-Week5-Facebook-LinkedIn-Ads
PPT
DBS-Week11-Measurement&Analyics
PPT
PPTX
DBS-Week10-EcommSites&SalesFunnells
PPT
GCD-Week5-SocialMediaPlatforms
PPT
DBS-Week3-B2C&B2B-ContentMarketing-Session
PPTX
DBS-Week9-Wordpress-Session
Diploma-GCD-ContentMarketing-Personas-Week2
DigitalInsights-DigitalMarketingStrategy&Planning
DBS-Week2-DigitalStrategySession
GCD-Measurement&Analytics-Week11
DBS-Week1-Introduction&OverviewDigitalMarketing
DCEB-DigitalStrategySession-Jan24th
Week12-DBS-ReviewAndPlanningSession
GCD-Week5-Facebook-LinkedIn-Ads
DBS-Week11-Measurement&Analyics
DBS-Week10-EcommSites&SalesFunnells
GCD-Week5-SocialMediaPlatforms
DBS-Week3-B2C&B2B-ContentMarketing-Session
DBS-Week9-Wordpress-Session

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
KodekX | Application Modernization Development
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Electronic commerce courselecture one. Pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Empathic Computing: Creating Shared Understanding
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
KodekX | Application Modernization Development
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Review of recent advances in non-invasive hemoglobin estimation
Electronic commerce courselecture one. Pdf
Unlocking AI with Model Context Protocol (MCP)
Building Integrated photovoltaic BIPV_UPV.pdf
Big Data Technologies - Introduction.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Spectral efficient network and resource selection model in 5G networks
Programs and apps: productivity, graphics, security and other tools
Empathic Computing: Creating Shared Understanding
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Encapsulation_ Review paper, used for researhc scholars
MYSQL Presentation for SQL database connectivity
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Mobile App Security Testing_ A Comprehensive Guide.pdf

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); ?>