SlideShare a Scribd company logo
Min Yang Jung
November 13, 2008
 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);
?>
 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";
$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>";
}
 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‘
?>
 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);
 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);
$rownumber = $execute->numrows();
$fieldnumber = $execute->numfields();
# PRINT THE RESULTS
print $rownumber."<br />";
print $fieldnumber."<br />";
 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 Content

What's hot (20)

PDF
6 windows-phone8-introduction-m6-slides
MasterCode.vn
 
PDF
Not Really PHP by the book
Ryan Kilfedder
 
PPT
Mysql DBI
Joe Christensen
 
PDF
Darkmira Tour PHP 2016 - Automatizando Tarefas com Phing
Matheus Marabesi
 
PPT
Lecture8 php page control by okello erick
okelloerick
 
PDF
Php 2
tnngo2
 
TXT
[Php] navigations
Vishal Gurujuwada
 
DOCX
Java script
Saint Columban College
 
ZIP
TerminalでTwitter
Yoshihiro Sugi
 
PDF
Groovy as a scripting language
Jenn Strater
 
PDF
[Php] navigations
Thai Pham
 
PDF
FormValidator::LazyWay で検証ルールをまとめよう
Daisuke Komatsu
 
PDF
How to calculate the optimal undo retention in Oracle
Jorge Batista
 
PDF
Não alimente os trolls: JavaScript é bonito - FrontInSM 2015
Rafael Specht da Silva
 
TXT
hotel managment
Melk Biks
 
PDF
MongoDB a document store that won't let you down.
Nurul Ferdous
 
PDF
Command-Oriented Architecture
Luiz Messias
 
PDF
API Design
Eddie Kao
 
PDF
Cycle.js: Functional and Reactive
Eugene Zharkov
 
PPTX
Coding for php with mysql
Rajamanickam Gomathijayam
 
6 windows-phone8-introduction-m6-slides
MasterCode.vn
 
Not Really PHP by the book
Ryan Kilfedder
 
Mysql DBI
Joe Christensen
 
Darkmira Tour PHP 2016 - Automatizando Tarefas com Phing
Matheus Marabesi
 
Lecture8 php page control by okello erick
okelloerick
 
Php 2
tnngo2
 
[Php] navigations
Vishal Gurujuwada
 
TerminalでTwitter
Yoshihiro Sugi
 
Groovy as a scripting language
Jenn Strater
 
[Php] navigations
Thai Pham
 
FormValidator::LazyWay で検証ルールをまとめよう
Daisuke Komatsu
 
How to calculate the optimal undo retention in Oracle
Jorge Batista
 
Não alimente os trolls: JavaScript é bonito - FrontInSM 2015
Rafael Specht da Silva
 
hotel managment
Melk Biks
 
MongoDB a document store that won't let you down.
Nurul Ferdous
 
Command-Oriented Architecture
Luiz Messias
 
API Design
Eddie Kao
 
Cycle.js: Functional and Reactive
Eugene Zharkov
 
Coding for php with mysql
Rajamanickam Gomathijayam
 

Similar to 与 PHP 和 Perl 使用 MySQL 数据库 (20)

PDF
The History of PHPersistence
Hugo Hamon
 
PPTX
Basics of Working with PHP and MySQL.pptx
jensas21
 
PDF
Dependency Injection
Rifat Nabi
 
DOCX
Php.docx
CHMuhammadZohaibZafa
 
PPTX
chapter_Seven Database manipulation using php.pptx
Getawu
 
PDF
veracruz
tutorialsruby
 
PDF
veracruz
tutorialsruby
 
PDF
veracruz
tutorialsruby
 
PDF
veracruz
tutorialsruby
 
PPTX
Practical MySQL.pptx
HussainUsman4
 
PPTX
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
Arti Parab Academics
 
KEY
Php 101: PDO
Jeremy Kendall
 
PDF
Pemrograman Web 8 - MySQL
Nur Fadli Utomo
 
PDF
Php (1)
pinalsadiwala
 
PDF
Pagination in PHP
Vineet Kumar Saini
 
ODP
Php 102: Out with the Bad, In with the Good
Jeremy Kendall
 
PDF
Mysql & Php
Inbal Geffen
 
PPTX
PHP DATABASE MANAGEMENT.pptx
CynthiaKendi1
 
