简体   繁体   中英

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". 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.

Interestingly, only the ID is returning "1", everything else is returning the correct values. The ID on the CLI is- "V8FBaMJT6bqPbpRutJgkRdc44S3Gz3H8VjW5iu5E4yhBlLA1/D8o+EcGMUY62LZLrxb2SSkaxBoUwaiQXQv+3OsoDTuheYTy4ibPsy91X8JhNGFjOWM2MWQyMWMxMTBmMTU5YjU5NzU2NGM3OTc1YQ==" , so there's no chance that it is equal to "1".

$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. Also, if I remove the INNER join part, and do a simple SELECT * FROM box , it returns the ID perfectly. And I have also tried doing a stmt->num_rows , and it returns '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. Thank you.

The problem is that you use SELECT * FROM ... which returns every column from any table you are using. 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 . The value of $post['id'] will contain the value of the interactions table. So, change the SELECT query by writing:

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

Also, do not use the eval() function, ever . You might want to switch to PDO which has a more clear API (the PDOStatement::fetch function returns the next row as an array)

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 . 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 .

  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.


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. 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 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(); to the code and it worked flawlessly just before I did a echo $stmt->num_rows; and it showed the results perfectly.

Thank you, again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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