78 users online (1 members and 77 guests)  

Thread: Sql statement


  Results 1 to 2 of 2

Related

  1. How to write if statement in MySQL    Forum: PHP Forum
    Replies: 2
  2. Syntax for Select Statement    Forum: Databases
    Replies: 2
  3. If statement inside <td>    Forum: CGI Perl Forum
    Replies: 1
  4. Select Case SQL Statement...    Forum: Databases
    Replies: 7
  5. MySQL Select statement    Forum: Databases
    Replies: 1
  1. #1
    fyrafadhli's Avatar
    New User

    Status
    Offline
    Join Date
    Aug 2007
    Posts
    1

    Sql statement

    hello...
    I want to ask about how to use sql statement when I have 2 tables and I want to display information from both table by using their ID.
    Here I have 2 tables.
    Table 1 is journal:
    This table have journal_ID and authorID

    Table 2 is author:
    This table have authorID and author_name

    So, I want to display info from table journal and get the author name from table author.But I can't display the author name.
    Here are my coding:

    Code:
    $result = mysql_query("SELECT * FROM journal HAVING description LIKE '%".$keyword."%' OR description LIKE '%".$keyword."%'");
           if (!$result || mysql_num_rows($result) < 1) {
            $href = "user_browse_1.php";
            print"<meta HTTP-EQUIV='REFRESH' CONTENT='1; URL=".$href."'><div class='sub_title'>item not found...</div><br><br><br>";
           }
    and

    Code:
    $result2 = mysql_query("SELECT name FROM author WHERE authorID=$item_Author");
             while($row2 = mysql_fetch_array($result2))
             {
              $authorname=$row2["authorID"]; 
             }
    Hope can hear from you soon.TQ
    Last edited by vinyl-junkie; 08-15-2007 at 07:21 AM. Reason: Added code tags

  2. #2
    vinyl-junkie's Avatar
    Moderator

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    721

    Re: Sql statement

    You only need one query to get what you want, by using an inner join like so:

    Code:
    $result = mysql_query("SELECT * FROM journal 
    HAVING description LIKE '%".$keyword."%' OR description LIKE '%".$keyword."%'
    INNER JOIN journal on journal.AuthorID = author.AuthorID");
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more



Tags for this Thread