Is there any way to search with a variable that is an array.

ie)

mysql_query("Select * FROM table_name WHERE `id` = '$var'");

where $var is an array.

Is this possible?

Recommended Answers

All 17 Replies

If your objective is to compare with every element in the array, I think you will need to loop through the array and create a series of OR conditions for the WHERE.

If your objective is to compare with every element in the array, I think you will need to loop through the array and create a series of OR conditions for the WHERE.

How would I go about doing that?

$i=0;
while($var[$i])
{
mysql_query("Select * FROM table_name WHERE `id` = '$var[$id]'");

// your code

$i++;
}

$i=0;
while($var[$i])
{
mysql_query("Select * FROM table_name WHERE `id` = '$var[$id]'");

// your code

$i++;
}

Nothing happened when i did that.

I tried using var_dump() and nothing returned.

$count=count($var); // counts the number of elements in ur array
$i=0;
while($i<$count)
{
    $query=mysql_query("Select * FROM usersms WHERE id = '$var[$i]'");
    if(!$query)
    {
        echo "query failed";
    }
    $row=mysql_fetch_array($query);
    $name=$row['name'];
    echo $name;
$i++;
}

try, it will work fine

I tried it but $name doesn't hold all the proper contents.
Select * FROM usersms WHERE id = '$var[$i]'
Let's say in the loop it searches for usersms WHERE id = '1'
Now in my database there may be 2 users with that id (in my db im not using this for users but that's irrelevant). In that case $name would only hold value for one of them.

Member Avatar for rajarajan2017
foreach ($var as $a) { 
    mysql_query("Select * FROM table_name WHERE `id` = '$a'");
    statements....
}
foreach ($var as $a) { 
    mysql_query("Select * FROM table_name WHERE `id` = '$a'");
    statements....
}

Warning: Invalid argument supplied for foreach() in /home/proof/public_html/results.php on line 100

foreach ($var as $a){
  $q = "Select `name` FROM characters WHERE accountid = '$a'";
  $r = mysql_query($q);
  $row=mysql_fetch_array($r);
	echo "<td>".$row['name']."<br></td>";
        echo "</tr>";
}
Member Avatar for rajarajan2017

The above warning will come when $var is not an array or string or null? Check it twice $var is an array?

I checked with is_array and it is an array.

while($row=mysql_fetch_array($result)) {
				$var = $row['id'];
			echo "<tr>";
            echo "<td>" . $row['id'] . "<br></td>";
            echo "<td>".  $row['name'] . "<br></td>";
            echo "<td>".  $row['banned'] . "<br></td>";
            echo "<td>".  $row['lastknownip'] . "<br></td>";
			
			foreach ($var as $a){
					$q = "Select `name` FROM characters WHERE accountid = '$a'";
					$r = mysql_query($q);
						$row=mysql_fetch_array($r);
							echo "<td>".$row['name']."<br></td>";
							echo "</tr>";
			
			}
Member Avatar for rajarajan2017

No...No...$var is not an array, you just storing a array value. One more thing if you are going to check within while then why do you need foreach, remove that for each loop and just use directly as $var.

It's still only showing one instead of 2.

while($row=mysql_fetch_array($result_getip)) {
		$var = $row['id'];
	    echo "<tr>";
            echo "<td>" . $row['id'] . "<br></td>";
            echo "<td>".  $row['name'] . "<br></td>";
            echo "<td>".  $row['banned'] . "<br></td>";
            echo "<td>".  $row['lastknownip'] . "<br></td>";
			
			
	$q = "Select `name` FROM characters WHERE accountid = '$var'";
	$r = mysql_query($q);
	$row=mysql_fetch_array($r);
			echo "<td>".$row['name']."<br></td>";
		        echo "</tr>";

there are 2 names with the ID of 1 and when i use var_dump() on $r i get "Select `name` FROM characters WHERE accountid = '1'"

Member Avatar for rajarajan2017
while($row=mysql_fetch_array($result_getip))

like above again you need to generate to print 2 names

commented: Thanks for the help +1

thanks it worked.

here is a pic of what is happening
[img]http://content.screencast.com/users/Deadeye077/folders/Jing/media/1605b283-371a-40d5-8cc1-fc3473d7310f/2010-07-19_0625.png[/img]

is there away to get the related 2 and related 3 all on the same row? and then for the account id 4 the related 2 needs to be on the same row as well.

echo "<table align=center class=data>
      <tr>
          <th>Account ID </th>
	  <th>Account Name</th>
	  <th>Banned</th>
	  <th>LastknownIP</th>
	  <th>Related</th>
      </tr>
      <br>
     ";

while($row = mysql_fetch_assoc($result_getname)){
			
			$acc_id = $row['id'];	
			echo "<tr>";
            echo "<td>" . $acc_id . "<br></td>";
            echo "<td>".  $row['name'] . "<br></td>";
            echo "<td>".  $row['banned'] . "<br></td>";
            echo "<td>".  $row['lastknownip'] . "<br></td>";
			

			
			



		$select = "Select * FROM characters WHERE accountid = '$acc_id'"; 
		
		$query_geta = mysql_query($select);
		
		while($row=mysql_fetch_array($query_geta)){
		$name=$row['name'];
			
			echo "<td>".$name."<br></td>";
			echo "</tr>";
			}
		
			
		
		} 
		
echo "</table><br>";

I got that to work, how can i get a <th> to cover the remaining columns?

still need help........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.