dongleman4760 2018-10-21 16:54
浏览 69
已采纳

返回False的PHP SQL Execute语句[重复]

This question already has an answer here:

I am trying to run a basic SQL statement, but I can't figure out what I am doing wrong. Here is the context:
I have a database named: my_database
a table named: users
Two columns: service_id which type is BIGINT
and video_id which type is varchar(20)

Here is my code:

$video_id = "chartext";
$service_id = 12345678910;
$bdd = new PDO("mysql:host=127.0.0.1;dbname=moods_db", "user", "pass");
$query = $bdd->prepare("INSERT INTO users(video_id) VALUES (?) WHERE service_id = $service_id");
$query->execute([$service_id]);
var_dump($req->execute([$video_id]));
// which gives me false

Some info: My PHP_INT_MAX gives me 9223372036854775807

I am aware of SQL Injection and just removed it to be clearer

I var_dumped each steps and the execute one is the only one that is not working

I ran the statement in phpmyadmin console and it told me: "Syntax error near 'WHERE (service_id = '12345678910')' line 1

I also searched the usage of WHERE Clause but I did not understand if I could put it in an INSERT statement, I think I already did that and it worked, I am lost

Thanks in advance

</div>
  • 写回答

4条回答 默认 最新

  • duanbeng6709 2018-10-21 17:05
    关注

    PDOStatement::execute — Executes a prepared statement. It returns TRUE on success or FALSE on failure.

    So for your case it is returning FALSE because In an INSERT statement you wouldn't have an existing row to do a WHERE clause.

    You need to try like this way to insert one row with two values or use UPDATE query to update one row with one value based on another value on the existing row. Hope it clears your doubts now :)

    $video_id = "chartext";
    $service_id = 12345678910;
    $bdd = new PDO("mysql:host=127.0.0.1;dbname=moods_db", "user", "pass");
    $sql = "INSERT INTO users(video_id, service_id) VALUES (?,?)";
    $array = array($video_id , $service_id );
    $sth = $bdd->prepare($sql);
    $sth->execute($array);
    var_dump($sth);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加