Jump to content

Need help correcting query

Recommended Posts



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.)  ...

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

Link to comment
Share on other sites



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?




Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • 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.