PHP/MySQL Information Retreival

Discussion in 'PHP' started by kodaks, Aug 28, 2008.

  1. #1
    Hi everyone,
    I not very well versed with PHP, but I am trying my best to learn it. I've put together a basic function for a script I am using that retrieves information from a MySQL database.

    Here is basically what I am trying to accomplish:

    1) Once an item has been added my shopping cart, each item is recognized by an ID number ($aid). I would like my PHP script to first find the corresponding 'aid' from a table called 'my_table'. While it is retrieving the 'aid' from the table, it will also retrieve 'filename' and 'pid' from the same table.

    With that being said, here is what step #1 looks like (it works fine):
    		foreach ($contents as $id=>$qty) {
    			$sql = 'SELECT * FROM ms_attachments WHERE aid = '.$id;
    			$result = $db->query($sql);
    			$row = $result->fetch();
    			extract($row);
    			$output[] = '<tr>';
    			$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
    			$output[] = '<td>'.$filename.' located in PID # '.$pid.'</td>';
    			$output[] = '</tr>';
    		}
    
    PHP:
    Here is where I don't know what to do. I would like to find the name of the product that I currently have added to my shopping cart. The only problem is, the name of the "aid" is located on another table, let's call it 'my_othertable'. Here is what I need to have happen to get this script to work the way I need it to:

    After completing Step #1 (like posted above), I want the script to take the 'pid' (collected from the first query in my_table) and match it to the corresponding 'pid' number in my second table (my_othertable). Once it matches the corresponding 'pid' number in 'my_othertable', it needs to find the 'subject' from 'my_othertable'.

    How would I got about doing this? I did a little research and I think I must have to use some kind of "LEFT JOIN" syntax, but other than that I have no idea.

    I really appreciate any help, thanks in advance!
     
    kodaks, Aug 28, 2008 IP
  2. rcadble

    rcadble Peon

    Messages:
    109
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can just use a mysql query, here's an example on how to get the subject.

    
    $pid = 0;//edit this to be your pid.
    $result = mysql_query("SELECT * FROM `my_othertable` WHERE `pid` = '{$pid}' LIMIT 1;") or die(mysql_error());
    if (mysql_num_rows($result)) {
         $row = mysql_fetch_array($result);
         $subject = $row['subject'];
    } else {
         echo "Corresponding PID {$pid} was not found in table!";
    }
    Code (markup):
     
    rcadble, Aug 28, 2008 IP
  3. definitely

    definitely Well-Known Member

    Messages:
    520
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    160
    #3
    $result = mysql_query("SELECT * FROM `my_othertable` WHERE `pid` = '{$aid}';") or die(mysql_error());
    $row = mysql_fetch_array($result);
    $subject = $row['subject'];
     
    definitely, Aug 31, 2008 IP