0

I have this code:

<?php
$data = mysql_query("SELECT * FROM repin WHERE new_pin_id LIKE ".$pinDetails->id) or         die(mysql_error()); 
while($info = mysql_fetch_array( $data ))
{
Print "".$info['from_pin_id'].",".$info['new_pin_id']."";
} 
?>

Obtained thanks to this article: Check field for identical number

I'm trying to use the detail I pulled: ".$info['from_pin_id']." to get data from another table. I'm looking for the best way to do this.

I thought about making it a variable and then running a second statement within the same <?php?> which would look something like this:

Print "".$info['from_pin_id'].",".$info['new_pin_id']."";
} 
$newdata = "".$info['from_pin_id']."";
// new statement here.
?>

But 1. it won't work and 2. it looks messy.

What is the best way to achieve it?

FYI, what I need to do is use ".$info['from_pin_id']." to match a field in another table where the data is the same ID, then pull more info based on the match.

Community
  • 1
  • 1
Dalían
  • 222
  • 3
  • 4
  • 14
  • Why do you surround your strings with `"".` and `.""`? – Barmar Nov 03 '13 at 16:19
  • 1
    Can't you just use a normal SQL JOIN query? – Barmar Nov 03 '13 at 16:20
  • Note that the code you posted here is NOT the working code from the other question you linked. – Johannes H. Nov 03 '13 at 16:21
  • Thanks Johannes, edited Q. @Barmar, I don't know, I thought this was the rule when using `Print`. I guess it is a habit that stuck. I have tried a JOIN but since I need the first statement to execute in order to get data to run in the second it won't work. – Dalían Nov 03 '13 at 16:24
  • 1
    A join gets data from both tables at once. Use a LEFT OUTER JOIN if there might not be matching rows in the second table. – Barmar Nov 03 '13 at 16:26
  • Thanks, I've completed it with the LEFT OUTER JOIN suggestion. – Dalían Nov 03 '13 at 16:39

1 Answers1

0

Use the following query:

"SELECT *
FROM repin r
LEFT JOIN otherTable o
ON o.someColumn = r.from_pin_id
WHERE r.new_pin_id LIKE '".$pinDetails->id."'"

Also, the argument to LIKE must be a string; you need to put quotes around it.

Barmar
  • 741,623
  • 53
  • 500
  • 612