Jump to content

Pulling data on MYSQL using an Array


Q695

Recommended Posts

What am I missing to do the search?

 

error test:

Array ( [0] => [1] => 1 ) Array ( [0] => [1] => 1 [2] => 4 ) sql error

 

sql : SELECT * FROM thoughts WHILE id = 'Array' ORDER BY `thoughts`.`time` DESC

 

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 'WHILE id = 'Array' ORDER BY `thoughts`.`time` DESC' at line 1

 

<?php
$sql2="SELECT * FROM thoughts WHILE id = '$contacts' ORDER BY `thoughts`.`time` DESC";
$result=@mysql_query($sql2,$con) or die(death($sql2));
?>

Link to comment
https://forums.phpfreaks.com/topic/214999-pulling-data-on-mysql-using-an-array/
Share on other sites

I can't really tell what your $contacts array looks like; but if it is a simple array of numeric id's you can just implode() it:

$sql2="SELECT * FROM thoughts WHERE id IN (" .
  implode(', ', $contacts) . 
  ") ORDER BY `thoughts`.`time` DESC";

new error:

Array ( [0] => [1] => 1 ) Array ( [0] => [1] => 1 [2] => 4 ) sql error

 

sql : SELECT * FROM thoughts WHERE id IN (, 1, 4) ORDER BY `thoughts`.`time` DESC

 

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 ' 1, 4) ORDER BY `thoughts`.`time` DESC' at line 1

 

My array loader code is:

$contacts = array("");
while ($row=mysql_fetch_array($result)){
  if ($id==$row[user_1]){
  $contact=$row[user_2];
  } else {
  $contact=$row[user_1];
  }
array_push($contacts, "$contact");
print_r($contacts);
  }

I know, but what what is the code solution to build an array out of filter picked solutions?

 

I can put 0, due to the auto-increment never having that be a number, but if the website takes off, I want something that will be quicker than checking if something is always going to be a 0.  It could put your own posts up.

implode() is correct, but apparently one of the values for $contact is an empty string, so the query fails. here is how I use implode to account for this possibility. i ALWAYS single-quote values, be they numeric or non-numeric.

 

$sql2="SELECT * FROM thoughts WHERE id IN ('" . implode("', '", $contacts) . "') ORDER BY `thoughts`.`time` DESC";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.