简体   繁体   English

打印 mysqli SELECT 查询的结果

[英]Print the Result of a mysqli SELECT Query

I am trying to search my database for a result, and display all the columns relating to that result.我正在尝试在我的数据库中搜索结果,并显示与该结果相关的所有列。 My connection to the database works fine, but I don't know how to print the result of my query.我与数据库的连接工作正常,但我不知道如何打印查询结果。

I tried accessing it as an array, but it did not work.我尝试将它作为数组访问,但没有奏效。 What I want is to be able to search the database for the username 'TEST', and to return the password, email and username (to check the username exists).我想要的是能够在数据库中搜索用户名“TEST”,并返回密码、电子邮件和用户名(检查用户名是否存在)。

My current code is this我目前的代码是这样的

$user = mysqli_query($con,"SELECT * FROM user_list where username = '$username'");
print_r($user); 

$username is the username. $username是用户名。 It returns this它返回这个

(
    [current_field] => 0
    [field_count] => 4
    [lengths] => 
    [num_rows] => 2
    [type] => 0
)

Use Fetch to display the result使用 Fetch 显示结果

     $result = mysqli_query($con,"SELECT * FROM user_list where username = '" . mysqli_real_escape_string($con, $username) . "'"); 
    while($row = mysqli_fetch_array($result))
     {
        print_r($row);
     } 

You need to fetch the result first with mysqli_fetch_assoc您需要先使用 mysqli_fetch_assoc 获取结果

$userResult = mysqli_query($con,"SELECT * FROM user_list where username = '" . mysqli_real_escape_string($con, $username) . "'"); 
$user = mysqli_fetch_assoc($userResult);
print_r($user);
  • Avoid sql injections by escaping your strings.通过转义字符串来避免 sql 注入。

To get the result of the mysqli_query() you need to use one of the functions, which return the rows.要获得mysqli_query()的结果,您需要使用返回行的函数之一。 For examplefetch_all() to get all the rows or fetch_array() to get a single row.例如fetch_all()获取所有行或fetch_array()获取单行。

You can also loop on the object directly.您也可以直接在对象上循环。

$result = mysqli_query($con, 'SELECT ...');
foreach($result as $row) {
    print_r($row);
    // do something with each row
}

However, in your case you should not be using mysqli_query() at all!但是,在您的情况下,您根本不应该使用mysqli_query() This leaves you vulnerable to SQL injection.这使您容易受到 SQL 注入的影响。 You must use parameter binding, which is available with prepared statements.您必须使用参数绑定,这可用于准备好的语句。

For example your fixed query would look like this:例如,您的固定查询如下所示:

$stmt = $con->prepare('SELECT * FROM user_list where username = ?');
$stmt->bind_param('s', $username);
$stmt->execute();
$result = $stmt->get_result();
foreach ($result as $row) {
    print_r($row);
}

The difference is that my variable is not separate from the SQL, so there is no risk of injection.不同的是我的变量没有和SQL分开,所以不存在注入的风险。 You should never allow any variable input directly in SQL query.您永远不应该在 SQL 查询中直接允许任何变量输入。 Doing this properly is really not that difficult.正确地做到这一点真的没有那么困难。

You have to fetch your result:您必须获取结果:

$user = mysqli_query($con,"SELECT * FROM user_list where username = '$username'");

while ($row = $user->fetch_row()) {
        print_r($row);
    }
while($row = mysql_fetch_array($user, MYSQL_ASSOC))
{
    echo "MATE :{$row['MATE']}  <br> ".
         "TEST : {$row['TEST']} <br> ".
         "--------------------------------<br>";
}
it's may work your you.

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

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