Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Proper Way To Grab User Data?
nightfox
post Feb 13 2007, 05:05 AM
Post #1


NiGHTFoX - Hiding in the dark
Group Icon

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
Go to the top of the page
 
+Quote Post
faulty.lee
post Feb 13 2007, 05:17 AM
Post #2


Premium Member
Group Icon

Group: [HOSTED]
Posts: 495
Joined: 5-November 06
Member No.: 17,016



QUOTE(nightfox @ Feb 13 2007, 01:05 PM) *

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
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Php, Sql Lite: Storing Session's Data?(1)
  2. Mini Apache Server W/ Php(5)
  3. Displaying Data From Mysql?(2)
  4. Php/mysql Data Display(3)
  5. Possible To Do?(7)
  6. Multilingual Site: Send The User To Page Of Choice(6)
  7. [PHP + MySQL] Encrypting Data(11)
  8. Generating A Table Into A File In CSV Format(6)
  9. Reading Data From Sessions(2)
  10. How To Reset The Server Variable Php_auth_user(9)
  11. User Authentication Session Handling Problems(14)
  12. Storing Data Into Xml With A Php Form(2)
  13. Need An Alternative To $http_post_data For PHP4(5)
  14. Data Passing - Re An Assignment For School - Please Help :)(8)
  15. Send XML Data To PHP Page(0)
  1. Backing Up User Forms As Static HTML(5)
  2. Retrieving Data And Displaying In Boxes(6)
  3. Sql Injection Prevention (passing Numerical Data Across Pages).(9)
  4. Getting Certain Parts Of A Record(17)
  5. Automated Product Suggestion Script(2)
  6. Letting Users Add Mysql Data With Php(1)
  7. Make A Script Run Even If No User Is Online(6)
  8. How To Create/edit/delete Ftp Accounts With Php(1)
  9. Reading Xml Data(2)


 



- Lo-Fi Version Time is now: 11th October 2008 - 05:10 AM