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 />";
Ad

More Related Content

What's hot (20)

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

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

The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistence
Hugo Hamon
 
Basics of Working with PHP and MySQL.pptx
Basics of Working with PHP and MySQL.pptxBasics of Working with PHP and MySQL.pptx
Basics of Working with PHP and MySQL.pptx
jensas21
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
Rifat Nabi
 
Php.docx
Php.docxPhp.docx
Php.docx
CHMuhammadZohaibZafa
 
Session8
Session8Session8
Session8
สอนทำโปรเจคจบ วิทคอมไอทีคอมธุรกิจ
 
Php
PhpPhp
Php
anilcoolz881234
 
chapter_Seven Database manipulation using php.pptx
chapter_Seven Database manipulation using php.pptxchapter_Seven Database manipulation using php.pptx
chapter_Seven Database manipulation using php.pptx
Getawu
 
veracruz
veracruzveracruz
veracruz
tutorialsruby
 
veracruz
veracruzveracruz
veracruz
tutorialsruby
 
veracruz
veracruzveracruz
veracruz
tutorialsruby
 
veracruz
veracruzveracruz
veracruz
tutorialsruby
 
Practical MySQL.pptx
Practical MySQL.pptxPractical MySQL.pptx
Practical MySQL.pptx
HussainUsman4
 
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
FYBSC IT Web Programming Unit V  Advanced PHP and MySQLFYBSC IT Web Programming Unit V  Advanced PHP and MySQL
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
Arti Parab Academics
 
Php 101: PDO
Php 101: PDOPhp 101: PDO
Php 101: PDO
Jeremy Kendall
 
Pemrograman Web 8 - MySQL
Pemrograman Web 8 - MySQLPemrograman Web 8 - MySQL
Pemrograman Web 8 - MySQL
Nur Fadli Utomo
 
Php (1)
Php (1)Php (1)
Php (1)
pinalsadiwala
 
Pagination in PHP
Pagination in PHPPagination in PHP
Pagination in PHP
Vineet Kumar Saini
 
Php 102: Out with the Bad, In with the Good
Php 102: Out with the Bad, In with the GoodPhp 102: Out with the Bad, In with the Good
Php 102: Out with the Bad, In with the Good
Jeremy Kendall
 
Mysql & Php
Mysql & PhpMysql & Php
Mysql & Php
Inbal Geffen
 
PHP DATABASE MANAGEMENT.pptx
PHP DATABASE MANAGEMENT.pptxPHP DATABASE MANAGEMENT.pptx
PHP DATABASE MANAGEMENT.pptx
CynthiaKendi1
 
Ad

More from YUCHENG HU (20)

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