PHP Mysqli语句返回单行,受影响的是-1行,并且没有错误 [英] PHP Mysqli statement returns a single row, -1 rows affected, and no error

查看:51
本文介绍了PHP Mysqli语句返回单行,受影响的是-1行,并且没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真是令人莫名其妙,我必须缺少一些简单的东西.我有一个查询,检查要插入的事务是否已经存在,以防止重复.这是代码:

This is so baffling I MUST be missing something simple. I have a query that checks to see if the transaction I'm inserting already exists in order to prevent duplicates. Here's the code:

function isaDupe($portableDB, $transactArray)
{
    $ref = $transactArray["reference"];
    $date = $transactArray["qdate"];
    $time =  $transactArray["time"];

  //prints the query so I can run by hand to test
    print "SELECT `counter` FROM transactions WHERE (`reference` = '$ref' AND `qdate` = '$date' AND `time` = '$time') ";

    if ($dupeSelectStmt = $portableDB->prepare("SELECT `counter` FROM transactions WHERE (`reference` = ? AND `qdate` = ? AND `time` = ?)"))
    {
        $dupeSelectStmt->bind_param('sss',$ref, $date, $time);
        $dupeSelectStmt->bind_result($counter);
        $dupeSelectStmt->execute();

        while ($dupeSelectStmt->fetch())
        {
            break;
        }

        $numRows = $portableDB->affected_rows;

        if ($numRows > 0)
            return TRUE;
        else if ($numRows == -1)
        {
            print " ERROR: ";
            print_r($portableDB->error);
            print_r($dupeSelectStmt->error);
            return FALSE;
        }
        else
            return FALSE;
    }
}

-如果我通过同一服务器上的Workbench手动运行查询,则会返回24行.

-If I run the query by hand through Workbench on the same server, I get 24 rows returned.

-如果我手动准备,设置和执行语句,这是相同的.

--this is the same if I prepare, set, and execute the statement by hand.

-affected_rows返回-1

-affected_rows returns -1

-与我在语句中执行num_rows一样

--same if I do num_rows on the statement

-在Statement或MySQLi对象上没有存储错误.

-there is no error stored on the Statement or MySQLi object.

-如果我在fetch()语句中添加了打印内容,它将打印出一行的数据

-if I put a print in the fetch() statement, it prints one row's worth of data

-如果我将提取的行存储到数组中并计算结果,则为1

-if I store the fetched rows into an array and count the results, it's 1

-我已经尝试使用每个变量分别运行它,同样的事情.

-I've tried running it with each variable separately, same thing.

-同一台服务器(heck,同一MySQLi对象)上的其他查询工作正常.选择,更新和插入.

-other queries on the same server (heck, on the same MySQLi object) are working fine. SELECTS, UPDATES, and INSERTS.

推荐答案

答案是我忘记了在mysqlistmt :: execute()之后再调用mysqlistmt :: store_result.

The answer is I was forgetting to call mysqlistmt::store_result after mysqlistmt::execute().

一旦我添加了$ dupSelectStmt-> store_result();我能够调用$ dupSelectStmt-> num_rows和$ portableDB-> affected_rows,它们都显示了我知道应该看到的24个.

Once I added $dupSelectStmt->store_result(); I was able to call $dupSelectStmt->num_rows and $portableDB->affected_rows and they both showed the 24 I knew I should be seeing.

这篇关于PHP Mysqli语句返回单行,受影响的是-1行,并且没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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