List:General Discussion« Previous MessageNext Message »
From:Gavin Towey Date:September 4 2009 7:31pm
Subject:RE: a better way, code technique?
View as plain text  
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:brenttech@stripped]
Sent: Friday, September 04, 2009 6:21 AM
To: AndrewJames
Cc: mysql@stripped
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<andrewhudds@stripped> 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=1
>
>

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


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.
Thread
a better way, code technique?AndrewJames4 Sep
  • AW: a better way, code technique?Majk.Skoric4 Sep
    • AW: a better way, code technique?Majk.Skoric4 Sep
  • Re: a better way, code technique?Per Jessen4 Sep
    • AW: Re: a better way, code technique?Majk.Skoric4 Sep
      • Re: AW: Re: a better way, code technique?AndrewJames4 Sep
        • AW: AW: Re: a better way, code technique?Majk.Skoric4 Sep
  • Re: a better way, code technique?Brent Baisley4 Sep
    • RE: a better way, code technique?Gavin Towey4 Sep
    • RE: a better way, code technique?Daevid Vincent4 Sep