SlideShare a Scribd company logo
1 of 9
Download to read offline
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

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
 
[Php] navigations
[Php] navigations[Php] navigations
[Php] navigations
Thai Pham
 
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
 

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
 
Not Really PHP by the book
Not Really PHP by the bookNot Really PHP by the book
Not Really PHP by the book
 
Mysql DBI
Mysql DBIMysql DBI
Mysql DBI
 
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
 
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
 
Php 2
Php 2Php 2
Php 2
 
[Php] navigations
[Php] navigations[Php] navigations
[Php] navigations
 
Java script
Java scriptJava script
Java script
 
TerminalでTwitter
TerminalでTwitterTerminalでTwitter
TerminalでTwitter
 
Groovy as a scripting language
Groovy as a scripting languageGroovy as a scripting language
Groovy as a scripting language
 
[Php] navigations
[Php] navigations[Php] navigations
[Php] navigations
 
FormValidator::LazyWay で検証ルールをまとめよう
FormValidator::LazyWay で検証ルールをまとめようFormValidator::LazyWay で検証ルールをまとめよう
FormValidator::LazyWay で検証ルールをまとめよう
 
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
 
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
 
hotel managment
hotel managmenthotel managment
hotel managment
 
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.
 
Command-Oriented Architecture
Command-Oriented ArchitectureCommand-Oriented Architecture
Command-Oriented Architecture
 
API Design
API DesignAPI Design
API Design
 
Cycle.js: Functional and Reactive
Cycle.js: Functional and ReactiveCycle.js: Functional and Reactive
Cycle.js: Functional and Reactive
 
Coding for php with mysql
Coding for php with mysqlCoding for php with mysql
Coding for php with mysql
 

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

PHP DATABASE MANAGEMENT.pptx
PHP DATABASE MANAGEMENT.pptxPHP DATABASE MANAGEMENT.pptx
PHP DATABASE MANAGEMENT.pptx
CynthiaKendi1
 

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

The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistence
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Php.docx
Php.docxPhp.docx
Php.docx
 
Session8
Session8Session8
Session8
 
Php
PhpPhp
Php
 
veracruz
veracruzveracruz
veracruz
 
veracruz
veracruzveracruz
veracruz
 
veracruz
veracruzveracruz
veracruz
 
veracruz
veracruzveracruz
veracruz
 
Practical MySQL.pptx
Practical MySQL.pptxPractical MySQL.pptx
Practical MySQL.pptx
 
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
 
Php 101: PDO
Php 101: PDOPhp 101: PDO
Php 101: PDO
 
Pemrograman Web 8 - MySQL
Pemrograman Web 8 - MySQLPemrograman Web 8 - MySQL
Pemrograman Web 8 - MySQL
 
Php (1)
Php (1)Php (1)
Php (1)
 
Pagination in PHP
Pagination in PHPPagination in PHP
Pagination in PHP
 
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
 
Mysql & Php
Mysql & PhpMysql & Php
Mysql & Php
 
PHP DATABASE MANAGEMENT.pptx
PHP DATABASE MANAGEMENT.pptxPHP DATABASE MANAGEMENT.pptx
PHP DATABASE MANAGEMENT.pptx
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
 
Stored Procedure
Stored ProcedureStored Procedure
Stored Procedure
 

More from YUCHENG HU

Confluence 回顾(retrospectives) 蓝图 cwikiossez
Confluence 回顾(retrospectives) 蓝图   cwikiossezConfluence 回顾(retrospectives) 蓝图   cwikiossez
Confluence 回顾(retrospectives) 蓝图 cwikiossez
YUCHENG HU
 

More from YUCHENG HU (20)

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

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