简体   繁体   English

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

[英]matching with name containing apostrophe in php

I have a query in which I want to match fname and lname 我有一个查询要匹配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" ';

If I have a name John'y then it doesn't produce any result, it returns no rows, I echoed the query and If I run the same query I get result in my sql. 如果我有一个名字John'y,那么它不会产生任何结果,它不返回任何行,我会回显查询,如果我运行相同的查询,则会在sql中得到结果。

The output becomes like this 输出变成这样


SELECT * 
FROM user_friend_detail
WHERE userId = "9306" AND FriendFirstName = "Aa\'tid"
AND FriendLastName = "Kenddy" 
AND FriendStatusCode="verified" AND friendId!=9366 AND ViewableRow  "0"

and it returns row in mysql. 并在mysql中返回行。 I have magic quotes turned off, I assumed it to be a very simple problem but it wasted so much of my time. 我关闭了魔术引号,我认为这是一个非常简单的问题,但是却浪费了我很多时间。

The FNAME is Aa'tid
The lname is Kenddy

am I missing something? 我错过了什么吗?

Since we've discussed changing to prepared statements in the comments, here's what you can do (this is the object-oriented approach, separate from the older, procedural approach): 由于我们已经讨论了在注释中更改为准备好的语句,因此您可以执行以下操作(这是面向对象的方法,与较旧的过程方法分开):

// 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();

This example doesn't use error handling, but it should. 此示例不使用错误处理,但应该使用。 There are also plenty of other ways to work with result sets (eg associative arrays), which you can look at the docs to use. 还有很多其他方法可用于结果集(例如,关联数组),您可以查看要使用的文档。 In this example, I used bind_result to loop through rows and actually assign variables because I believe it to be cleaner and easier to keep track of when you have a lot of code. 在此示例中,我使用bind_result遍历行并实际分配变量,因为我认为这样可以更bind_result ,更轻松地跟踪何时有很多代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM