Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Apr 2005
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    SQL UNION clause

    Anything wrong with this code?

    Code:
    $sql = "SELECT * FROM decipleInfo WHERE decipleName='".$q."' UNION SELECT * FROM deciplezonedetail WHERE decipleName='".$q."'";
    PHP Code:
    $result mysql_query($sql);

    while(
    $row mysql_fetch_array($result))
    {
    echo 
    "<tr>";
    echo 
    "<td>" $row['decipleInfo.Name'] . "</td>";
    echo 
    "<td>" $row['decipleInfo.userid'] . "</td>";
    echo 
    "<td>" $row['deciplezonedetail.email'] . "</td>";
    echo 
    "<td>" $row['deciplezonedetail.zoneName'] . "</td>";
    echo 
    "</tr>";
    }
    echo 
    "</table>"
    I got this Error..
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Inetpub\wwwroot\cyyamhub\library\getPageInfo.php on line 27

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    A union between 2 tables requires them to have the same number of columns, and to generally be similar (I'm not sure what mysql does if you try to union a text-column with, for example, a timestamp).

    But regardless, from your code, it doesn't look like you actually want to be doing a union, but a JOIN instead.

    Without the structure of your tables I'm guessing, but

    SELECT i.*, z.* FROM discipleinfo i JOIN disciplezonedetail z ON (i.disciple_id=z.disciple_id) WHERE i.disciplename=\''.mysql_real_escape_string.'\'';

    should point you in the right direction.

    As far as the output goes, table prefixes don't get used as array indices, so $row['name'] rather than $row['i.name'] (with the aliased tables in my query).
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •