正确的方式来获取mysql数据

I have 2 mysql tables as follows:

  1. tblEvents - which has fields id, album_id, etc.
  2. tblImages - which has fields id, images and album_id

I am fetching data from tblEvents as follows:

<?php
$sqlBlog = "SELECT * FROM tblEvents";  
$myBlog = mysql_query($sqlBlog, $con);  
while($blog = mysl_fetch_array($myBlog)){  
$port = '<div>Album Id:'.$blog['album_id'].'</div>'
echo $port;  
} 

Now, I want to fetch all images from tblImages that has the corresponding album_id. Note that album_id is the same in both the tables. I want to echo the images in <li></li> within the above div. What is the best way to do that??

Thanks and Regards.

You can use JOIN for this. Have a look at https://dev.mysql.com/doc/refman/4.1/en/join.html

An example query could look like this:

SELECT *
FROM tblEvents events
JOIN tblImages images
ON images.album_id = events.album_id 

Note that events and images are now aliases for the tables tblEvents and tblImages
Now you could even select both ids in the same query by prepending said aliases like so:

SELECT images.album_id, events.album_id
FROM tblEvents events
JOIN tblImages images
ON images.album_id = events.album_id 
SELECT 
  * 
FROM
  tblEvents AS e
  INNER JOIN tblImages AS i
    ON i.album_id = e.album_id

Use this query and try also implment mysqli or PDO