douyi4205 2015-06-09 15:46
浏览 32
已采纳

是否可以在预准备语句中使用变量而不是文字? 如果是这样,怎么样?

What I mean is, my current prepared statement starts like this:

$stmt = $bd->prepare("SELECT Beer_Name FROM Beer WHERE Gluten = ?");

But is is possible to do it like this(below)? Because I've tried several ways and none have been successful.

$stmt = $bd->prepare("'.$sql.'");

I ask because I'm trying to create a function, as I am coding a website that uses a lot of queries.

<?php

include('connection.php');


    global $sql;
    global $ready;


        $sql = 'SELECT Beer_Name FROM Beer WHERE Gluten = ?';
        $ready = "Yes";


function bindingHelper($ready, $sql) {

            $stmt = $bd->prepare($sql);
            $stmt->bind_param("s", $ready);
            $stmt->execute();
            $stmt->store_result();
            $stmt->bind_result($beer_name);
            $stmt->fetch();

            echo($beer_name);
}

bindingHelper($ready, $sql);

?>
  • 写回答

2条回答 默认 最新

  • du94414 2015-06-09 16:19
    关注

    I didn't notice that actually, and yes I have a fair idea. We all make mistakes. I moved include('connection.php'); inside the function and now it works perfectly. Please add this as an answer and I'll accept. – user3822332

    As per OP's request.

    what you have here is a variable scope issue.

    therefore, your variables aren't being accessed from the function.

    • Amidst the comments under their question to establish the solution.

    Error checking links references:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题