How to fetch specific data from MySQL database to my PHP table

0 votes

I want to get data from the MySQL database and put it in an a.php file with my table. I want to display the title, text, and attachment ($name) for the user to download in each row of my table. The issue is that every table entry in the my.php file that has a table displays a list of all attachments from the database when I try to display that. Therefore, I only want to display one attachment per database entry that shares the same ID as the row's title and text. 

This is my database table:

CREATE TABLE IF NOT EXISTS `table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(30) NOT NULL,
`text` varchar(30) NOT NULL,
`name` varchar(30) NOT NULL,
`type` varchar(30) NOT NULL,
`size` int(11) NOT NULL,
`content` longblob NOT NULL,
PRIMARY KEY (`id`)  )

My code:

<?php
include('config.php');
$sqlget="SELECT * FROM table ORDER BY timestamp DESC";
$sqldata= mysqli_query($dbcon, $sqlget) or die ('error');
echo"<table>";
while ($row=mysqli_fetch_array($sqldata,
MYSQLI_ASSOC)) {
echo "<tr><td>";
echo"<b><font color='#DF01A5'> Title: ".$row['title']."</font></b>";
echo "<br/>";
echo $row['text'];
echo "<br/>";
echo "<b><font color='#DF01A5'>Attachment: </font>";
?>
<?php
$con = mysql_connect('localhost', 'root', 'pass') or die(mysql_error());
$db = mysql_select_db('database', $con);
$query = "SELECT  id, name FROM table";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
} 
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<a href="download.php?id=<?php echo urlencode($id);?>"
><?php echo urlencode($name);?></a> <br>
<?php 
}
}
mysql_close();
echo"</table";
?>

Can someone help me with this?

Jul 28, 2022 in PHP by Kithuzzz
• 38,000 points
3,282 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In PHP

0 votes
0 answers

How to filter data from a MySQL Database Table with PHP

I'm attempting to code a search box ...READ MORE

Jul 22, 2022 in PHP by narikkadan
• 63,600 points
6,651 views
0 votes
1 answer

How to retrieve or obtain data from the MySQL database using PHP?

Hello kartik,  Actually there are many functions that  ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,840 points
3,357 views
0 votes
1 answer

What is Php Mysql Database? How php connect to database?

Hello kartik, MySQL is the most popular database ...READ MORE

answered Feb 25, 2020 in PHP by Dey
1,131 views
0 votes
1 answer

How to convert from MySQL datetime to another format with PHP?

Hello, To convert a date retrieved from MySQL ...READ MORE

answered May 19, 2020 in PHP by Niroj
• 82,840 points
3,519 views
0 votes
1 answer

How to export the MySql database structure without the data just the structure?

Hello @kartik, You can do with the --no-data option with ...READ MORE

answered Aug 18, 2020 in PHP by Niroj
• 82,840 points
2,616 views
0 votes
1 answer

How to see indexes for a database or table in MySQL?

Hello @kartik, To see the index for a ...READ MORE

answered Aug 18, 2020 in PHP by Niroj
• 82,840 points
150,486 views
0 votes
0 answers

Creating a search form in PHP [duplicate]

I am working on a function where ...READ MORE

Jun 9, 2022 in PHP by Kichu
• 19,040 points
415 views
0 votes
0 answers

How to delete image form folder and database

With the code below, I'm displaying data ...READ MORE

Jul 26, 2022 in PHP by Kithuzzz
• 38,000 points
1,337 views
0 votes
0 answers

How can I prevent SQL injection in PHP?

The programme  becomes vulnerable to SQL injection ...READ MORE

Jul 28, 2022 in PHP by Kithuzzz
• 38,000 points
441 views
0 votes
0 answers

php records navigation

Code: <?php $host='localhost'; $user='root'; $password='root'; $database='database'; $startindex=@$_REQUEST['seek']; $db=mysql_connect($host, ...READ MORE

Aug 8, 2022 in PHP by Kithuzzz
• 38,000 points
484 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP