Jump to content

Need help correcting query


Recommended Posts

Hello,

 

I am running MySQL 4.1.9 on an Apache/RedHatLinux server and am using the following code, which crashes when I introduce the SELECT query (I've marked it with a comment line above).  Can anyone tell me what I need to change?  (P.S. - You can probably tell that I'm a newbie, and this is a self education exercise.)  ...

<?php
$usr = $_SERVER['PHP_AUTH_USER'];
$link = mysql_connect('mysql_host', 'mysql_dbusername', 'mysql_dbpassword');
$fields = mysql_list_fields("my_database", "my_table", $link);
$columns = mysql_num_fields($fields);

for ($i = 0; $i < $columns; $i++) {
    	$var1 = mysql_field_name($fields, $i);
// Page crashes when I introduce this line
$var2 = SELECT $var1 FROM my_table WHERE my_fieldname = "'" . $usr . "'";
echo "<input type='hidden' name=" . "'" . $var1 . "'" . " value=" . "'" . $var2  . "'" . "><br />";
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/50134-need-help-correcting-query/
Share on other sites

normally queries are written in the following form in PHP

 

$var2 = "SELECT '$var1' FROM my_table WHERE my_fieldname = '$usr'";
$var2_result = mysql_query($var2) or die("Error in query" . mysql_wrror());
$var2_row = mysql_fetch_row($var2_result);
$var3 = $var2_row[$var1];
echo "<input type='hidden' name=" . "'" . $var1 . "'" . " value=" . "'" . $var3  . "'" . "><br />";

Paul,

 

I tried the code you supplied (but, changing the spelling of mysql_wrror() to mysql_error()).

 

I get a result that does not provide a value to $var3

 

This is one of the results ...

<input type='hidden' name='userid' value=''>

 

I was expecting a value.

 

Did I missread?

 

Thanks,

Kirk

According to the PHP manual mysql_let_fields is deprecated - use mysql_query("SHOW COLUMNS FROM table"); instead, or simply DESC table, something like this:

 

<?
print "</PRE>";

$result = mysql_query("DESC table");

while ($record = mysql_fetch_assoc($result)) 
{
     print $record['Field]."\n"; // prints a column name
}

print "<PRE>";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.