Netbeans Xdebug:mysqli_affected_rows返回"-1".当它应该为"1"时 [英] Netbeans Xdebug: mysqli_affected_rows returns "-1" when it should be "1"

查看:114
本文介绍了Netbeans Xdebug:mysqli_affected_rows返回"-1".当它应该为"1"时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感到困惑的是,为什么以下代码在signup.php中在mysqli_affected_rows($dbc)返回"-1"(因此出错)的同时向数据库表中成功添加了新行:

I'm puzzled why the following code successfully adds a new row to my database table while mysqli_affected_rows($dbc) returns "-1", thus an error, in signup.php:

dbc.inc.php:

dbc.inc.php :

DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', '');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'v');


$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('Error connecting to MySQL server.');

mysqli_set_charset($dbc, 'utf8');

signup.php:

signup.php :

require('dbc.inc.php');

//code to set variables for the following SQL statement

$q = "INSERT INTO users (username, email, pass, first_name, last_name, sex, birth_day, birth_month, birth_year, phone, street, street_nr, city, zip_code, country, user_img) VALUES ('$u', '$e', '$p', '$fn', '$ln', '$sex', '$bd', '$bm', '$by', '$pn', '$st', '$sn', '$cit', '$pc', '$ct', '$user_img')";
$r = mysqli_query($dbc, $q) or die(msg(0, "Error connecting to the database"));

if (mysqli_affected_rows($dbc) === 1) { //Returns 'false' despite one row having been added...
echo "Success";
}
else {
echo "Error"; //...resulting in "Error" being echoed
}

出于测试目的:使用SQL创建表用户":

For testing purposes: SQL to create table "users":

CREATE TABLE IF NOT EXISTS `v`.`users` (
  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `type` ENUM('member','admin') NOT NULL DEFAULT 'member',
  `username` VARCHAR(45) NOT NULL,
  `email` VARCHAR(80) NOT NULL,
  `pass` VARCHAR(255) NOT NULL,
  `first_name` VARCHAR(45) NOT NULL,
  `last_name` VARCHAR(45) NOT NULL,
  `sex` CHAR(1) NOT NULL,
  `birth_day` INT NOT NULL,
  `birth_month` INT NULL,
  `birth_year` INT NULL,
  `phone` VARCHAR(20) NULL,
  `street` VARCHAR(60) NOT NULL,
  `street_nr` VARCHAR(9) NOT NULL,
  `city` VARCHAR(45) NOT NULL,
  `zip_code` VARCHAR(45) NOT NULL,
  `country` VARCHAR(45) NOT NULL,
  `user_img` VARCHAR(65) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `username_UNIQUE` (`username` ASC),
  UNIQUE INDEX `email_UNIQUE` (`email` ASC),
  INDEX `login` (`email` ASC, `pass` ASC))
ENGINE = InnoDB
AUTO_INCREMENT = 4
DEFAULT CHARACTER SET = utf8;

非常感谢您的提示!

同时,我能够进一步隔离该问题:该代码可以在Web浏览器中直接正常运行,只是在Netbeans/Xdebug中,相应行中的mysqli_affected_rows($dbc)首先正确返回"1",但在进入下一行(F7)突然变为"-1",因此错误地跳转到"else"分支,尽管数据已正确写入数据库,但返回错误. 显然,我不是唯一遇到此问题的人.

I have meanwhile been able to isolate the problem a little further: the code works fine run directly in a web browser, it's just in Netbeans/Xdebug that mysqli_affected_rows($dbc) in the according line first correctly returns "1", but after stepping into the following line (F7) suddenly changes to "-1" and thus wrongly jumps to the "else"-branch returning an error although the data is correctly written to the database. Apparently I'm not the only one having this problem.

这些是我在php.ini中的Xdebug设置,但我认为它们是正确的.

These are my Xdebug Settings in php.ini, but I think they are correct.

zend_extension = "C:\xampp\php\ext\php_xdebug.dll"
;xdebug.profiler_append = 0
xdebug.profiler_enable = 1
;xdebug.profiler_enable_trigger = 0
;xdebug.profiler_output_dir = "C:\xampp\tmp"
;xdebug.profiler_output_name = "cachegrind.out.%t-%s"
xdebug.remote_enable = 1
xdebug.remote_handler = "dbgp"
xdebug.remote_host = "localhost"
xdebug.remote_port = 9000
;xdebug.trace_output_dir = "C:\xampp\tmp"

有任何线索吗?

推荐答案

我已经测试了您的代码,并将新记录插入数据库时​​回显成功,请重新检查数据库以确保数据插入

I have tested your code and it echos Success when new record inserted to database, please recheck your database to ensure data insertion

mysqli_affected_rows:返回值

mysqli_affected_rows: return value

    - An integer > 0 indicates the number of rows affected. 
    - 0 indicates that no records where affected &
    -1 indicates that the query returned an error(that may be you case)

这篇关于Netbeans Xdebug:mysqli_affected_rows返回"-1".当它应该为"1"时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