+ Reply to Thread
Results 1 to 3 of 3

Thread: Problem: Taking information from...

  1. #1
    begamer26 is offline x10Hosting Member begamer26 is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    24

    Problem: Taking information from...

    I'm trying to take information from one mysql table and put it into another.

    It's VERY hard to explain but here's my code:
    PHP Code:
                          $con = include('dbc.php');
                          
    mysql_select_db("tb_begamer, $con");
                          
                          
    $user mysql_query("SELECT user FROM users");
                          
    $pic mysql_query("SELECT picture FROM images WHERE username = $user");
                          echo(
    '<img src="' $pic '" height="140" />'); 

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Problem: Taking information from...

    It's not as hard as you think. Start with a statement in English describing the data you want (such as "The picture for the user with a given ID."). Note that you're not putting data from one table into another (which would involve an INSERT statement), you're using data from one table to filter rows from another (i.e. selecting data from one table based on restraints on another, related table). What you're looking for is the JOIN clause.

    Code:
    SELECT picture 
      FROM images 
        JOIN users ON images.username=users.user
      WHERE ...
    One thing you've left out of your sample is how you're picking a user. That is, you've no WHERE clause when selecting from the `users` table. Consequently, the query will return all usernames in the `users` table.

    Queries return results, not strings. To get a username from $user, you have to use a function to retrieve a row from it, such as mysql_fetch_row. Rather than making that change, you should switch to PDO. The mysql driver is quite outdated. If you want a PDO tutorial, try "Writing MySQL Scripts with PHP and PDO".

    PHP Code:
    $getImage $db->prepare('SELECT picture 
      FROM images 
        JOIN users ON images.username=users.user
      WHERE users.id = ?'
    );
    $getImage->execute(array($id));
    $pic $getImage->fetchColumn(); 
    Lastly, separate display from database access. The code that fetches the image URL shouldn't also generate the <img> tag. This is a consequence of the single responsibility principle and part of separation of concerns. Read up on the Model-View-Controller pattern for more on a better approach.
    Last edited by misson; 11-14-2010 at 08:23 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    begamer26 is offline x10Hosting Member begamer26 is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    24

    Red face Re: Problem: Taking information from...

    Thank you very much! :D Your the mannn!

+ Reply to Thread

Similar Threads

  1. Problem Loading Page: Taking too long to respond?
    By matthewbyrom in forum Free Hosting
    Replies: 7
    Last Post: 02-23-2010, 06:13 PM
  2. Changes not taking
    By curtc95 in forum Free Hosting
    Replies: 1
    Last Post: 01-29-2010, 10:23 AM
  3. Account information retrieval problem
    By vinayan in forum Free Hosting
    Replies: 5
    Last Post: 03-19-2008, 10:08 AM
  4. Not Taking new PW
    By dead420 in forum Free Hosting
    Replies: 4
    Last Post: 01-03-2008, 05:38 PM
  5. Why is no one taking me or us on
    By kemraj in forum Free Hosting
    Replies: 13
    Last Post: 11-22-2007, 11:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers