I am trying use to mysqli to select an array of links from a mysql table.
What I want to do is put all of the selected links into an array, so I can then echo them later in a webpage. I want to index by number, like $image_link[0], or $image_link[7], etc.
In my query, $id is a foreign key in a table called images. The table contains images uploaded by many users.
So, the command works fine in mysql, the users primary ID is the foreign key and it correctly selects all the links for the user’s images. The row is called image_link.
I tried this code, I feel I am on the right track, but I cannot make it work. Can I have any pointers. I spent all day searching for the answer, but to no avail.
<?php
session_start();
$id = $_SESSION["id"];
$conn = mysqli_connect(
"localhost", "user", "password", "DB_name");
// Check connection
if (mysqli_connect_errno()) {
echo "Database connection failed.";
}
$query = "SELECT image_link FROM images WHERE userID='$id'";
//$query = "SELECT id, domain FROM services";
echo "test";
$result = $sql->query($query);
$data = [];
while ($row = $result->fetch_assoc()) {
$data[$row['image_link']] = $row;
}
mysqli_close($conn);
?>