Jump to content

Array row count is right, but can't see values


mattward

Recommended Posts

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?

 

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.

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
)

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.

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.

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

 (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

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

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.