Min Yang Jung
 November 13, 2008
 Open        a connection
<?php
$dbhost   =   'localhost';
$dbhost   =   'db.php-mysql-tutorial.com:3306';
$dbuser   =   ...
 Close    a connection
<?php
// it does nothing but closing
// a mysql database connection
mysql_close($conn);
?>
 SELECT
<?php
  include 'config.php';
  include 'opendb.php';
   $query = "SELECT name, subject, message FROM contact";
 ...
 INSERT
<?php
  include 'config.php';
  include 'opendb.php';
  mysql_select_db($mysql);
  $query = "INSERT INTO user (ho...
   Open a Connection
#!/usr/bin/perl
use Mysql;
print "Content-type: text/html nn";

$host = "localhost";
$database = "st...
   Close a Connection

# free result set
mysql_free_result($result);

# close the connection
mysql_close($connect);

?>
   SELECT
# DEFINE A MySQL QUERY
$myquery = "SELECT * FROM $tablename";

$execute = $connect->query($myquery);

$rownumbe...
   INSERT
$myquery = "INSERT INTO $tablename (id, product,
  quantity) VALUES (DEFAULT,'pineapples','15')";
$execute = $c...
Upcoming SlideShare
Loading in …5
×

MySQL

789 views
746 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
789
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

MySQL

  1. 1. Min Yang Jung November 13, 2008
  2. 2.  Open a connection <?php $dbhost = 'localhost'; $dbhost = 'db.php-mysql-tutorial.com:3306'; $dbuser = 'root'; $dbpass = 'password'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'petstore'; mysql_select_db($dbname); ?>
  3. 3.  Close a connection <?php // it does nothing but closing // a mysql database connection mysql_close($conn); ?>
  4. 4.  SELECT <?php include 'config.php'; include 'opendb.php'; $query = "SELECT name, subject, message FROM contact"; $result = mysql_query($query); while($row = mysql_fetch_row($result)) { $name = $row[0]; $subject = $row[1]; $message = $row[2]; echo "Name :$name <br>" . "Subject : $subject <br>" . "Message : $row <br><br>"; }
  5. 5.  INSERT <?php include 'config.php'; include 'opendb.php'; mysql_select_db($mysql); $query = "INSERT INTO user (host, user, password, select_pr iv, insert_priv, update_ priv) VALUES ('localhost', 'phpcak e', PASSWORD('mypass'), 'Y', 'Y', 'Y')"; mysql_query($query) or die('Error, insert query failed'); $query = "FLUSH PRIVILEGES"; mysql_query($query) or die('Error, insert query failed'); include 'closedb.php‘ ?>
  6. 6.  Open a Connection #!/usr/bin/perl use Mysql; print "Content-type: text/html nn"; $host = "localhost"; $database = "store"; $tablename = "inventory"; $user = "username"; $pw = "password"; $connect = Mysql->connect($host, $database, $user, $pw); $connect->selectdb($database);
  7. 7.  Close a Connection # free result set mysql_free_result($result); # close the connection mysql_close($connect); ?>
  8. 8.  SELECT # DEFINE A MySQL QUERY $myquery = "SELECT * FROM $tablename"; $execute = $connect->query($myquery); $rownumber = $execute->numrows(); $fieldnumber = $execute->numfields(); # PRINT THE RESULTS print $rownumber."<br />"; print $fieldnumber."<br />";
  9. 9.  INSERT $myquery = "INSERT INTO $tablename (id, product, quantity) VALUES (DEFAULT,'pineapples','15')"; $execute = $connect->query($myquery); $affectedrows = $execute->affectedrows($myquery); $lastid = $execute->insertid($myquery); print $affectedrows."<br />"; print $lastid."<br />";

×