You could mash it into two lines.  Though I think the verbose syntax is more 
readable.

mysql_fetch_array( mysql_query("SELECT uid FROM users WHERE
 users.username='".mysql_real_escape_string($username)."'") );
$u = $uid['uid'];

However do you really think that 4 lines is too much to make a (possible) 
network call to an external resource, ask it to parse a statement and retrieve 
a specific piece of data, then return it to you and assign it to a variable?

For one, that's what functions are for, write it once then call your function!

Two, you should look at how much code is already hidden from you in those few 
functions! =P

Regards,
Gavin Towey



-----Original Message-----
From: Brent Baisley [mailto:brentt...@gmail.com]
Sent: Friday, September 04, 2009 6:21 AM
To: AndrewJames
Cc: mysql@lists.mysql.com
Subject: Re: a better way, code technique?

You should store the current user id in a session variable. Then you
don't have to hit the database at all.

There really is no short way of doing it. Normally you would create a
function (i.e. runQuery) that you pass the query too. Then it handles
running the query, fetching the data, error checking, etc. That way
you don't have to rewrite the same lines every time you want to run a
query.

Brent Baisley

On Fri, Sep 4, 2009 at 6:51 AM, AndrewJames<andrewhu...@gmail.com> wrote:
> is there a better way (hopefully simpler) to code this?
>
> i want to get the user id of the logged in user to use in my next statement.
>
> $q1 = sprintf("SELECT uid FROM users WHERE users.username='$username'");
> $result1 = mysql_query($q1);
> $uid = mysql_fetch_array($result1);
> $u = $uid['uid'];
>
> it seems like a long way around to get 1 bit of data??
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe:    http://lists.mysql.com/mysql?unsub=brentt...@gmail.com
>
>

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/mysql?unsub=gto...@ffn.com


The information contained in this transmission may contain privileged and 
confidential information. It is intended only for the use of the person(s) 
named above. If you are not the intended recipient, you are hereby notified 
that any review, dissemination, distribution or duplication of this 
communication is strictly prohibited. If you are not the intended recipient, 
please contact the sender by reply email and destroy all copies of the original 
message.

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/mysql?unsub=arch...@jab.org

Reply via email to