How do I display a single entry from an array from MYSQL

This works ...
$result = mysqli_query($conn, "SELECT * from $table");
$pix = array();
while ($row = $result->fetch_assoc()) {
$pix[] = $row;
}
foreach ($pix as $pic) {
echo "PID: {$pic['PID']}<br>"
. "Title: {$pic['title']}<br>"
. "Filename: {$pic['filename']}<br><br>";
}
$result = mysqli_query($conn, "SELECT * from $table");
$pix = array();
while ($row = $result->fetch_assoc()) {
$pix[] = $row;
}
foreach ($pix as $pic) {
echo "PID: {$pic['PID']}<br>"
. "Title: {$pic['title']}<br>"
. "Filename: {$pic['filename']}<br><br>";
}
How do I display just the entry with PID value of "5" from the array?
19 Replies
Jochem
Jochem•2y ago
SELECT * FROM table_name WHERE PID = 5;
SELECT * FROM table_name WHERE PID = 5;
alternatively if you really have to do it in PHP:
foreach ($pix as $pic) {
if ($pic['PID'] != 5) continue;

echo "PID: {$pic['PID']}<br>"
. "Title: {$pic['title']}<br>"
. "Filename: {$pic['filename']}<br><br>";
}
foreach ($pix as $pic) {
if ($pic['PID'] != 5) continue;

echo "PID: {$pic['PID']}<br>"
. "Title: {$pic['title']}<br>"
. "Filename: {$pic['filename']}<br><br>";
}
Blackwolf
BlackwolfOP•2y ago
I need the whole database in the array I am then dynamically changing data on the screen
Jochem
Jochem•2y ago
the if-statement I added would leave the array alone, but only print out info if $pic['PID'] is 5
Blackwolf
BlackwolfOP•2y ago
oh right, i thought there would be an easier way like echo $pix[5, 'PID']; 5 being the 5th entry in the array
Jochem
Jochem•2y ago
you can't guarantee the one with PID 5 will always be the fifth one though
Blackwolf
BlackwolfOP•2y ago
so $pix[5,'title'] would be the title of the 5th entry
Jochem
Jochem•2y ago
if all you want is the fifthsixth element, you can just do $pix[5]['PID'] though
Blackwolf
BlackwolfOP•2y ago
ahhhhh cool, it was getting the format correct 🙂
Jochem
Jochem•2y ago
and remember arrays are 0-indexed, so 0 is the first element just remember that there's no direct relation to a field in the database and the index of the array. If PID 2 is ever missing, all of a sudden you're getting the wrong element if you really need PID 5
Blackwolf
BlackwolfOP•2y ago
oh yea, but PID was an example of being specific i am listing titles in a <select> and when an option changes I want to display the elements for that opion option* when an option is selected, not changed
Jochem
Jochem•2y ago
well, the syntax is like I said if you want to access elements inside an array, and you can chain the square brackets to go deeper if you have deeper arrays 🙂
Blackwolf
BlackwolfOP•2y ago
Thank you kindly sir, that works a treat 🙂
Jochem
Jochem•2y ago
no worries 🙂
Blackwolf
BlackwolfOP•2y ago
if I know the title, can I display the corresponding data? so if I have a picture called "Scream" ... would $pix['Scream']['filename'] work
Jochem
Jochem•2y ago
oh wait, you think $pix[5]['PID'] filters the array and finds a record that has 5 as value for the PID? what it's doing is accessing the $pix array, taking the sixth (starting from 0, 5 is the sixth element) element, and returning that. ['PID'] then gives you the value for that element's PID key so no, $pix['Scream']['filename'] doesn't filter for elements with filename scream, it would try to take the 'Scream' element from the pix array, and then the filename element from that Scream array
Blackwolf
BlackwolfOP•2y ago
i understand that $pix[5]['PID'] is taking the 6 line of the array i was just wondering if i could use actualy data to find other data actual* so to find the date of when the picture "Scream" was posted for example
Jochem
Jochem•2y ago
the result from fetch_assoc would contain indexed rows, and the rows themselves would have named keys, so no, $pix['Scream'] wouldn't work without some manipulation of the result from mysql
Blackwolf
BlackwolfOP•2y ago
ok, i understand, thank you so my best bet is to simply put the array value in the <option> and use that 😉 so this works beautifully, thank you so much 🙂
// load whole db into array
$result = mysqli_query($conn, "SELECT * from $table ORDER BY title");
$pix = array();
while ($row = $result->fetch_assoc()) {
$pix[] = $row;
}
// echo $pix[5]['title'];


// create <select> options
$result = mysqli_query($conn, "SELECT * from $table ORDER BY title");
if ($result->num_rows > 0) {
$count = "0";
while ($row = $result->fetch_assoc()) {
echo '
<option value="' . $count . '">' . $row['title'] . '</option>';
$count++;
}
}
$conn->close();
?>
</select>
<?php
echo $pix[5]['title'];
?>
// load whole db into array
$result = mysqli_query($conn, "SELECT * from $table ORDER BY title");
$pix = array();
while ($row = $result->fetch_assoc()) {
$pix[] = $row;
}
// echo $pix[5]['title'];


// create <select> options
$result = mysqli_query($conn, "SELECT * from $table ORDER BY title");
if ($result->num_rows > 0) {
$count = "0";
while ($row = $result->fetch_assoc()) {
echo '
<option value="' . $count . '">' . $row['title'] . '</option>';
$count++;
}
}
$conn->close();
?>
</select>
<?php
echo $pix[5]['title'];
?>
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server