1 / 9

Using MySQL with PHP/Perl

Using MySQL with PHP/Perl. Min Yang Jung November 13, 2008. MySQL + PHP. Open a connection <?php $dbhost = 'localhost'; $dbhost = 'db.php-mysql-tutorial.com:3306'; $dbuser = 'root'; $dbpass = 'password';

irene-wade
Download Presentation

Using MySQL with PHP/Perl

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using MySQL with PHP/Perl Min Yang Jung November 13, 2008

  2. MySQL + PHP • 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. MySQL + PHP • Close a connection <?php // it does nothing but closing // a mysql database connection mysql_close($conn); ?>

  4. MySQL + PHP • SELECT <?phpinclude '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. MySQL + PHP • INSERT <?phpinclude 'config.php';include 'opendb.php'; mysql_select_db($mysql);$query = "INSERT INTO user (host, user, password, select_priv, insert_priv, update_ priv) VALUES ('localhost', 'phpcake', 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. MySQL + PERL • Open a Connection #!/usr/bin/perl use Mysql; print "Content-type: text/html \n\n"; $host = "localhost"; $database = "store"; $tablename = "inventory"; $user = "username"; $pw = "password"; $connect = Mysql->connect($host, $database, $user, $pw); $connect->selectdb($database);

  7. MySQL + PERL • Close a Connection # free result set mysql_free_result($result); # close the connection mysql_close($connect); ?>

  8. MySQL + PERL • 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. MySQL + PERL • 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 />";

More Related