calvinmicklefinger Posted May 5, 2007 Share Posted May 5, 2007 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 />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50134-need-help-correcting-query/ Share on other sites More sharing options...
paul2463 Posted May 5, 2007 Share Posted May 5, 2007 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 />"; Quote Link to comment https://forums.phpfreaks.com/topic/50134-need-help-correcting-query/#findComment-246289 Share on other sites More sharing options...
calvinmicklefinger Posted May 5, 2007 Author Share Posted May 5, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/50134-need-help-correcting-query/#findComment-246315 Share on other sites More sharing options...
bubblegum.anarchy Posted May 5, 2007 Share Posted May 5, 2007 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/50134-need-help-correcting-query/#findComment-246377 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.