Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mysqli使用fetch_assoc准备语句_Php_Sql_Mysqli - Fatal编程技术网

Php mysqli使用fetch_assoc准备语句

Php mysqli使用fetch_assoc准备语句,php,sql,mysqli,Php,Sql,Mysqli,我的目标是能够获取一个变量(使用php)并在一个准备好的语句中使用它(使用mysqli),然后获取_assoc。我有rtm,我还没有发现任何结合fetch_assoc和准备好的语句的东西,所以我不确定这是否可能。非常感谢您的帮助,这是我目前的代码 $where = $_GET['section']; $mysqli = mysqli_connect("localhost", "root", "","test"); if($stmt = mysql

我的目标是能够获取一个变量(使用php)并在一个准备好的语句中使用它(使用mysqli),然后获取_assoc。我有rtm,我还没有发现任何结合fetch_assoc和准备好的语句的东西,所以我不确定这是否可能。非常感谢您的帮助,这是我目前的代码

$where = $_GET['section'];
            $mysqli = mysqli_connect("localhost", "root", "","test");

            if($stmt = mysqli_prepare($mysqli,"SELECT title, img, active, price FROM ? ORDER by ID limit 5 ")){
                mysqli_stmt_bind_param($stmt, 's', $where);
                mysqli_stmt_execute($mysqli);
                mysqli_stmt_fetch($mysqli);
                 while($row = mysqli_fetch_assoc($stmt)){
                    if($row['active']=="yes"){
                        echo 'the rest of my stuff goes here';

从PHP网站页面(在最相关的部分添加重点):

注:

这些标记仅在SQL语句中的某些位置是合法的。对于 例如,在INSERT语句的VALUES()列表中允许它们 (为行指定列值),或与列进行比较 在WHERE子句中指定比较值

但是,标识符(如表或列)不允许使用它们 名称),位于选择列表中,该列表为要由 SELECT语句),或指定二进制运算符的两个操作数 例如=等号。后一种限制是必要的,因为 无法确定参数类型。一般来说 参数仅在数据操作语言(DML)中是合法的 语句,而不是数据定义语言(DDL)语句

假设您能够克服这个问题,那么您对
mysqli
的使用就有点混乱了。您可以正确地绑定参数并执行,但是您混淆了获得结果的两种不同方式。或者

  • 使用
    mysqli\u stmt\u get\u result
    获取结果集,然后在该结果集上使用
    mysqli\u fetch\u assoc
    ,或者
  • 使用
    mysqli\u stmt\u Bind\u result
    绑定结果,然后使用
    mysqli\u stmt\u fetch
    将下一组结果提取到绑定变量中。(通常您会使用
    while(mysqli_stmt_fetch($stmt)){//do stuff here}

  • 另一种风格,我们可以写在下面:

    $mysqli=new mysqli("host","user","pass","db");
    $stmt = $mysqli->prepare($query);
    $stmt->bind_param('s', $variable);
    $stmt->execute();
    $result = $stmt->get_result();
    while($row = $result->fetch_assoc()){
    ....
    }
    

    动态表选择通常表示设计不好。@Pinharry好的,我添加了一些关于使用准备语句的内容,因为您在示例代码中的使用似乎不太正确。