dongshang3309 2016-06-28 18:38
浏览 191

为什么这个SQL UPDATE QUERY返回false?

I'm making a game and I ran into some trouble with a query that won't seem to work what ever I try to do. I have four variables (that are validated)

$id, $plantId, $stage, $started

And my query looks like this...

$sql = "
        UPDATE growth
        SET plantId = '$plantId', stage = '$stage', started = '$started'
        WHERE id = '$id'
";

It returns false, but I tried the same query in phpMyAdmin and it worked! What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • doudu8291 2016-06-28 19:12
    关注

    Try not to use plain MySql. I know that learning new technologies might feel overwhelming but we live in year 2016. There are plenty of tools that will make your life easier.

    For example DiBi database layer: https://github.com/dg/dibi

    You can use either OFFICIAL VERSION as shown in the documentation:

    SELECT:

    dibi::query('SELECT * FROM users WHERE id = ?', $id);
    

    INSERT

    $arr = [
        'name' => 'John',
        'is_admin'  => TRUE,
    ];
    dibi::query('INSERT INTO users', $arr);
    

    UPDATE

    dibi::query('UPDATE users SET', $arr, 'WHERE `id`=?', $x);
    

    Or you can use FLUENT VERSION:

    SELECT

    $res = dibi::select('product_id')->as('id')
        ->select('title')
        ->from('products')
        ->innerJoin('orders')->using('(product_id)')
        ->orderBy('title')
        ->fetchAll();
    

    INSERT

    $record = array(
        'title'  => 'Výrobek',
        'price'  => 318,
        'active' => TRUE,
    );
    dibi::insert('products', $record)
        ->execute();
    

    UPDATE

    $record = array(
        'title'  => 'Výrobek',
        'price'  => 318,
        'active' => TRUE,
    );
    dibi::update('products', $record)
        ->where('product_id = %d', $id);
        ->execute();
    

    It will make your life much easier. Learning fluent version and concatenating your queries is even quite fun. When you need to test your query just use test() method.

    You do not even have to worry about sql injections.

    评论

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python