Jump to content
xisto Community
nightfox1405241487

Proper Way To Grab User Data?

Recommended Posts

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

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 JOIN

Something 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 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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.