Display content of a file

I want to display the content of a file which is stored in a database. I put a read button next to each column and if you click on this button you should be able to read the content of the stored file on a new page.

Here’s the code.

<?php if (isset($_POST['Read'])) { mysql_connect("localhost","root","") or die ("Connection failed"); mysql_select_db("txt_archive") or die ("Connection failed"); $sql2=("SELECT location FROM files") or die ("Fájl nem található."); $myData = mysql_query ($sql2,$con); $content = file_get_contents($sql2); print ($content); }; ?>

[php]

<?php if (isset($_POST['Read'])) { mysql_connect("localhost","root","") or die ("Connection failed"); mysql_select_db("txt_archive") or die ("Connection failed"); $sql2 = "SELECT location FROM files"; $myData = mysql_query ($sql2,$con) or die ("Fájl nem található."); while ($result = $myData->fetch_assoc()){ $loc = $result["location"]; $content = file_get_contents($loc); print ($content); } }; ?>

[/php]

I got this message error:

Fatal error: Call to a member function fetch_assoc() on a non-object in C:\xampp\htdocs\txtupload\read.php on line 11

I managed to display the content of the file. The problem now is that it display the content of all the files that are in the database, but I want only the content of that file, where I clicked to the read button next to it.

ad a where variable to your sql statement that specifies the file you want.

What should I exactly write there?

something link
[php] $sql2 = “SELECT location FROM files WHERE PID =’”.$_POST[‘Read’]."’"; //in this case PID is your primary identifier for each entry in the database. assumeing thats what your selecting when you hit the read button.
[/php]

Unfortunately this doesn’t work because $_POST[‘Read’] has the value read.

How could I store the value of the auto-increment id?

in your click button for “read” you should set the value to the PID/Automatic ID because it is a unique field. You can query the value when you query the names.

Can you help me how to do it exactly?

when you do your sql query get your PID as well. then its something like

Name of file

then when you click submit you get the pid when you call the value read in the $_POST.
Here is something I have written that does and edit command based on a PID mine is $row[‘id’]
[php] while($row = mysql_fetch_array($result)) {
$count ++;
echo “

”;
echo “”.$count."";
echo “<a href=“selentry.php?sel_id=”.$row[‘id’].”">".$row[‘name’]."";
echo “<form align=“center”><input type=“button” onClick=“location.href='update.php?sel_id=”.$row[‘id’].”’" value=“Edit”>";
echo “”;
}
[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service