|
|
|
|
![]() ![]() |
Feb 13 2007, 05:05 AM
Post
#1
|
|
|
NiGHTFoX - Hiding in the dark Group: Members Posts: 680 Joined: 3-April 05 Member No.: 3,584 |
I'm working on a script where there is a custom user profile and I was wondering if there was a more efficient way to grab data stored in a database than this method:
CODE $sql = "SELECT * FROM users WHERE `access_name` = \"" .$active_user. "\""; $row = mysql_fetch_array(mysql_query($sql)); //Link the two tables together; grab the most common thing that is the *SAME* $user_id = $row['id']; $sql2 = "SELECT * FROM content WHERE `cid` = \"" .$user_id. "\""; $row2 = mysql_fetch_array(mysql_query($sql2)); Then on the pages, I just do a <?php echo $row2['content']; ?> where ever something is supposed to be. Is this an efficient way? Thanks! I'm trying to "rust off" my PHP knowledge since it has been a while... I'm picking up on a project I quit on a while ago. [N]F |
|
|
|
Feb 13 2007, 05:17 AM
Post
#2
|
|
|
Premium Member Group: [HOSTED] Posts: 495 Joined: 5-November 06 Member No.: 17,016 |
I'm working on a script where there is a custom user profile and I was wondering if there was a more efficient way to grab data stored in a database than this method: CODE $sql = "SELECT * FROM users WHERE `access_name` = \"" .$active_user. "\""; $row = mysql_fetch_array(mysql_query($sql)); //Link the two tables together; grab the most common thing that is the *SAME* $user_id = $row['id']; $sql2 = "SELECT * FROM content WHERE `cid` = \"" .$user_id. "\""; $row2 = mysql_fetch_array(mysql_query($sql2)); Then on the pages, I just do a <?php echo $row2['content']; ?> where ever something is supposed to be. Is this an efficient way? Thanks! I'm trying to "rust off" my PHP knowledge since it has been a while... I'm picking up on a project I quit on a while ago. [N]F You should consider using INNER JOIN Something like this CODE $sql = "SELECT users.*, content.* FROM users INNER JOIN content ON content.cid = users.id WHERE users.access_name = \"" .$active_user. "\""; $result = mysql_query($sql); while ($row = mysql_fetch_array($result)) { echo $row['content']; } 1. I prefer to split the query into multiple lines, easier to read and manage 2. You should be using mysql_query on a separate line, that way, you can do a check on the mysql_fetch_array or use a while loop, to prevent error in case your contents table contain nothing. 3. INNER JOIN only works if you do not need anything else from the users table, other than to get the id to link with the content table. Other wise you'll have to do it the same way as you did before. 4. Try to return only those column that you need, instead of * everything |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 11th October 2008 - 05:10 AM |