Having trouble executing a SELECT query in a prepared statement

后端 未结 3 1089
甜味超标
甜味超标 2020-12-14 12:26

Ive followed a bunch of different examples regarding using a SELECT in a prepared statement, but nothing is returned. EDIT I have changed my code a bit to look lik

3条回答
  •  误落风尘
    2020-12-14 12:51

    I think you have to bind to the columns in bind_results() like

    /* prepare statement */
    if ($stmt = $mysqli->prepare("SELECT Code, Name FROM Country ORDER BY Name LIMIT 5")) {
    $stmt->execute();
    
    /* bind variables to prepared statement */
    $stmt->bind_result($col1, $col2);
    
    /* fetch values */
    while ($stmt->fetch()) {
        printf("%s %s\n", $col1, $col2);
    }
    

    Here $col1 and $col2 binds to Code and Name columns of Country table

    (Instead of * in SELECT use the column names)

    Further reference : http://php.net/manual/en/mysqli-stmt.bind-result.php

提交回复
热议问题