0

I have this code: <?php echo $pinDetails->id;?>

this gives me a unique ID and displays it on my page.

I am trying to check whether this unique ID for one table is also in another table. Then if it is, show other data from that table.

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

To my understanding this should work fine but nothing appears. Do I have the syntax wrong?

Also, is there a cleaner way of doing this?

zzlalani
  • 22,960
  • 16
  • 44
  • 73
Dalían
  • 222
  • 3
  • 4
  • 14
  • Please, **don't use `mysql_` functions**. They are officially deprecated, see [this topic on StackOverflow](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). You should have a look at [PDO](http://php.net/pdo), or [`mysqli_`](http://php.net/mysqli). – Guillaume Poussel Nov 03 '13 at 15:26
  • change your query to `"SELECT * FROM repin WHERE new_pin_id>{$pinDetails->id}"` – zzlalani Nov 03 '13 at 15:26
  • and why it is `>` why not `=` – zzlalani Nov 03 '13 at 15:26

2 Answers2

1

You cannot use string interpolation by calling an object attribute directly.

Try this:

$data = mysql_query("SELECT * FROM repin WHERE new_pin_id>{$pinDetails->id}") 

or even better with string concatenation:

$data = mysql_query("SELECT * FROM repin WHERE new_pin_id>" . $pinDetails->id) 
Guillaume Poussel
  • 9,572
  • 2
  • 33
  • 42
0

Change your query like

<?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']."";
} 
?>
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • This gives the following error code: 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 '.20042' at line 1. "20042" is the ID number. – Dalían Nov 03 '13 at 15:26
  • 1
    Perfect, works fine now. I'll examine my original code and the one you provided to see where I went wrong. – Dalían Nov 03 '13 at 15:49
  • Q: How would I use the detail `".$info['from_pin_id']."` in another statement to grab info? Combine it with variables? – Dalían Nov 03 '13 at 16:09