How to select a specific row in a table (MySQL)- Please help!!

Discussion in 'PHP' started by j0563, Jun 2, 2010.

  1. #1
    I have a table named "df_users_permissions" and each user has a row which includes information such as their name, etc.

    I am trying to find the "homefolder" for the current user. Each user is given a unique ID and each user has a different home folder, so I was thinking I could find the user's home folder by locating the row which matches the user's ID then seeing what the value for "homefolder" is in that row.

    Here is what I have, but it's not working:

    $homefolder = mysql_query("SELECT homefolder FROM df_users_permissions WHERE id= $docsuserid");
    PHP:
    Could someone give me a hand with this? Much appreciated!
     
    j0563, Jun 2, 2010 IP
  2. jsgt

    jsgt Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The actual query looks correct, but if you take a look at the documentation for mysql_query you'll see that it returns a resource, which you then feed into mysql_fetch_assoc, for example. So:

    
    $res = mysql_query( "SELECT..." );
    $fields = mysql_fetch_assoc( $res );
    $homefolder = $fields[ 'homefolder' ];
    
    PHP:
     
    jsgt, Jun 2, 2010 IP
  3. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Your SQL looks correct but you are not referencing the query correctly.

    http://php.net/manual/en/function.mysql-query.php

    Note the return value - (assuming success) it returns a resource. You need to then process the result set in that
    resource using either of these;

    http://www.php.net/manual/en/function.mysql-fetch-array.php
    http://php.net/manual/en/function.mysql-fetch-assoc.php

    I.E
    Something like;

    $sql = mysql_query("SELECT homefolder FROM df_users_permissions WHERE id= $docsuserid");
    
    if($sql === FALSE)
    	die(mysql_error());
    
    else
    	{
    	# Its only 1 record thats being returned, so no need to use a loop here
    	# You might want to check 1 record was returned using mysql_num_rows too....
    	$results = mysql_fetch_array($sql);
    	print $results[0];
    	}
    PHP:
     
    lukeg32, Jun 2, 2010 IP