mysql\u受影响的\u行有时返回0而不是1

mysql\u受影响的\u行有时返回0而不是1,mysql,Mysql,我对php脚本有一个奇怪的问题——mysql\u impacted\u rows()有时会无缘无故地返回“0” stackoverflow也有一个类似的问题,对此问题的答案是: MySQL只在更新前后有明显差异的情况下才实际更新一行 但这不是我的情况。例如,如果更新前的值为1320402744,更新后的值为1320402944mysql\u受影响的行()无论如何返回“0”。这种差异还不够明显吗 下面是3个文件。如您所见,所有文件都包括文件“functions.inc.php”,该文件调用函数“o

我对php脚本有一个奇怪的问题——mysql\u impacted\u rows()有时会无缘无故地返回“0”

stackoverflow也有一个类似的问题,对此问题的答案是:

MySQL只在更新前后有明显差异的情况下才实际更新一行

但这不是我的情况。例如,如果更新前的值为1320402744,更新后的值为1320402944mysql\u受影响的行()无论如何返回“0”。这种差异还不够明显吗

下面是3个文件。如您所见,所有文件都包括文件“functions.inc.php”,该文件调用函数“online()”

文件“login.php”工作正常。它在“会话”表中正确插入新行

文件“content.php”工作正常-它显示内容并正确运行“functions.inc.php”中的函数“online()

