mysqli转义字符串附加参数

时间:2012-11-27 17:40:01

标签: php

为什么mysqli_real_escape_string会询问其他参数?在程序方法中,我们还必须包括连接建立。这背后的逻辑是什么?

2 个答案:

答案 0 :(得分:2)

您正在提供数据库连接链接。这样你就可以同时拥有多个MySQL连接。

答案 1 :(得分:0)

在某些情况下,您可能希望打开多个连接并连接到多个MySQL数据库。明确声明要使用的连接链接标识符允许您查询多个MySQL数据库而不必担心冲突。

//Open connections to two separate databases. For example: DatabaseOne and DatabaseTwo
$databaseOne = mysqli_connect("example.com", "user", "password", "DatabaseOne");
$databaseTwo = mysqli_connect("example.com", "user", "password", "DatabaseTwo");

//Make variable clean for first connection to DatabaseOne
$var= mysqli_real_escape_string($databaseOne, $var);

//QUERY TABLE FROM DATABASE ONE
$res1 = mysqli_query($databaseOne, "SELECT col FROM tableInDatabaseOne WHERE col = '$var'");
//do something with $res1

//QUERY TABLE FROM DATABASE TWO
$res2 = mysqli_query($databaseTwo, "SELECT col FROM tableInDatabaseTwo");
//do something with $res2