duanshan3427
2015-03-11 10:06 阅读 55
已采纳

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 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')
    
    点赞 评论 复制链接分享

相关推荐