-2

So instead of caption1, caption2, caption3 etc, how am I able to just pull the caption from a DB? Probably simple but I'm fairly new so I apologize.

<?php
    $captions = array(
    /*01*/ "caption1",
    /*02*/ "caption2",
    /*03*/ "caption3",
    /*04*/ "caption4",
    ); 

    for($n = 1; $n <= count($captions); $n++):
?>

EDIT: And yes I have a DB connected.

Here is my attempt at trying to retrieve the captions. Unfinished yet because I'm not to sure if I'm even going in the right direction.

<?php
            $query = mysql_query("SELECT * FROM images_tbl");
            $array = array();
            while($row = mysqli_fetch_assoc($array)){
            echo $row['caption'];
                        }


                $caption = array ( "");

                for($n = 1; $n <= count($caption); $n++):
            ?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Calvin M
  • 1
  • 3

1 Answers1

1

Adjust your code like this:

// establish database connection with your credentials
$connection = mysqli_connect(
    $host,
    $user,
    $password,
    $database
);

// query database
$result = mysqli_query(
    $connection,
    'SELECT caption FROM images_tbl'
);

$captions = [];

// check if querying succeeded
if (false !== $result) {
    // populate captions with values from database
    while ($row = mysqli_fetch_assoc($result)) {
        $captions[] = $row['caption'];
    }
}

Note Adjusted your query to select caption only instead of all columns, and consistently used mysqli instead of mysql.

For reference, see:

localheinz
  • 9,179
  • 2
  • 33
  • 44