MySQL Selecting 2 Fields and Showing Them in Order /Matching Them/

Discussion in 'Programming' started by ColorWP.com, Dec 23, 2008.

  1. #1
    Hello,

    I have the following table structure:

    table name - postmeta
    fields - post_id, meta_key, meta_value
    contents (example):
    140, blogger_author, Admin
    140, blogger_blog, asd.com
    140, blogger_permalink, /something1.html
    141, blogger_author, Admin
    141, blogger_blog, asd.com
    141, blogger_permalink, /something2.html
    142, blogger_author, Admin
    142, blogger_blog, asd.com
    142, blogger_permalink, something3.html
    etc...etc

    table name - posts
    fields - ID, post_name
    contents (example):
    140, something-else1.html
    141, something-else2.html
    142, something-else3.html
    etc..etc

    I want the PHP script to extract and echo the results in the following way:
    140 something1.html something-else1.html
    141 something2.html something-else2.html
    142 something3.html something-else3.html

    Here is my script till now:
    $link = mysql_connect("host", "name", "pass") or die(mysql_error());
    mysql_select_db("db") or die(mysql_error());
    
    $query = "SELECT * FROM postmeta WHERE meta_key = 'blogger_permalink' ORDER BY 'post_id'";
    $result = mysql_query($query) or die(mysql_error());
    
    $querys = "SELECT * FROM posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY 'ID'";
    $results = mysql_query($querys) or die(mysql_error());
    
    	while($row = mysql_fetch_array($result) || $rows = mysql_fetch_array($results))
    	{
    		echo "<br>";
    		echo $row['post_id'];
    		echo " ";
    		echo $row['meta_value'];
    		echo " ";
    		echo $rows['ID'];
    		echo " ";
    		echo $rows['post_name'];
    	}
    PHP:
    No matter how hard I try, only one of the tables get extracted. The result from above is like this:
    <br> <br> <br> (this goes on until the results in "postmeta" table are over)
    140 something-else1.html<br> 141 something-else2.html<br> 142 something-else3.html etc...etc.
    HTML:
    I guess the two query fetches in the while() don't do well together.

    Any help?
     
    ColorWP.com, Dec 23, 2008 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    well I never encountered something like this but I know that this || operator means OR.
    try with && or simple and instead of ||
    let me know
     
    crivion, Dec 23, 2008 IP
  3. dremos

    dremos Peon

    Messages:
    13
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    why you use two query , try to use only one

    
    SELECT p1.post_id,
            p2.id,
            p1.meta_value
            p2.post_name
     
    FROM postmeta p1 ,posts p2
    
    WHERE p1.post_id = p2.id
    
    ORDER BY p1.post_id DESC
    
    Code (markup):
     
    dremos, Dec 23, 2008 IP