Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-03-2009, 04:29 AM   PM User | #1
wonderblunder
New to the CF scene

 
Join Date: Jan 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
wonderblunder is an unknown quantity at this point
In writing a query Function.. Appreciate you help

Hello Everyone,

I am new to PHP, I am trying to write a small query function which would would send in one argument for the query such as "LASTNAME".

Code:
function getInfo($n)
{
	$sql = "SELECT `user_id`, `first_name`, `last_name`, `age`
			FROM `users`
			WHERE `last_name` = " . $n;
			$result = mysql_query($sql);

}
I am unable to send the argument via function. If I just run a query straight up, I am able to see the information. I would highly appreciate your help on this.

Thanks a lot
wonderblunder is offline   Reply With Quote
Old 02-03-2009, 06:41 AM   PM User | #2
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
I'm assuming last_name is a string value, so you would want quotes around the value in the WHERE clause. By-the-by, if you have a primary key on user_id, and know the user_id to begin with, your query will run a lot faster if you use it for the lookup.
PHP Code:

function getInfo($n)
{
    
$sql "SELECT `user_id`, `first_name`, `last_name`, `age`
            FROM `users`
            WHERE `last_name` = '{$n}'"
;
            
$result mysql_query($sql);


Inigoesdr is offline   Reply With Quote
Old 02-03-2009, 09:20 AM   PM User | #3
JRM
New Coder

 
Join Date: Feb 2009
Location: Oxford
Posts: 31
Thanks: 0
Thanked 5 Times in 5 Posts
JRM is an unknown quantity at this point
not only faster but think about all the SMITH's.
JRM is offline   Reply With Quote
Old 02-03-2009, 11:11 AM   PM User | #4
hinch
Regular Coder

 
hinch's Avatar
 
Join Date: Sep 2005
Location: UK
Posts: 921
Thanks: 25
Thanked 79 Times in 79 Posts
hinch is on a distinguished road
PHP Code:
function getInfo($n)
{
    
$sql "SELECT `user_id`, `first_name`, `last_name`, `age`
            FROM `users`
            WHERE `last_name` = '"
.$n."'";
            
$result mysql_query($sql);
             return 
mysql_fetch_row($result);

then to call it

PHP Code:
$inforow getInfo('LASTNAME'); 

then just handle $inforow as you normally would
__________________
A programmer is just a tool which converts caffeine into code

My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
My hobby: http://www.angel-computers.co.uk
My life: http://www.furious-angels.com
hinch is offline   Reply With Quote
Old 02-03-2009, 03:58 PM   PM User | #5
wonderblunder
New to the CF scene

 
Join Date: Jan 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
wonderblunder is an unknown quantity at this point
Thanks you guys so much, appreciate your help.
wonderblunder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:15 PM.


Advertisement
Log in to turn off these ads.