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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.