nightfox1405241487 0 Report post Posted February 13, 2007 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: $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 Share this post Link to post Share on other sites
faulty.lee 0 Report post Posted February 13, 2007 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: $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 JOINSomething like this$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 manage2. 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 Share this post Link to post Share on other sites