i have a table named IMAGES in which there is attribute name img_ID which is the primary key,
there is another table called Favorites in which there are two attributes ‘img_ID’ & user_ID … img_ID serves as the foreign key to an image and user_ID serves as foreign key to the user
NOW i need details of from IMAGES of all the img_IDs from Favorites where user_ID is some x-value
this i could do like this, b.t.w i am using PHP & mySQL
$gett = mysql_query("SELECT img_ID FROM favorites WHERE user_ID='".$variable."'");
if( mysql_num_rows($gett) > 0 )
{
while( $steps = mysql_fetch_assoc($gett) )
{
$res = mysql_query("SELECT * FROM images WHERE img_ID='".$steps['img_ID']."' ORDER BY img_Date");
if( mysql_num_rows($res) > 0 )
{
while( $result= mysql_fetch_assoc($res) )
{
echo $result['img_Name'];
echo $result['img_Desc'];
echo $result['img_ext'];
}
else
echo "no images were found";
}
}
else
echo "no favorites were found";
THE PSEUDO CODE will be like
RUN QUERY
for each img_ID where user_ID is this
RUN QUERY
for each img_ID
perform DISPLAY
This methodology has a flaw & a back-draw
FLAW = the order which is on time stamp is destroyed
back-draw = there is a query in nested loop..
QUESTION: Can this all be done in with query? if Yes then How ?
Use a join:
Also, change your code to use mysqli and prepared statements with bind variables to both improve performance and eliminate the threat of sql injection attacks.