Implode with While Loop

Hi guys…

my while loop is working but as you can see on the last line i do a vard_dump() it shows 2 values in the array, but yet i only get 1 result from my previous page even though there are 2 in the array.

   <?php		
        $value= $_POST['check'];
        $id=implode(',', $value);

        $sql = "SELECT  id,code,item,measure,size,test,sdone,sdue  FROM stores WHERE id = '$id'";
            $result = mysqli_query($link, $sql);


          $no 	= 1;
                $total 	= 0;          
        while ($row = mysqli_fetch_array($result))

                {

                    echo "<tr>";
                   echo "<td>$no</td>";
                    echo "<td>{$row['id']}</a></td>";
                    echo "<td>{$row['code']}</td>";
                    echo "<td>{$row['item']}</td>";	
                    echo "<td>{$row['measure']}</td>";   
                    echo "<td>{$row['size']}</td>";
                    echo "<td>{$row['sdone']}</td>";
                    echo "<td>{$row['sdue']}</td>";
                    echo "</tr>";
        }
           var_dump($id);
                    print_r($id);
        ?>

Makes no sense. An ID is a unique identifier, so from a logical point of view you will never get more then exactly one result for any ID.

On my previous page i have a list of items, where you can choose multiple ones via a checkbox (on the second page where the $_POST variable is) i captured that input. it shows on the vardump() that it is actually there. are you saying that my SQL query might be at fault here. as in my SQL each item has a unique ID and the value im checking is basicaly to see $_post = any id in the database and display the multiple ID as per previous page in a while loop…

still a newbie as you can tell

string(12) “219,220,1004” This is the output of the vardump…

my while only prints out the value of “219” and item details.
not “220”
and “1004”

You are checking for which row the column value is literally 219,220,1004.

But 219,220,1004 is not a number so MySQL doesn’t understand that and it stops parsing after 219 1. If you want all of those numbers you need to modify your query that want the rows where the ID is 219 or the ID is 220 or the ID is 1004.

1 It should have just went :boom: instead of you ask me, but ok.

2 Likes

What a weird hack MySQL does here. And another item on my pro-postrges-list

Postgres:

ERROR: invalid input syntax for integer: "2,3,4"

At least MySQL throws an error when the “list” is unescaped

select * from ids where id = 2,3,4
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '3,4'

alternative: where ID is IN a list of integers.

thanx i will keep trying… very frustrating :frowning:

It’s basically the same PHP and JavaScript do when casting such a value to an int (or indeed on loose comparison).
However I do agree that throwing an error like Postgres does is a lot nicer :thumbsup:

There’s no problem, the solution is in post #5 and post #7. Just modify the query to use the correct syntax for whichever of those options you prefer.

Thanks all you guys for the guidance… Sometimes its better to struggle a bit . You learn much more.

Thanks Again… :smile:

1 Like

Out of curiosity, what did you end up with?

Same here. I realize mistakes in logic can’t be easily detected (eg. == instead of === :blush: ) but I would have expected a syntax error or wrong data type to throw an error.

@johannesmoolman , any chance that field isn’t an INT ?

$value= $_POST[‘check’];
$CID=implode(‘,’, $value);

    $sql = "SELECT  id,code,item,measure,size,test,sdone,sdue from stores WHERE id IN ($CID)";

Cool. Next up is learning how to use prepared statements, as this query is open for SQL Injection.
Not that it’s much of a problem for this one, but there will certainly be others where it is a problem.

Have a look at https://secure.php.net/manual/en/mysqli.prepare.php

1 Like

Agreed, currently not worried about it, but for sure can be dangerous later on

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.