umguy Posted October 8, 2008 Share Posted October 8, 2008 Possible problem with mysql_fetch_arry() function. Problem: After insert into new record, query result set only retrieves first character of each field I am creating a database of material test data (i.e. temperature, strength, orientation, etc.). Each material may have multiple sets of tests assoiciated with it. MATERIAL TEST material_id -> material_id, test_id The following gives me essentially columns of individual test data with a SAVE button at the bottom of each column (HTML table related tags omitted for easier reading). I enter this test.php code (below) via a $_POST = $material_id variable from a search screen. All display correctly. <?php if (isset($HTTP_SESSION_VARS['material_id'])) { $material_id = $HTTP_SESSION_VARS['material_id']; } $material_id = $HTTP_POST_VARS['material_id']; $result = "SELECT * FROM test WHERE material_id = '$material_id'"; $result1 = mysql_query($result); $total_rows = mysql_num_rows($result1); while($row = mysql_fetch_array($result1, MYSQL_NUM)) { $test_id[$count_col] = $row[0]; $temperature[$count_col] = $row[1]; $orientation[$count_col] = $row[2]; $strength[$count_col] = $row[3]; etc... } if ($total_rows > 0) { $count_col = 0; while($count_col < $total_rows){ echo '<form method="post" action="save_test.php" name="save_test">'; echo '<input type="hidden" name="material_id" value="'.$material_id.'">'; echo <input type="text" name="temperature" value="'.$temperature[$count_col].'"> echo <input type="text" name="orientation" value="'.$orientation[$count_col].'"> echo <input type="text" name="strength" value="'.$strength[$count_col].'"> <input type="submit" value="Save Changes"></form> $count_col = $count_col + 1; } ?> The above form passes form field values to save_test.php, which updates each modified record without a problem. Then, at the bottom of the save_test.php file, I create a $_SESSION variable containing the value of $material_id and pass this back to test.php to display the results, testing either for the existence of $SESSION_VAR or $HTML_POST_VARS and set the $material_id variable accordingly upon reentering test.php. Here is where the problem occurs. The values obtained from the query truncate each value in the result set to only the first character, including the $test_id value which is needed for any further edits of a given record. The same occurs with new records. Upon inserting a new record into MySQL, when the system comes back to test.php, it displays the new record but all results have only one character, even though it accurately reflects the first digit of each field. I have hardcoded the $material_id value to the top of the test.php file and omitted any reference to $_POST or $_SESSION variables to make sure the correct value is in place. As such, the query is retrieving the correct records into the array, but only the first character. It doesn't matter what version of PHP or MySQL I am using since my test environment is using the latest build of both PHP and MySQL and my production environment is using a 3-4 year old version. One system is Windows and the other is Linux. I have also changed the field types from VARCHAR to FLOAT and that doesn't help either. Also, I have tried unsuccessfully to unset variables throughout the test.php file in hopes that it would reset it somehow and allow things to beging working as they do when you first enter the test.php module. Hopefully, I have explained this well enough to not burden anyone willing to assist with some advice. As are most people who post issues, I am at wits end and can usually figure things out on my own, in spite of being somewhere between a newbie and a novice. Does anyone have any ideas? Thanks in advance. DR (edited by kenrbnsn to add tags) Quote Link to comment Share on other sites More sharing options...
kazil Posted October 8, 2008 Share Posted October 8, 2008 while($row = mysql_fetch_array($result1, MYSQL_NUM)) { $test_id[$count_col] = $row[0]; $temperature[$count_col] = $row[1]; $orientation[$count_col] = $row[2]; $strength[$count_col] = $row[3]; etc... } Where did you defined $count_col? I see it only after the IF statement. Try adding a $count_col before this WHILE loop Quote Link to comment Share on other sites More sharing options...
umguy Posted October 9, 2008 Author Share Posted October 9, 2008 Thank you kindly for your reply. It's probably redundant, but I defined $count_col = 0 near the very top of the test.php file as well. Also, it is, in fact, before the while loop as you suggested, but within the IF statement. Not sure if you misread that or not. Quote Link to comment Share on other sites More sharing options...
philipolson Posted October 9, 2008 Share Posted October 9, 2008 This is too wordy for me to read, however, the problem sounds like a classic "string being treated as an array" problem... For example: <?php $notarray = 'abc'; echo $notarray[0]; // Prints 'a' ?> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 9, 2008 Share Posted October 9, 2008 Have you directly examined the data in the database. It is likely that only the first character was inserted into the table. Quote Link to comment 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.