0

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?

4
Contributors
17
Replies
18
Views
7 Years
Discussion Span
Last Post by Mike516
0

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.

0

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?

0

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

// your code

$i++;
}

0

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

0
$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

Edited by Nick Evan: Fixed formatting

0

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.

Edited by Nick Evan: Fixed formatting

0
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>";
}
0

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>";
			
			}

Edited by Mike516: n/a

0

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.

0

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

Edited by Mike516: n/a

1
while($row=mysql_fetch_array($result_getip))

like above again you need to generate to print 2 names

Votes + Comments
Thanks for the help
0

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

Edited by Mike516: n/a

Attachments 2010-07-19_0625.png 35.16 KB
0

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

Attachments dani.png 29.17 KB
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.