连接MYSQL
`
面向对象风格
<?php
$mysqli = new mysqli ( 'localhost' , 'my_user' , 'my_password' , 'my_db' );
/*
* This is the "official" OO way to do it,
* BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
*/
if ( $mysqli -> connect_error ) {
die( 'Connect Error (' . $mysqli -> connect_errno . ') '
. $mysqli -> connect_error );
}
/*
* Use this instead of $connect_error if you need to ensure
* compatibility with PHP versions prior to 5.2.9 and 5.3.0.
*/
if ( mysqli_connect_error ()) {
die( 'Connect Error (' . mysqli_connect_errno () . ') '
. mysqli_connect_error ());
}
echo 'Success... ' . $mysqli -> host_info . "\n" ;
$mysqli -> close ();
?>
面向对象风格 when extending mysqli class
<?php
class foo_mysqli extends mysqli {
public function __construct ( $host , $user , $pass , $db ) {
parent :: __construct ( $host , $user , $pass , $db );
if ( mysqli_connect_error ()) {
die( 'Connect Error (' . mysqli_connect_errno () . ') '
. mysqli_connect_error ());
}
}
}
$db = new foo_mysqli ( 'localhost' , 'my_user' , 'my_password' , 'my_db' );
echo 'Success... ' . $db -> host_info . "\n" ;
$db -> close ();
?>
过程化风格
<?php
$link = mysqli_connect ( 'localhost' , 'my_user' , 'my_password' , 'my_db' ); //连接数据库
if (! $link ) {
die( 'Connect Error (' . mysqli_connect_errno () . ') '
. mysqli_connect_error ());
}
echo 'Success... ' . mysqli_get_host_info ( $link ) . "\n" ;
mysqli_close ( $link ); //关闭数据库连接
?>
执行SQL语句
使用 mysql_query() 函数。此函数用于向 MySQL 连接发送查询或命令
例子
在下面的例子中,我们创建了一个名为 "my_db" 的数据库:
<?php
$con = mysqli_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
mysqli_close($con);
?>
向数据库表插入数据
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con); //选择数据库
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin', '35')");
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Glenn', 'Quagmire', '33')");
mysqli_close($con);
?>
把来自表单的数据插入数据库
现在,我们创建一个 HTML 表单,这个表单可把新记录插入 "Persons" 表。
这是这个 HTML 表单:
<html>
<body>
<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
当用户点击上例中 HTML 表单中的提交按钮时,表单数据被发送到 "insert.php"。"insert.php" 文件连接数据库,并通过 $_POST 变量从表单取回值。然后,mysql_query() 函数执行 INSERT INTO 语句,一条新的记录会添加到数据库表中。
下面是 "insert.php" 页面的代码:
<?php
$con = mysqli_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con); //选择数据库
$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysqli_close($con)
?>
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。