简体   繁体   English

mysqli使用select *和一个准备好的语句和查询

[英]mysqli using select * with a prepared statement and query

I have exhausted looked and looked to find my exact situation. 我已经筋疲力尽地看了看,并期待找到我的确切情况。 I want to understand why this isn't working, also I want to make sure this logic is safer from injection hacks. 我想了解为什么这不起作用,我也想确保这种逻辑从注入黑客中更安全 I know nothing is 100% safe. 我知道没有什么是100%安全的。 The following code does not work: 以下代码不起作用:

$query= mysqli_prepare($con,"SELECT * FROM *table*
    WHERE Resource = ? AND WorkDate >= ? AND WorkDate <= ? ORDER BY WorkDate, StartTime" );


mysqli_stmt_bind_param($query, "sss", $resource, $from, $to);
mysqli_execute($query);

if (!mysqli_stmt_execute($query))
{
    die('Error: ' . mysqli_error());
}
mysqli_stmt_store_result($query);

mysqli_stmt_fetch($query);

$result= mysqli_query($con,$query, MYSQLI_STORE_RESULT); // or die("Could not get results: ".mysqli_error()); 
while($rows=mysqli_fetch_array($result)){<fill in table>

This code dies in the line $result. 此代码在行$ result中消失。 I've done var_dumps on query, and all variables. 我在查询和所有变量上都做过var_dumps。 When I var_dump query it tells me affected rows correctly. 当我查询var_dump时,它会正确地告诉我受影响的行。 So to me that means the prepared statement is working. 所以对我来说这意味着准备好的声明正在发挥作用 But when I try to run my query so I can fetch it to output data on my screen. 但是,当我尝试运行我的查询,以便我可以获取它以在我的屏幕上输出数据。

Now this works with mysql, but I'm trying to convert it to mysqli to avoid injection. 现在这适用于mysql,但我正在尝试将其转换为mysqli以避免注入。 Originally had the whole sql statement in place, but now using the prepared statement to avoid that. 最初有整个sql语句到位,但现在使用准备好的语句来避免这种情况。

You need to understand the difference between a prepared statement, and regular querying. 您需要了解预准备语句和常规查询之间的区别。 Once you start off with a prepared statement, you must run that way until the end (unless you store the results though mysqli_stmt::get_result() , then you can use mysqli::fetch_assoc() and similar functions -- that is not covered in this answer, see the manual for examples). 一旦你开始准备一个语句,你必须以这种方式运行直到结束(除非你通过mysqli_stmt::get_result()存储结果,然后你可以使用mysqli::fetch_assoc()和类似的函数 - 这是没有涵盖的在这个答案中,请参阅手册中的示例)。

Given that you have *table* in your code, I assume that's incorrect. 鉴于你的代码中有*table* ,我认为这是不正确的。 Please change the first two lines of the query below (the columns you select and the table you select them form) accordingly. 请相应地更改下面查询的前两行(您选择的列和您选择它们​​的表格)。

It's important that the number of variables given to bind_result() is an exact match with the number of columns you select in the query. bind_result()提供的变量数与您在查询中选择的列数完全匹配非常重要。 These variables will hold the value for the column for each iteration. 这些变量将保存每次迭代的列值。

Here's a starting-point to guide you in the right direction. 这是一个指导您正确方向的起点。 Change the names of column1 through column3 accordingly (both in the querystring ( prepare() ) and in the binding of the results bind_result() ). 相应地更改column1column3的名称(在querystring( prepare() )和结果bind_result()的绑定中。 As mentioned before, these are a one-to-one match. 如前所述,这些是一对一的匹配。 You must also change the name of your table accordingly, myTableName is currently just a placeholder (as is column1 through column3 ). 您还必须相应地更改表的名称, myTableName当前只是一个占位符(如column1column3 )。

// Prepare the query
$stmt = $con->prepare("SELECT column1, column2, column3 
                       FROM myTableName
                       WHERE Resource = ? 
                         AND WorkDate >= ? 
                         AND WorkDate <= ? 
                       ORDER BY WorkDate, StartTime");
if (!$stmt) {
    // Check for errors, if the prepare failed, it will return 'false'
    echo "An unexpected error occurred, check the logs.";
    error_log($con->error);
    exit;
}

// Bind the parameters (?)  in the query and execute it
$stmt->bind_param("sss", $resource, $from, $to);
if (!$stmt->execute()) {
    echo "An unexpected error occurred, check the logs.";
    error_log($stmt->error);
    $stmt->close();
    exit;
}

// Bind the results of each column into a variable
$stmt->bind_result($column1, $column2, $column3);

// In this loop we use the variables that we bound in the function bind_result above
// In this example, we simply print their values
while ($stmt->fetch()) {
    echo "$column1 -- $column2 -- $column3";
}

// Close the statement after use!
$stmt->close();

The manual is also a good place to read up on examples 手册也是阅读示例的好地方

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

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