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 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?