Judit1983-2 Posted July 12, 2012 Share Posted July 12, 2012 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. <?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); } }; I got the following error message: Fatal error: Call to a member function fetch_assoc() on a non-object in C:\xampp\htdocs\txtupload\read.php on line 11 Has anybody any idea what's wrong with my code? mod edit: add tags Quote Link to comment https://forums.phpfreaks.com/topic/265576-display-content-of-a-file/ Share on other sites More sharing options...
scootstah Posted July 12, 2012 Share Posted July 12, 2012 You are using mysqli syntax with the mysql driver. It should be: while ($result = mysql_fetch_assoc($myData)){ Quote Link to comment https://forums.phpfreaks.com/topic/265576-display-content-of-a-file/#findComment-1361115 Share on other sites More sharing options...
Judit1983-2 Posted July 12, 2012 Author Share Posted July 12, 2012 Thankx for your help. It is not perfect yet, because it displays the content of all uploaded file, but I want only the file where I click on the Read button next to it. Quote Link to comment https://forums.phpfreaks.com/topic/265576-display-content-of-a-file/#findComment-1361124 Share on other sites More sharing options...
scootstah Posted July 12, 2012 Share Posted July 12, 2012 Then you'll probably want to attach an ID to the button, or a hidden form element or something to use in a WHERE clause. Quote Link to comment https://forums.phpfreaks.com/topic/265576-display-content-of-a-file/#findComment-1361130 Share on other sites More sharing options...
Judit1983-2 Posted July 12, 2012 Author Share Posted July 12, 2012 I use an auto-increment ID in the database and save the location next to it. I open the file from the location column of the database. I put here the index.php file as well and please help me what to write in the WHERE clause. (Sorry some messages in the code are in Hungarian) <html> <head> <body> <?php ini_set("memory_limit","128M"); ini_set("post_max_size","64M"); ini_set("upload_max-filesize","64M"); if(isset($_POST['ok'])) { if($_FILES['fajl']['name']!="") { if($_FILES['fajl']['type']=="text/plain") { if($_FILES['fajl']['error']==0) { if(!is_dir("uploads")) { mkdir("uploads"); } $filename=basename($_FILES['fajl']['name']); $title=$_POST['title']; move_uploaded_file($_FILES['fajl']['tmp_name'],"uploads/".$filename); $location="uploads/".$filename; print $location; if(file_exists("uploads/".$filename)) { $error="Sikeres f?jlfelt?lt?s!"; $con = mysql_connect("localhost","root",""); if (!$con) { die ("Kapcsol?d?s nem siker?lt a kapcsol?d?s ehhez: " . mysql_error()); } mysql_real_escape_string($filename); mysql_real_escape_string($title); strip_tags($filename); strip_tags($title); mysql_select_db("txt_archive",$con); $sql = mysql_query("INSERT INTO files (id,location,title) VALUES ('','$location','$title')"); } else { $error="A F?jl felt?lt?se sikertelen!"; } } else { $error="Hiba a felt?lt?s sor?n: ".$_FILES['fajl']['error']; } } else { $error="Hib?s f?jlform?tum - csak TXT megengedett"; } } else { $error="Nem adott meg f?jlt!"; } } echo "Kor?bban felt?lt?tt fileok:"; mod edit: add tags Quote Link to comment https://forums.phpfreaks.com/topic/265576-display-content-of-a-file/#findComment-1361133 Share on other sites More sharing options...
Psycho Posted July 12, 2012 Share Posted July 12, 2012 OK, we need to take a step back. On the page that you create the buttons you need to pass the ID of the file when the button is clicked. You didn't show the code used to create the buttons, but each button should be inside it's own form with a hidden field for the file id. Then on the page that you display the content you need to use the passed value of the ID to ONLY get the data for the selected file. Right now you are getting all the files and using a WHILE loop to display all of them. So, something like this $file_id = isset($_POST['file_id']) ? (int) isset($_POST['file_id']) : false; if ($file_id) { mysql_connect("localhost","root","") or die ("Connection failed"); mysql_select_db("txt_archive") or die ("Connection failed"); $sql2 = "SELECT location FROM files WHERE id = $file_id LIMIT 1"; $myData = mysql_query ($sql2, $con) or die ("F?jl nem tal?lhat?."); if(!mysql_num_rows($myData)) { echo "No file matching your request."; } else { $result = $myData->fetch_assoc(); $loc = $result["location"]; $content = file_get_contents($loc); print ($content); } }; Not tested Quote Link to comment https://forums.phpfreaks.com/topic/265576-display-content-of-a-file/#findComment-1361135 Share on other sites More sharing options...
PFMaBiSmAd Posted July 12, 2012 Share Posted July 12, 2012 Please use the forum's bbcode tags (the edit form's # button) when posting code. Quote Link to comment https://forums.phpfreaks.com/topic/265576-display-content-of-a-file/#findComment-1361137 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.