0

I am still a beginner with php and MySQL. I am having trouble getting rows from my database to display in an html select drop down box. I have researched it and it seems like my code should be good. The campaigns table as a row titled name. This is the row I am wanting to echo into the drop down. The drop down shows, however there is no content in it. Not sure what I am missing here...

Here is the code

<?php
$con=mysqli_connect("localhost","username","password","db_name");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$query = mysqli_query($con,"SELECT * FROM campaigns"); 

echo '<select name="campaignChange">';

while ($row = mysql_fetch_array($query)) {
    echo "<option value='" . $row['name'] ."'>" . $row['name'] ."</option>";
}

echo '</select>';

?>

1 Answer 1

1

You are mixing mysql and mysqli syntax.

You should change:

$query = mysql_query($con,"SELECT * FROM campaigns");

to:

$query = mysqli_query($con,"SELECT * FROM campaigns");

and:

while ($row = mysql_fetch_array($query)) {

to:

while ($row = mysqli_fetch_array($query)) {

By the way, you should add error handling. If you add this to the top:

mysqli_report(MYSQLI_REPORT_ALL);

mysqli will throw exceptions so you will always know what goes wrong exactly. As long as you use mysqli functions of course...

Sign up to request clarification or add additional context in comments.

3 Comments

Changed it to mysqli, still no luck.
My bad, I also changed the fetch_array to be mysqli_fetch_array. That did the trick, ty!!!
I will mark as answer as soon as it lets me. Thanks again :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.