doudinghan8319 2015-07-18 09:25
浏览 23
已采纳

PHP准备语句错误:无法准备选择

I wanted to select data from MYSQL database. For that I did the following:

$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);

mysqli_set_charset($mysqli, "utf8");

date_default_timezone_set("Europe/Berlin");

session_name('User Session'); 
session_start(); 

$private_id = session_id(); 
$private_questions = get_questions($mysqli);

session_write_close(); 

    function get_questions($mysqli, $stmt_get_questions) { 
    $stmt = $mysqli->query($stmt_get_questions);

    $questions = array();

    while($question = $stmt->fetch_assoc()) {
        $questions[] = $question;   
    }

    $stmt->close();
    $mysqli->close();

    return $questions;
}

And call my variable in HTML:

<div class="container">

    <p>Private ID is <?=$private_id?></p>
    <p>Questions <?=$private_questions?></p>
</div>

But I get an internal server error:

GET mywebsite/myhtml.html 500 (Internal Server Error)

I can't find the problem. The MYSQL Select is correct.

  • 写回答

1条回答 默认 最新

  • douqian6315 2015-07-18 09:35
    关注

    You got confused in the concept.

    You can run that SELECT statement like this:

    $stmt_select = "SELECT A, B, C FROM MY_TABLE";
    $stmt = $mysqli->query($stmt_select);
    

    Or if you want to have protection against MYSQL injection hacks then you can use prepared function like this: (Pay attention that in a prepared statement you must have question marks "?" and then use bind_param() function)

     if ($stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?")) {
    
    /* bind parameters for markers */
    $stmt->bind_param("s", $city);
    
    /* execute query */
    $stmt->execute();
    
    /* bind result variables */
    $stmt->bind_result($district);
    
    /* fetch value */
    $stmt->fetch();
    
    printf("%s is in district %s
    ", $city, $district);
    
    /* close statement */
    $stmt->close();
    }
    

    Read this link for more clarification:

    Mysql prepared statement

    The full answer goes as follow:

     $mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
    
     $mysqli_set_charset($mysqli, "utf8");
    
     date_default_timezone_set("Europe/Berlin");
    
     $stmt_get_questions = "SELECT A, B, C FROM MY_TABLE";
    
     session_name('User Session'); 
     session_start(); 
    
     $private_id = session_id(); 
     $private_questions = get_questions($mysqli);
    
     session_write_close(); 
    
     function get_questions($mysqli) { 
     // Execute the MYSQL statement
     $stmt = $mysqli->query($stmt_get_questions);
    
     // Get the result and iterate through it
     while($row = $stmt->fetch_assoc()) {
         // Do Something with the each row, Like reading a column:
         $column_one = $row['column_one'];  
     }
    
     $stmt->close();
     $mysqli->close();
    
     return $questions;
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的