mysql和mysqli不可以混淆

来源:https://zhidao.baidu.com/question/553389929884782172.htmlhttps://zhidao.baidu.com/question/1045880669860075819.html

mysql是mysql
mysqli是mysqli
不可混淆啊!

如mysql连接
那么, mysql_query( sql, object)
sql为sql命令语句, object为数据库连接对象, 第二个参数可省略,默认为上次打开的数据库连接对象

如mysqli连接
mysqli_query( object, sql )
第一个参数为数据库连接对象,这个参数必须,第二个参数为sql命令语句,也是必须

如果是mysqli连接
则对数据库的操作函数都必须提供数据库连接对象参数

比如 mysqli_error( $con )

但mysql连接就可以省略!

例子:

怎么用php把html表单内容写入数据库

这是用的mysql

1:首先要使用PHP的超全局变量 $_GET 和 $_POST 用于收集表单数据(form-data)

2:然后使用INSERT INTO 语句用于向数据库表中插入新记录。

具体示例:

(1)首先创建了一个名为 "Persons" 的表,有三个列:"Firstname", "Lastname" 以及 "Age"。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?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')");

mysql_close($con);

?>

(2)其次创建一个 HTML 表单,这个表单可把新记录插入 "Persons" 表。

1

2

3

4

5

6

7

8

9

10

11

12

<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>

(3)接着当用户点击上例中 HTML 表单中的提交按钮时,表单数据被发送到 "insert.php"。"insert.php" 文件连接数据库,并通过 $_POST 变量从表单取回值。然后,mysql_query() 函数执行 INSERT INTO 语句,一条新的记录会添加到数据库表中。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<?php

$con mysql_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";

mysql_close($con)

?>

这是用的mysqli

html界面

<form action="xiexin2.php" method="post">
<table width="100%" border="1">
  <tr>
    <td colspan="2"><p>
       <input type="submit" name="send" id="send" value="发送">
       <input type="submit" name="save" id="save" value="存草稿">
       <input type="submit" name="quxiao" id="quxiao" value="取消">
  </p></td>
    </tr>
  <tr>
    <td width="7%"><label for="textfield">发件人</label></td>
    <td width="93%">
      <input type="text" name="textSend" id="textSend"></td>
  </tr>
  <tr>
    <td><label for="textfield2">收件人</label></td>
    <td>
      <input type="text" name="textRecipient" id="textRecipient"></td>
  </tr>
  <tr>
    <td><label for="textfield3">主题</label></td>
    <td>
      <input type="text" name="textTheme" id="textTheme"></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

     &nbsp;&nbsp;
     
     <p>&nbsp;</p>
     
</form>

PHP界面

<?php
//连接数据库,以下用的都是mysqli,不是mysql
$link = mysqli_connect('localhost','root','123','db_mail');
mysqli_set_charset($link,'utf8');
//判断数据库是否连成功接
if(!$link)
{
	//数据库没有链接成功
	die('Could not connect:' . mysqli_error());
}
//数据库连接成功

//插入记录
$sql="INSERT INTO mail (fjr, sjr, zt)
VALUES
('$_POST[textSend]','$_POST[textRecipient]','$_POST[textTheme]')";
 
if (!mysqli_query($link,$sql))
  {
  die('Error: ' . mysqli_error());
  }
echo "成功添加一条记录";
 
mysqli_close($link);
?>

猜你喜欢

转载自blog.csdn.net/qq_42058441/article/details/84238980