homepage Welcome to WebmasterWorld Guest from 54.204.94.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Need help with PHP/MySQL Query
Trying to extract field values - not getting desired result
calvinmicklefinger




msg:3331877
 11:21 pm on May 5, 2007 (gmt 0)

Hello,

I have a table with multiple columns, with various or unknown names (The developer built it so I could add and remove fields as I needed).

I use the database with a password protected folder, which uses Apache basic protection (htaccess and htpasswd).

I wish to query the table to retrieve the field names and values for the current user.

I have started a SESSION and get the user name with ...

$usr = $_SERVER['PHP_AUTH_USER'];

I create a database connection ...

$link = mysql_connect('localhost', 'db_username', 'db_password');

I then get the field names and list the name and try to display that information ...

$fields = mysql_list_fields("site_members", "user_profile", $link);
$columns = mysql_num_fields($fields);

for ($i = 0; $i < $columns; $i++) {

$var1 = mysql_field_name($fields, $i);
$query = "SELECT * FROM user_profile WHERE userid = '$usr'";
$var2 = $query['$var1'];

echo "fieldname=" . "'" . $var1 . "'" . " fieldvalue=" . "'" . $var2 . "'" . "<br /> \n";
}

This is one of the results ...

fieldname='id' fieldvalue='S'
fieldname='userid' fieldvalue='S'
fieldname='Name' fieldvalue='S'
fieldname='Email' fieldvalue='S'
fieldname='Address' fieldvalue='S'
fieldname='Licenses' fieldvalue='S'

The fieldvalue='S' appears for all fields, where the fieldvalue for the userid field is 'kirkward' in the database.

Here is a repeat of the code, all together ...

$url = $_SERVER['REMOTE_USER'];
$usr = $_SERVER['PHP_AUTH_USER'];
$pwd = $_SERVER['PHP_AUTH_PW'];

$link = mysql_connect('localhost', 'db_username', 'db_password');

$fields = mysql_list_fields("site_members", "user_profile", $link);
$columns = mysql_num_fields($fields);

for ($i = 0; $i < $columns; $i++) {

$var1 = mysql_field_name($fields, $i);
$query = "SELECT * FROM user_profile WHERE userid = '$usr'";
$var2 = $query['$var1'];

echo "fieldname=" . "'" . $var1 . "'" . " fieldvalue=" . "'" . $var2 . "'" . "<br /> \n";

}

Any ideas what I need to do to display results such as ...

fieldname='userid' fieldvalue='kirkward'

Any help is greatly appreciated.

Thanks
Kirk

 

Birdman




msg:3332132
 1:36 pm on May 6, 2007 (gmt 0)

Hello,

Your problem is here:

$query = "SELECT * FROM user_profile WHERE userid = '$usr'";
$var2 = $query['$var1'];

You need to do this:

$var2 = mysql_query($query);

I believe you can acclomplish what you want in a simpler way, and only query the db one time:

$url = $_SERVER['REMOTE_USER'];
$usr = $_SERVER['PHP_AUTH_USER'];
$pwd = $_SERVER['PHP_AUTH_PW'];

$link = mysql_connect('localhost', 'db_username', 'db_password');
mysql_select_db('site_members');

$query = "SELECT * FROM user_profile WHERE userid = '$usr'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);

foreach ( $row as $key => $val ) {

echo "fieldname=" . "'" . $key . "'" . " fieldvalue=" . "'" . $val . "'" . "<br /> \n";

}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved