Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    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);



  • #3
    JRM
    JRM is offline
    New Coder
    Join Date
    Feb 2009
    Location
    Oxford
    Posts
    31
    Thanks
    0
    Thanked 5 Times in 5 Posts
    not only faster but think about all the SMITH's.

  • #4
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    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

  • #5
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks you guys so much, appreciate your help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •