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 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题