duanshan3427 2015-03-11 10:06
浏览 69
已采纳

PHP mysqli INSERT无法正常工作

Argh I can't get my head round this. I have a connection to a mySQL database:

function db_connect() {

  static $connection;

  if(!isset($connection)) {
    $config = parse_ini_file($_SERVER['DOCUMENT_ROOT'] . "/cfi/config.ini");
    $connection = mysqli_connect('localhost',$config['username'],$config['password'],$config['dbname']);    
  }


  if($connection === false) {
    return mysqli_connect_error();
  }

  return $connection;

}

I run it like so:

$connection = db_connect();

I then try and run an INSERT like so:

if (mysqli_query($connection, "INSERT INTO pending ('aw_id','in_stock') VALUES ('1','yes')")) {
  echo "success";
} else {
  echo "fail";
}

But it always fails... what am I missing?

I'm not sure if it helps but a var_dump of $connection is:

object(mysqli)#1 (19) { ["affected_rows"]=> int(0) ["client_info"]=> string(79) "mysqlnd 5.0.11-dev - 20120503 - $Id: bf9ad53b11c9a57efdb1057292d73b928b8c5c77 $" ["client_version"]=> int(50011) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["field_count"]=> int(0) ["host_info"]=> string(25) "Localhost via UNIX socket" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(6) "5.6.23" ["server_version"]=> int(50623) ["stat"]=> string(147) "Uptime: 684382 Threads: 3 Questions: 5532897 Slow queries: 14 Opens: 287168 Flush tables: 37 Open tables: 1024 Queries per second avg: 8.084" ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(184926) ["warning_count"]=> int(0) } 

Grrrr!!

  • 写回答

1条回答 默认 最新

  • doumiyi7063 2015-03-11 10:10
    关注
    INSERT INTO pending ('aw_id','in_stock') VALUES ('1','yes')
    

    replace with

    INSERT INTO pending (aw_id,in_stock) VALUES ('1','yes')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?