mattward Posted August 16, 2013 Share Posted August 16, 2013 I just moved a PHP site from a WAMP server to a LAMP server, and everything works the same, except for my dropdowns are not populating. Here is a sample of the code I am using to pull data from the mysql database and populate the dropdowns: if($conn = dbConnect('username')) { $sql = "SELECT * FROM Sections ORDER BY SectionName"; $result = $conn->query($sql) or die(mysqli_error()); $numRows = $result->num_rows; } <select name="sections" style="width:200px;"> <?php foreach ($result as $row) { echo "<option value=" . $row['SectionID'] . ">" . $row['SectionName'] . "</option>"; } ?> </select> This code works perfectly in the WAMP environment, but not in the LAMP environment. I have echoed out $numRows, and it shows the correct row count for the result set, and when I click on the drop down, there are the appropriate number of rows, but the rows are blank. There is nothing there. I am really baffled by this. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/281252-array-row-count-is-right-but-cant-see-values/ Share on other sites More sharing options...
Psycho Posted August 16, 2013 Share Posted August 16, 2013 Have you verified that you are connecting to the database? You don't show that code. My assumption is that this condition is returning false if($conn = dbConnect('username')) Also, you say "dropdowns". If you have multiple select lists that you are doing this on you should definitely create a function. Quote Link to comment https://forums.phpfreaks.com/topic/281252-array-row-count-is-right-but-cant-see-values/#findComment-1445380 Share on other sites More sharing options...
Barand Posted August 16, 2013 Share Posted August 16, 2013 Can you post the output from this echo '<pre>',print_r($result, true),'</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/281252-array-row-count-is-right-but-cant-see-values/#findComment-1445381 Share on other sites More sharing options...
mattward Posted August 16, 2013 Author Share Posted August 16, 2013 Thanks for your replies. Psycho, I have verified that the connection to the database is working. Barand, here is the output: mysqli_result Object([current_field] => 0[field_count] => 5[lengths] => Array([0] => 1[1] => 14[2] => 3[3] => 1[4] => 23)[num_rows] => 6[type] => 0) Quote Link to comment https://forums.phpfreaks.com/topic/281252-array-row-count-is-right-but-cant-see-values/#findComment-1445414 Share on other sites More sharing options...
Solution Barand Posted August 16, 2013 Solution Share Posted August 16, 2013 I don't see any rows of data in there, do you? try while ($row = $result->fetch_assoc()) { echo "<option value=" . $row['SectionID'] . ">" . $row['SectionName'] . "</option>"; } I must confess to being puzzled about it working previously. Quote Link to comment https://forums.phpfreaks.com/topic/281252-array-row-count-is-right-but-cant-see-values/#findComment-1445417 Share on other sites More sharing options...
mattward Posted August 16, 2013 Author Share Posted August 16, 2013 This is the output I get when I run it on the WAMP server. Code is the exact same in both instances: mysqli_result Object([current_field] => 0[field_count] => 5[lengths] =>[num_rows] => 6[type] => 0 ) In the WAMP server, the drop down populates as it should. Quote Link to comment https://forums.phpfreaks.com/topic/281252-array-row-count-is-right-but-cant-see-values/#findComment-1445419 Share on other sites More sharing options...
mattward Posted August 16, 2013 Author Share Posted August 16, 2013 Well, the while loop worked. Don't ask me why or how, but for some reason, replacing the foreach loop with the while loop worked. Thanks for the replies! (And if anyone has any ideas as to why the foreach loop wasn't working, please post them! I am still really baffled by it, and would like to figure it out.) Quote Link to comment https://forums.phpfreaks.com/topic/281252-array-row-count-is-right-but-cant-see-values/#findComment-1445420 Share on other sites More sharing options...
hakimserwa Posted August 16, 2013 Share Posted August 16, 2013 I think the reason why the for each did not work is because you had not done a fetch (either fetch_array or other) on your sql. Quote Link to comment https://forums.phpfreaks.com/topic/281252-array-row-count-is-right-but-cant-see-values/#findComment-1445422 Share on other sites More sharing options...
Barand Posted August 16, 2013 Share Posted August 16, 2013 (And if anyone has any ideas as to why the foreach loop wasn't working, please post them! I am still really baffled by it, and would like to figure it out.) The foreach didn't work because $result (as shown by the print_r() ) was not an array of row data, it was a mysqli result object Quote Link to comment https://forums.phpfreaks.com/topic/281252-array-row-count-is-right-but-cant-see-values/#findComment-1445425 Share on other sites More sharing options...
mattward Posted August 16, 2013 Author Share Posted August 16, 2013 The foreach didn't work because $result (as shown by the print_r() ) was not an array of row data, it was a mysqli result object That makes sense. What still doesn't make sense to me is why it DOES work on my WAMP server. Go figure. Quote Link to comment https://forums.phpfreaks.com/topic/281252-array-row-count-is-right-but-cant-see-values/#findComment-1445426 Share on other sites More sharing options...
Barand Posted August 16, 2013 Share Posted August 16, 2013 I'm using windows and it certainly wouldn't have worked on mine. Were you using mysqli on the WAMP server? Quote Link to comment https://forums.phpfreaks.com/topic/281252-array-row-count-is-right-but-cant-see-values/#findComment-1445430 Share on other sites More sharing options...
hakimserwa Posted August 16, 2013 Share Posted August 16, 2013 (edited) You used oop so the answers to you questions lies in the object valuable $result which in my case it should be a mysql resource. i dont think data was fetched and by the way how did you use the while loop? unless you mixed oop with procedural, it doesnt make sense whenb you talk about using while Edited August 16, 2013 by hakimserwa Quote Link to comment https://forums.phpfreaks.com/topic/281252-array-row-count-is-right-but-cant-see-values/#findComment-1445431 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.