SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    Ohio, USA
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating vars from 2 tables for use on same page

    I've seen similar questions in here but not quite the one I have. Mind you, I understand a bit of MySQL but in no way am I a pro at it, hence this question.

    I need to create variables from 2 different tables in the same DB. Neither table contains the same colname. Check out the below:

    PHP Code:
    <?
    include("admin/dbinfo.inc.php");
    mysql_connect(localhost,$username,$password);
    @
    mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM contacts";
    $result=mysql_query($query);

    $num=mysql_numrows($result); 

    mysql_close();


    ?>


    <?
    $first
    =mysql_result($result,$i,"first");
    $last=mysql_result($result,$i,"last");
    $mobile=mysql_result($result,$i,"mobile");
    $phone=mysql_result($result,$i,"phone");

    ?>
    PHP Code:
    <?
    include("admin/dbinfo.inc.php");
    mysql_connect(localhost,$username,$password);
    @
    mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM users";
    $result=mysql_query($query);

    $num=mysql_numrows($result); 

    mysql_close();


    ?>


    <?
    $sname
    =mysql_result($result,$i,"sname");
    $sphoto=mysql_result($result,$i,"sphoto");
    $sinfo=mysql_result($result,$i,"sinfo");
    $scontact=mysql_result($result,$i,"scontact");
    $id=mysql_result($result,$i,"id");

    ?>
    The above are my 2 tables with the SELECT info & the variables I need. How would I go about combining the two so I could then use all vars on the same page?

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Identify your queries with meaningful variable names early on. Its a familiar trap to fall into. You have query syntax that actually works so you end up copy/pasting it.

    Try working like this.

    Make your SQL statements really obvious by formatting them in a particular way (believe me, there will be a future version of you coming back to this code saying 'I need to change that query, now where would it be?')

    Give them a variable name that describes what the returned array actually contains, rather than bland $query monikers that threaten to overwrite each other (yeah, skip the step where you start numbering them $query1, $query2 -- believe me, that's just another route to madness )

    PHP Code:
          $users_query="
          SELECT id
              , sname
              , sphoto
              , sinfo
              , scontact 
              FROM users"


    // array called `users` (plural)

    $users mysql_query($users_query); 

    // now loop through all your users

    // turn the plural `users` into a single `user` - use the same pattern everywhere you can

    foreach( $users as $user){

    // if you are using the array notation
    echo $user['sname'] . ' has the id ' $user['id'] . PHP_EOL ;




Bookmarks

Posting Permissions

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