doupafu6980 2015-06-24 09:59
浏览 46
已采纳

查询绑定在CodeIgniter 3中不起作用

I have a pretty simple query that's failing in CI:

$sql = "INSERT INTO tenant (name, image, url) VALUES (?, ?, ?)";
$this->db->query($sql, $name, $image, $url);

When I try to execute this query, I end up with the following error:

A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?, ?)' at line 1

INSERT INTO tenant (name, image, url) VALUES (?, ?, ?)

Filename: models/Tenant.php

Line Number: 107

All of the variables used are defined properly, and I can get it to work no problem by converting it to this:

$this->db->query("INSERT INTO tenant (name, image, url) VALUES ('$name', '$image', '$url')");

There's nothing special about any of the variables - they're all just strings. What needs to happen for query binding to work here?

  • 写回答

2条回答 默认 最新

  • dongzong5017 2015-06-24 10:02
    关注

    Take a look at documentation about query binding. query takes two arguments. Your bindings should be in one array.

    $sql = "INSERT INTO tenant (name, image, url) VALUES (?, ?, ?)";
    $this->db->query($sql, array($name, $image, $url));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?