Welcome to WebmasterWorld Guest from 54.226.27.104

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Need help with PHP/MySQL Query

Trying to extract field values - not getting desired result

     

calvinmicklefinger

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

10+ Year Member



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

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

WebmasterWorld Senior Member 10+ Year Member



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";

}

 

Featured Threads

Hot Threads This Week

Hot Threads This Month