Jump to content

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


mattward
Go to solution Solved by Barand,

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

 (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

Link to comment
Share on other sites

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 by hakimserwa
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.

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