然后我调用文件“test.php”。它正确地删除了“sometable中的某些内容”。然后它刷新自己(Header(“Location:/test.php”);刷新后,我将注销

我将此添加到“online()”函数中:

echo "affected_rows";
$checkuser = mysql_query("SELECT userid FROM session WHERE userid = '" . $_SESSION['id'] . "'") or die('Error');
$found = mysql_num_rows($checkuser);

echo $found;

 $result = mysql_query("UPDATE session SET time='$ctime' WHERE userid='".$_SESSION['id']."'") or die('Error');
    $affected_rows = mysql_affected_rows();
    if ($affected_rows != 1) @session_destroy();

echo $affected_rows;
它返回0

我在“online()函数”中添加了更多代码:

echo "affected_rows";
$checkuser = mysql_query("SELECT userid FROM session WHERE userid = '" . $_SESSION['id'] . "'") or die('Error');
$found = mysql_num_rows($checkuser);

echo $found;

 $result = mysql_query("UPDATE session SET time='$ctime' WHERE userid='".$_SESSION['id']."'") or die('Error');
    $affected_rows = mysql_affected_rows();
    if ($affected_rows != 1) @session_destroy();

echo $affected_rows;
结果是10

我检查了数据库。会话表中的“时间”字段已更新

因此,我无法理解该行怎么可能存在,它更新正确,但mysql_影响了_行();返回0,以及只有在刷新同一页面时才会发生这种情况的原因

functions.inc.php

<?php
@ob_start();@session_start();
@mysql_connect(C_HOST, C_USER, C_PASS) or die('Cant connect');
@mysql_select_db(C_BASE) or die('Cant select DB');

function online() {
$ctime = time()+1800;

if((isset($_SESSION['id']))&&(is_numeric($_SESSION['id']))) {

$query = mysql_query("UPDATE session SET time='$ctime2' WHERE userid='".$_SESSION['id']."'") or die('Error');
$affected_rows = mysql_affected_rows();
if ($affected_rows != 1) @session_destroy();
}
}

//many other functions go here

online();
?>
<?php
include_once 'configuration.inc.php';
include_once 'functions.inc.php';

//many things go here

$upd = mysql_query("INSERT INTO session VALUES ('" . $i['id'] . "','$ctime')") or die('Error2');
Header("Location: /content.php?justlogged=1");
die;
?>
<?php
include_once 'configuration.inc.php';
include_once 'functions.inc.php';
//many thing go here
echo "content";

?>
    <?php
    include_once 'configuration.inc.php';
    include_once 'functions.inc.php';

    if (isset($_GET['tid'])&&(is_numeric($_GET['tid']))){
    $result = mysql_query("delete from some_table where something = '" . $_GET['tid'] . "'") or die('Error123a');
Header("Location: /test.php");
    die;
    }

    //file content

    ?>

login.php

<?php
@ob_start();@session_start();
@mysql_connect(C_HOST, C_USER, C_PASS) or die('Cant connect');
@mysql_select_db(C_BASE) or die('Cant select DB');

function online() {
$ctime = time()+1800;

if((isset($_SESSION['id']))&&(is_numeric($_SESSION['id']))) {

$query = mysql_query("UPDATE session SET time='$ctime2' WHERE userid='".$_SESSION['id']."'") or die('Error');
$affected_rows = mysql_affected_rows();
if ($affected_rows != 1) @session_destroy();
}
}

//many other functions go here

online();
?>
<?php
include_once 'configuration.inc.php';
include_once 'functions.inc.php';

//many things go here

$upd = mysql_query("INSERT INTO session VALUES ('" . $i['id'] . "','$ctime')") or die('Error2');
Header("Location: /content.php?justlogged=1");
die;
?>
<?php
include_once 'configuration.inc.php';
include_once 'functions.inc.php';
//many thing go here
echo "content";

?>
    <?php
    include_once 'configuration.inc.php';
    include_once 'functions.inc.php';

    if (isset($_GET['tid'])&&(is_numeric($_GET['tid']))){
    $result = mysql_query("delete from some_table where something = '" . $_GET['tid'] . "'") or die('Error123a');
Header("Location: /test.php");
    die;
    }

    //file content

    ?>

content.php

<?php
@ob_start();@session_start();
@mysql_connect(C_HOST, C_USER, C_PASS) or die('Cant connect');
@mysql_select_db(C_BASE) or die('Cant select DB');

function online() {
$ctime = time()+1800;

if((isset($_SESSION['id']))&&(is_numeric($_SESSION['id']))) {

$query = mysql_query("UPDATE session SET time='$ctime2' WHERE userid='".$_SESSION['id']."'") or die('Error');
$affected_rows = mysql_affected_rows();
if ($affected_rows != 1) @session_destroy();
}
}

//many other functions go here

online();
?>
<?php
include_once 'configuration.inc.php';
include_once 'functions.inc.php';

//many things go here

$upd = mysql_query("INSERT INTO session VALUES ('" . $i['id'] . "','$ctime')") or die('Error2');
Header("Location: /content.php?justlogged=1");
die;
?>
<?php
include_once 'configuration.inc.php';
include_once 'functions.inc.php';
//many thing go here
echo "content";

?>
    <?php
    include_once 'configuration.inc.php';
    include_once 'functions.inc.php';

    if (isset($_GET['tid'])&&(is_numeric($_GET['tid']))){
    $result = mysql_query("delete from some_table where something = '" . $_GET['tid'] . "'") or die('Error123a');
Header("Location: /test.php");
    die;
    }

    //file content

    ?>

test.php

<?php
@ob_start();@session_start();
@mysql_connect(C_HOST, C_USER, C_PASS) or die('Cant connect');
@mysql_select_db(C_BASE) or die('Cant select DB');

function online() {
$ctime = time()+1800;

if((isset($_SESSION['id']))&&(is_numeric($_SESSION['id']))) {

$query = mysql_query("UPDATE session SET time='$ctime2' WHERE userid='".$_SESSION['id']."'") or die('Error');
$affected_rows = mysql_affected_rows();
if ($affected_rows != 1) @session_destroy();
}
}

//many other functions go here

online();
?>
<?php
include_once 'configuration.inc.php';
include_once 'functions.inc.php';

//many things go here

$upd = mysql_query("INSERT INTO session VALUES ('" . $i['id'] . "','$ctime')") or die('Error2');
Header("Location: /content.php?justlogged=1");
die;
?>
<?php
include_once 'configuration.inc.php';
include_once 'functions.inc.php';
//many thing go here
echo "content";

?>
    <?php
    include_once 'configuration.inc.php';
    include_once 'functions.inc.php';

    if (isset($_GET['tid'])&&(is_numeric($_GET['tid']))){
    $result = mysql_query("delete from some_table where something = '" . $_GET['tid'] . "'") or die('Error123a');
Header("Location: /test.php");
    die;
    }

    //file content

    ?>

function.inc.php
中调用
online()
-会话时间每秒都会更改。但是,您在页面(登录、内容、测试)之间切换的速度是否会超过1秒?在这种情况下,时间将是相同的,并且由于未受影响的行,您将得到会话破坏

编辑:

对。正如我所想

看看它是怎么来的:


调用login.php:成功登录后,它将使用time
X
创建新会话。在此之后,您将立即重定向到content.php(时间仍然是
X
),再次调用
online
。当然,当你立即重定向时,时间是一样的。。因此,在
content.php
会话已经被破坏,因为时间没有改变。

我们可以知道您的MySQL版本吗?在再次阅读您的代码后,我找不到错误。但我的建议是,指定链接标识符以避免使用错误的DB链接。@Shivan Raptor,代码设计中的错误。他正在为每个文件调用
online()
,即使是在login.php中。。他正在调用
online()
,然后在会话中插入新记录。。然后将用户重定向到content.php,再次调用
online()
。这方面的时间表是什么?回答:最低限度。这很可能所有的秒数都是相同的,当然受影响的行将返回
0
。Vitaly Dyatlov,这里有两个时间戳:页面刷新前1320405278,会话被破坏时更新后1320405343。@tfe,页面刷新是什么意思?哪个页面,页面导航进行得如何?我将创建一个名为debug.txt的文件,并在函数
online
中向该文件写入$user\u id和$ctime,以确保每次经过不同的时间。Vitaly,现在我明白了(看看它是怎么来的:…),谢谢。因此,在这种情况下,我不能使用mysql\u受影响的\u行。还没有看到任何其他解决方案。我之所以需要它,是因为我想为那些由于不活动而从“会话”表中删除的用户销毁会话,但他们回来后会话仍然有效,但他们不在“会话”表中,成为“幽灵”用户。我也不能使用“复制密钥更新”,因为“会话”表不仅包含用户ID,还包含年龄、化身和其他信息,这些信息在用户登录时写入“会话”表。@tfe,只需在更新之前使用
SELECT
(检查用户是否存在)。然后更新会话表中的用户,而不检查受影响的行,我可以这样做,但在每个页面单击时需要+1个MySQL查询:(