str_split并从mysql获取数据

maybe someone can help me with this solution. It´s a little bit tricky for me.

I refer with _GET a long number (like 102030). Now i want to split this long number into shorter numbers (like 10 20 30). This short numbers are id´s in my database in mysql and with them i want to get all the id_name's foreach short number (like name-10 name-20 name-30).

To split the long number i used str_split, but i don't know how to use this array now.

Here is my code. (The database is included before this code)

<?php
$long_id =$_GET[long_id];

if($_SESSION['test']) {
foreach($_SESSION['test'] as $split_id => $number) {

$split_id = str_split( $long_id, 2);

                $result = mysql_query("SELECT id, id_name FROM names"); 
//Here is my WHERE missing, because it's not working with split_id...
                list($id, $id_name) = mysql_fetch_row($result);
                while($row = mysql_fetch_array($result)){
                echo $id_name;

                }
}
} else {
   echo "test is empty";
}
?>

Thanks

change this

   $long_id =$_GET[long_id];

to

 $long_id =$_GET['long_id'];

you may try this

  $result = mysql_query("SELECT id, id_name FROM names WHERE id= '$split_id[0]' ");

this will look for the two first digits , if you want the other digits also

you may add

    OR id= '$split_id[1]' OR  id= '$split_id[2]' ....

Okay. Instead of bludgeoning on on a sketchy design path, let's just have a re-think about your design.

Your data

You have one long number, 102030, and you split that up. Might I ask, How? You have no delimiting characters. How does PHP know where one number starts and one number ends? It appears you've settled for 'every two characters'. This is quite rigid code.

I would recommend taking a CSV approach, and using commas to split up the numbers. That way, it's much more readable for you if you have to debug, and the code will be alot more flexible. It would mean you can have 1 or 4904909 as an ID, instead of 01.

Your database connectivity

You're using deprecated code! Deprecated code is no longer supported and in later versions of PHP, WILL NOT WORK. You need to update your code to either the mysqli or use a PDO object.

Lets pretend that $_GET['long_id'] holds the value "102030".

Then your where should be something like:

WHERE id=$split_id[0] OR id2=$split_id[1] OR id3=$split_id[2]

To just show all the separate id's you can do the following:

$long_id =$_GET[long_id];
foreach ($split_id as $id)
{
  echo "This is a id: ".$id."<br />";
}

Output: This is a id: 10 This is a id: 20 This is a id: 30