与php中包含撇号的名称匹配

与php中包含撇号的名称匹配,php,mysql,sql,magic-quotes-gpc,mysql-escape-string,Php,Mysql,Sql,Magic Quotes Gpc,Mysql Escape String,我有一个要匹配fname和lname的查询 $result = $mysqli->query('SELECT * FROM user WHERE userId = "'.$_SESSION["userId"].'" AND FriendFirstName = "'.htmlentities($firstName, ENT_QUOTES,"UTF-8").'" AND FriendLastName = "'.htmlentities($lastName, ENT_QUOTES,"UTF-8"

我有一个要匹配fname和lname的查询

$result = $mysqli->query('SELECT * FROM user WHERE userId = "'.$_SESSION["userId"].'" AND FriendFirstName = "'.htmlentities($firstName, ENT_QUOTES,"UTF-8").'" AND FriendLastName = "'.htmlentities($lastName, ENT_QUOTES,"UTF-8").'"   AND   FriendStatusCode="verified" AND friendId!='.$fid.' AND ViewableRow <> "0" ')  or die($mysqli->error);
echo 'SELECT * FROM user WHERE userId = "'.$_SESSION["userId"].'" AND FriendFirstName = "'.htmlentities($firstName, ENT_QUOTES,"UTF-8").'" AND FriendLastName = "'.htmlentities($lastName, ENT_QUOTES,"UTF-8").'"   AND   FriendStatusCode="verified" AND friendId!='.$fid.' AND ViewableRow <> "0" ';
它返回mysql中的行。我关掉了魔术语录,我认为这是一个非常简单的问题,但它浪费了我太多的时间

The FNAME is Aa'tid
The lname is Kenddy

我遗漏了什么吗?

因为我们在评论中讨论了如何更改为准备好的语句,下面是您可以做的事情(这是面向对象的方法,与旧的过程性方法不同):

//此代码将使用以下变量,您必须在运行查询之前在某处设置这些变量:
//$firstName
//$lastName
//$fid
//它还使用:
//$\会话[“用户ID”]
//连接到数据库(在下面填写数据库的值)
$mysqli=newmysqli('host','username','password','defaultdatabase');
//使用参数生成查询
$query=“从userId=?和FriendFirstName=?和FriendLastName=?以及FriendStatusCode='verified'和friendId!=?和ViewableRow'0'的用户中选择*;
//准备报表
如果($stmt=$mysqli->prepare($query)){
//绑定参数
$stmt->bind_参数(“issi”、$会话['userId']、$firstName、$lastName、$fid);
//执行语句
$stmt->execute();
//设置用于存储每行结果值的变量(在本例中,假设查询返回3列,`userId`、`firstName`、和`lastName`,这是我编的变量)
$stmt->bind_result($returnUserId、$returnFirstName、$returnLastName);
//循环遍历每一行
而($stmt->fetch()){
//输出循环通过的变量
printf(“%d:%s%s\n”、$returnUserId、$returnFirstName、$returnLastName);
}
//结束语
$stmt->close();
}
//密切联系
$mysqli->close();

本例不使用错误处理,但应该使用。还有很多其他方法可以处理结果集(例如关联数组),您可以查看要使用的文档。在这个例子中,我使用了
bind\u result
来循环行并实际分配变量,因为我相信它更干净,更容易跟踪代码量大的情况。

HTML转义和取消转义是必需的。@duffymo htmlentites用于相同的目的这是
htmlentities
根据文档()的预期行为。我会退一步考虑在MySQLi中使用准备好的语句(请参阅),而不是直接向查询中添加字符串并在PHP中转义它们。不管怎么说,这是一种更优雅、更安全的做法,而且它将在引号上匹配而不会出现复杂情况。@Anton我尝试过,如何在select、$stmt->bind_param('issi'、$_SESSION['userId']、$firstName、$lastName、$fid)的情况下从准备好的语句中获得结果$stmt->execute();这是唯一的解决方案吗?我不能不使用预先准备好的语句就这样做吗?你可以,但我不想建议你使用直接插入文本的变量来构建查询字符串;这是一种不好的做法,可能会带来严重的安全问题。
The FNAME is Aa'tid
The lname is Kenddy
// this code will use the following variables that you must set somewhere before running your query:
//     $firstName
//     $lastName
//     $fid
// it also uses:
//     $_SESSION["userId"]

// connect to the database (fill in values for your database below)
$mysqli = new mysqli('host','username','password','default database');

// build query with parameters
$query = "SELECT * FROM user WHERE userId = ? AND FriendFirstName = ? AND FriendLastName = ? AND FriendStatusCode='verified' AND friendId != ? AND ViewableRow <> '0'";

// prepare statement
if ($stmt = $mysqli->prepare($query)) {

    // bind parameters
    $stmt->bind_param("issi", $_SESSION['userId'], $firstName, $lastName, $fid);

    // execute statement
    $stmt->execute();

    // set the variables to use to store the values of the results for each row (I made the variables up, in this case, let's assume your query returns 3 columns, `userId`, `firstName`, and `lastName`)
    $stmt->bind_result($returnUserId, $returnFirstName, $returnLastName);

    // loop through each row
    while ($stmt->fetch()) {

        // output the variables being looped through
        printf ("%d: %s %s\n", $returnUserId, $returnFirstName, $returnLastName);

    }

    // close statement
    $stmt->close();

}

// close connection
$mysqli->close();