开发者社区> 问答> 正文

如何在PHP中使用mysqli准备的语句??mysql

我正在尝试准备好的语句,但是下面的代码不起作用。我收到错误消息:

致命错误:在第12行的/var/www/prepared.php中的非对象上调用成员函数execute()

connect_errno) { echo "Failed to connect to MySQL: " . $mysqli->connect_error; } $stmt = $mysqli->prepare("INSERT INTO users (name, age) VALUES (?,?)"); // insert one row $stmt->execute(array('one',1)); // insert another row with different values $stmt->execute(array('two',1)); ?> 另外,我需要对准备好的语句使用mysqli吗?谁能指出我关于从连接到插入再到带有错误处理的选择的准备好的语句的完整示例?

展开
收起
保持可爱mmm 2020-05-13 14:36:06 475 0
1 条回答
写回答
取消 提交回答
  • 从mysqli::prepare文档:

    在执行语句或获取行之前,必须使用mysqli_stmt_bind_param()和/或mysqli_stmt_bind_result()将参数标记绑定到应用程序变量。

    bind_paramdocs。

    即:

    $name = 'one'; $age = 1;

    $stmt = $mysqli->prepare("INSERT INTO users (name, age) VALUES (?,?)");

    // bind parameters. I'm guessing 'string' & 'integer', but read documentation. $stmt->bind_param('si', $name, $age);

    // now we can execute $stmt->execute();来源:stack overflow

    2020-05-13 14:36:18
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
搭建电商项目架构连接MySQL 立即下载
搭建4层电商项目架构,实战连接MySQL 立即下载
PolarDB MySQL引擎重磅功能及产品能力盛大发布 立即下载

相关镜像