简体   繁体   English

PHP mysqli查询在联接上返回结果“ 1”

[英]PHP mysqli query returning result “1” on joins

I have been trying to pull out some values from php-mysql, and it's returning a strange result "1". 我一直在尝试从php-mysql中提取一些值,并且它返回一个奇怪的结果“ 1”。 It comprises of an inner join with a where clause, everything works fine, but the result is strangely "1" when I try to echo out the ID, but I am assured that the ID is not 1. When I run the same query on the MySQL CLI, it returns exactly what I need but in the php, it doesn't even say there's an error. 它由一个带有where子句的内部联接组成,一切正常,但是当我尝试echo显ID时,结果奇怪的是“ 1”,但是我确信ID不是1。 MySQL CLI,它完全返回我需要的内容,但是在php中,它甚至没有说出错误。

Interestingly, only the ID is returning "1", everything else is returning the correct values. 有趣的是,只有ID返回“ 1”,其他所有返回正确的值。 The ID on the CLI is- "V8FBaMJT6bqPbpRutJgkRdc44S3Gz3H8VjW5iu5E4yhBlLA1/D8o+EcGMUY62LZLrxb2SSkaxBoUwaiQXQv+3OsoDTuheYTy4ibPsy91X8JhNGFjOWM2MWQyMWMxMTBmMTU5YjU5NzU2NGM3OTc1YQ==" , so there's no chance that it is equal to "1". CLI上的ID是- "V8FBaMJT6bqPbpRutJgkRdc44S3Gz3H8VjW5iu5E4yhBlLA1/D8o+EcGMUY62LZLrxb2SSkaxBoUwaiQXQv+3OsoDTuheYTy4ibPsy91X8JhNGFjOWM2MWQyMWMxMTBmMTU5YjU5NzU2NGM3OTc1YQ=="

$showPosts = "SELECT * FROM box 
              INNER JOIN interactions 
              ON box.u_id=interactions.actorid 
              WHERE interactions.actorid=? 
                AND interactions.type IN ('1','5') 
              ORDER BY box.time_created";

if ($stmt = $conn->prepare($showPosts)) {
    /* bind parameters for markers */
    $b = "V8FBaMJT6bqPbpRutJgkRdc44S3Gz3H8VjW5iu5E4yhBlLA1/D8o+EcGMUY62LZLrxb2SSkaxBoUwaiQXQv+3OsoDTuheYTy4ibPsy91X8JhNGFjOWM2MWQyMWMxMTBmMTU5YjU5NzU2NGM3OTc1YQ==";
    $stmt->bind_param("s", $b);


    /* execute query */
    $stmt->execute();

    /* bind result variables */
    $metaResults = $stmt->result_metadata();
    $fields = $metaResults->fetch_fields();
    $statementParams = '';

    //build the bind_results statement dynamically so I can get the results in an array
    foreach($fields as $field){
        if(empty($statementParams)){
            $statementParams .= "\$post['".$field->name."']";
        } else {
            $statementParams .= ", \$post['".$field->name."']";
        }
    }

    $statment = "\$stmt->bind_result($statementParams);";
    eval($statment);

    while($stmt->fetch()){
        echo $post['id'];
        echo $post['time_created'];
    }
}

Now, in the result- the first is the strange "1" and the second is the timestamp. 现在,结果是-第一个是奇怪的“ 1”,第二个是时间戳。 Also, if I remove the INNER join part, and do a simple SELECT * FROM box , it returns the ID perfectly. 另外,如果我删除INNER连接部分,并执行一个简单的SELECT * FROM box ,它会完美地返回ID。 And I have also tried doing a stmt->num_rows , and it returns '0'. 而且我也尝试过做一个stmt->num_rows ,它返回'0'。 Maybe I am missing something obvious. 也许我缺少明显的东西。

I am new to using joins in php mysql and it has been a big headache for me for the last 2 hours, any help is appreciated. 我是在php mysql中使用Joins的新手,在过去的2个小时中,这一直让我头疼不已,感谢您的帮助。 Thank you. 谢谢。

The problem is that you use SELECT * FROM ... which returns every column from any table you are using. 问题是您使用SELECT * FROM ...会返回您正在使用的任何表中的每一列。 This is not what you want. 这不是您想要的。 Always write the columns you want to read from the query. 始终编写要从查询中读取的列。

In this case you have more than one Id column in your result set, one from the table box , the other one from the table interactions . 在这种情况下,您的结果集中有多个Id列,一个Id列来自表box ,另一个来自表interactions The value of $post['id'] will contain the value of the interactions table. $post['id']的值将包含interactions表的值。 So, change the SELECT query by writing: 因此,通过编写以下命令来更改SELECT查询:

SELECT box.id, box.time_created FROM ...

Also, do not use the eval() function, ever . 另外, 永远不要使用eval()函数。 You might want to switch to PDO which has a more clear API (the PDOStatement::fetch function returns the next row as an array) 您可能希望切换到具有更清晰API的PDOPDOStatement :: fetch函数将下一行作为数组返回)

After hours of research, I have come to an working example and it works just fine. 经过数小时的研究,我得出了一个可行的例子,它很好用。 Someone else might face the same problems in the future and make mistakes, so I am posting an answer to the problem here: 将来其他人可能会遇到相同的问题并犯错,因此我在这里发布了该问题的答案:


Problems: 问题:

  1. Having same column names in two joined tables 在两个联接表中具有相同的列名

The first problem was the the table interactions had the same column name id as that of the table box . 第一个问题是表交互具有与表相同的列名id Since I used SELECT * FROM box along an inner join with interactions , it resulted in the return of the results based on the second id which was of interactions rather than box . 由于我沿着带有交互作用的内部SELECT * FROM box使用SELECT * FROM box ,因此它基于第二个id交互作用而不是box)返回结果。

  1. Not storing the results before showing record count 在显示记录数之前不存储结果

Secondly, the problem was the results were not being stored as in http://php.net/manual/en/mysqli-stmt.num-rows.php , which was a silly mistake. 其次,问题是结果没有像http://php.net/manual/en/mysqli-stmt.num-rows.php那样存储,这是一个愚蠢的错误。


Solutions: 解决方案:

  • Addressing the column repition 解决列重复

So, firstly I decided to change the column name of the table interactions for id , and I changed it to inter_id to avoid further conflicts. 因此,首先我决定将表交互的列名更改为id ,然后将其更改为inter_id以避免进一步的冲突。 Though not a very smart step, it will avoid such silly mistakes in the future. 尽管这不是一个非常明智的步骤,但它将避免将来出现此类愚蠢的错误。 Then as pointed out by the previous answer, I had to specify the column names of the results I wanted to output rather than using a SELECT * , so I changed it to 然后,如前一个答案所指出的,我必须指定要输出的结果的列名,而不是使用SELECT * ,因此将其更改为

              SELECT box.id, box.time_created FROM box 
              INNER JOIN interactions 
              ON box.u_id=interactions.actorid 
              WHERE interactions.actorid=? 
              AND interactions.type IN ('1','5') 
              ORDER BY box.time_created";

That pretty much solved the first problem. 这几乎解决了第一个问题。

  • Counting the number of results 计算结果数

The second one was just about adding the $stmt->store_result(); 第二个只是添加$stmt->store_result(); to the code and it worked flawlessly just before I did a echo $stmt->num_rows; 代码,并且在我执行echo $stmt->num_rows;之前,它可以完美地工作echo $stmt->num_rows; and it showed the results perfectly. 它完美地显示了结果。

Thank you, again. 再次感谢你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM