mysql - Fetch separated array values from database in php
Get the solution ↓↓↓
I've database with a table 'history'
id title subtitle file
I want to fetch title, subtitle and file of that ID, to html input values.
{-code-2}
I need the output like when id=2 :
{-code-3}
Answer
Answer
Solution:
Looking at your values, you just have to loop and print them simultaneously with{-code-2} and{-code-3} index values like below:
<?php
foreach($rows as $row) {
$title = explode("*" , $row['title']);
{-code-2} = explode("*" , $row['subtitle']);
{-code-3} = explode("*" , $row['file']);
foreach($title as $index => $title_value){
echo 'Title:' ,$title_value,PHP_EOL;
echo 'Subtitle:',{-code-2}[ $index ],PHP_EOL;
echo 'File:',{-code-3}[ $index ],PHP_EOL;
echo " Answer
Solution:
When you explode your row data you have an array, so you can do this:
foreach($name as $key => $value){
echo 'name - '.$value;
if(isset($subtitle[$key])){
echo 'subtitle - '.$subtitle[$key];
}
if(isset($file [$key])){
echo 'file - '.$file [$key];
}
}
But if you are not sure that number if names is equal to numbers of subtitle and file, you can get the highest count of array elements and do it like that:
for($i = 0; $i < $highestCount; $i++){
...
if(isset($subtitle[$i])){
echo 'subtitle - '.$subtitle[$i];
}
....
}
Answer
Solution:
You can just try:
$id = $_GET['id'];
$get_history = mysqli_query($mysqli, "SELECT * FROM history WHERE id=".$id."");
$row = mysqli_fetch_assoc($get_history);
these three below will get the data that is exploded from the original data.
$title = explode("*" , $row['title']);
$subtitle = explode("*" , $row['subtitle']);
$file= explode("*" , $row['file']);
to display them:
Title : $title[0]
Subtitle: $subtitle[0]
File: $file[0]
Let me know if this works :)
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: npm err! code 1
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