The History of PHPersistence
Hugo Hamon
 
Basics of Working with PHP and MySQL.pptx
jensas21
 
Dependency Injection
Rifat Nabi
 
chapter_Seven Database manipulation using php.pptx
Getawu
 
veracruz
tutorialsruby
 
veracruz
tutorialsruby
 
veracruz
tutorialsruby
 
veracruz
tutorialsruby
 
Practical MySQL.pptx
HussainUsman4
 
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
Arti Parab Academics
 
Php 101: PDO
Jeremy Kendall
 
Pemrograman Web 8 - MySQL
Nur Fadli Utomo
 
Php (1)
pinalsadiwala
 
Pagination in PHP
Vineet Kumar Saini
 
Php 102: Out with the Bad, In with the Good
Jeremy Kendall
 
Mysql & Php
Inbal Geffen
 
PHP DATABASE MANAGEMENT.pptx
CynthiaKendi1
 
Ad

More from YUCHENG HU (20)

PDF
Confluencewiki 使用空间
YUCHENG HU
 
PDF
Git
YUCHENG HU
 
PDF
Presta shop 1.6 如何安装简体中文语言文件
YUCHENG HU
 
PDF
Logback 介绍
YUCHENG HU
 
PDF
Presta shop 1.6 详细安装指南
YUCHENG HU
 
PDF
Presta shop 1.6 的安装环境
YUCHENG HU
 
PDF
Presta shop 1.6 如何安装简体中文语言文件
YUCHENG HU
 
PDF
Presta shop 1.6 图文安装教程
YUCHENG HU
 
PDF
V tiger 5.4.0 图文安装教程
YUCHENG HU
 
PDF
Confluence 回顾(retrospectives) 蓝图 cwikiossez
YUCHENG HU
 
PDF
Confluence 会议记录(meeting notes)蓝图 cwikiossez
YUCHENG HU
 
PDF
VTIGER - 销售机会 - CWIKIOSSEZ
YUCHENG HU
 
PDF
Confluence 使用一个模板新建一个页面 cwikiossez
YUCHENG HU
 
PDF
Confluence 使用模板
YUCHENG HU
 
PDF
Cwikiossez confluence 订阅页面更新邮件通知
YUCHENG HU
 
PDF
Cwikiossez confluence 关注页面 博客页面和空间
YUCHENG HU
 
PDF
My sql università di enna a.a. 2005-06
YUCHENG HU
 
PDF
My sql would you like transactions
YUCHENG HU
 
PDF
MySQL 指南
YUCHENG HU
 
PDF
MySQL 简要介绍
YUCHENG HU
 
Confluencewiki 使用空间
YUCHENG HU
 
Presta shop 1.6 如何安装简体中文语言文件
YUCHENG HU
 
Logback 介绍
YUCHENG HU
 
Presta shop 1.6 详细安装指南
YUCHENG HU
 
Presta shop 1.6 的安装环境
YUCHENG HU
 
Presta shop 1.6 如何安装简体中文语言文件
YUCHENG HU
 
Presta shop 1.6 图文安装教程
YUCHENG HU
 
V tiger 5.4.0 图文安装教程
YUCHENG HU
 
Confluence 回顾(retrospectives) 蓝图 cwikiossez
YUCHENG HU
 
Confluence 会议记录(meeting notes)蓝图 cwikiossez
YUCHENG HU
 
VTIGER - 销售机会 - CWIKIOSSEZ
YUCHENG HU
 
Confluence 使用一个模板新建一个页面 cwikiossez
YUCHENG HU
 
Confluence 使用模板
YUCHENG HU
 
Cwikiossez confluence 订阅页面更新邮件通知
YUCHENG HU
 
Cwikiossez confluence 关注页面 博客页面和空间
YUCHENG HU
 
My sql università di enna a.a. 2005-06
YUCHENG HU
 
My sql would you like transactions
YUCHENG HU
 
MySQL 指南
YUCHENG HU
 
MySQL 简要介绍
YUCHENG HU
 
Ad

与 PHP 和 Perl 使用 MySQL 数据库

  • 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.  Close a connection <?php // it does nothing but closing // a mysql database connection mysql_close($conn); ?>
  • 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.  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.  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.  Close a Connection # free result set mysql_free_result($result); # close the connection mysql_close($connect); ?>
  • 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.  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 />";