illusive101 Posted May 23, 2009 Share Posted May 23, 2009 Hey guys, trying to load some images from a database and kind of new to the whole PHP / MYSQL thing. Here's a code snippet and I keep getting the unexpected T_VARIABLE error on page load. <?php include 'connect.php' $self = $_SERVER['PHP_SELF']; $query = "SELECT id, img_name, img_type, img_size, img_data FROM img_tbl ORDER BY id"; $result = mysql_query($query) or die(’Error, query failed’); while($row = mysql_fetch_array($result)){ echo “<img src=\”viewimage.php?id=$row[id]\” width=\”55\” height=\”55\” /> <br/>”; } mysql_close($dbconn); ?> Any help is appreciated, thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/ Share on other sites More sharing options...
MadTechie Posted May 23, 2009 Share Posted May 23, 2009 try this <?php include 'connect.php' $self = $_SERVER['PHP_SELF']; $query = "SELECT id, img_name, img_type, img_size, img_data FROM img_tbl ORDER BY id"; $result = mysql_query($query) or die('Error, query failed'); while($row = mysql_fetch_array($result)){ echo "<img src=\"viewimage.php?id=$row['id']\" width="55" height="55" /> <br/>"; } mysql_close($dbconn); ?> Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840452 Share on other sites More sharing options...
DarkSuperHero Posted May 23, 2009 Share Posted May 23, 2009 include 'connect.php'; $self = $_SERVER['PHP_SELF']; $query = "SELECT id, img_name, img_type, img_size, img_data FROM img_tbl ORDER BY id"; $result = mysql_query($query) or die(’Error, query failed’); while($row = mysql_fetch_array($result)){ echo “<img src=”viewimage.php?id=$row[id]” width=”55” height=”55” /> <br/>”; } mysql_close($dbconn); wasnt he just missing the ; after his include? Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840453 Share on other sites More sharing options...
illusive101 Posted May 23, 2009 Author Share Posted May 23, 2009 Update :: I used the method http://www.techcubetalk.com/2009/01/tutorial-on-how-to-store-images-in-mysql-blob-field/ found there to store my images and show them. However, I got all the code in place but no images show? The database gets larger as I upload them so I know their uploading correct, however they are not displaying on the page I'd like them to. Anyone know why possibly? Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840454 Share on other sites More sharing options...
MadTechie Posted May 23, 2009 Share Posted May 23, 2009 view the page "viewimage.php?id=1" in your browser if no image appears double check your code for viewimage.php Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840455 Share on other sites More sharing options...
illusive101 Posted May 23, 2009 Author Share Posted May 23, 2009 The requested URL /viewimage.php was not found on this server. Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840461 Share on other sites More sharing options...
illusive101 Posted May 23, 2009 Author Share Posted May 23, 2009 <?php include 'connect.php'; $self = $_SERVER['PHP_SELF']; $query = "SELECT id, img_name, img_type, img_size, img_data FROM img_tbl ORDER BY id"; $result = mysql_query($query) or die(’Error, query failed’); while($row = mysql_fetch_array($result)){ echo “<img src=\”viewimage.php?id=$row[id]\” width=\”55\” height=\”55\” /> <br/>”; } mysql_close($dbconn); ?> There's the code again, I don't see anything wrong :S The connect.php is just fine too. Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840465 Share on other sites More sharing options...
illusive101 Posted May 23, 2009 Author Share Posted May 23, 2009 Okay, I got a little bit further. The viewimage.php loads, but it's completely white -- there's no error nor is there an image. ...? Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840471 Share on other sites More sharing options...
illusive101 Posted May 23, 2009 Author Share Posted May 23, 2009 viewimage.php?id=1 brings up a blank image also, which is weird because it's in the database :S Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840484 Share on other sites More sharing options...
Ken2k7 Posted May 23, 2009 Share Posted May 23, 2009 Why do the quotes look funny in the echo? If this doesn't work, check your PHP file. Is it sending out the correct data headers? <?php include 'connect.php'; $self = $_SERVER['PHP_SELF']; $query = "SELECT id, img_name, img_type, img_size, img_data FROM img_tbl ORDER BY id"; $result = mysql_query($query) or die(’Error, query failed’); while($row = mysql_fetch_array($result)){ echo '<img src="viewimage.php?id='.$row['id'].'" width="55" height="55" /> <br/>'; } mysql_close($dbconn); ?> Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840487 Share on other sites More sharing options...
illusive101 Posted May 23, 2009 Author Share Posted May 23, 2009 Viewimages.php <? if(isset($_REQUEST['id'])) { // get the file with the id from database include 'connect.php'; $id = $_REQUEST['id']; $query = "SELECT 'img_name', 'img_type', 'img_size', 'img_data' FROM img_tbl WHERE id = '$id'"; $result = mysql_query($query) or die(mysql_error()); list($name, $type, $size, $content) = mysql_fetch_array($result); header("Content-length: $fileSize"); header("Content-type: $fileType"); print $content; } ?> Showimages.php <?php include 'connect.php'; $self = $_SERVER['PHP_SELF']; $query = "SELECT id, img_name, img_type, img_size, img_data FROM img_tbl ORDER BY id"; $result = mysql_query($query) or die(’Error, query failed’); while($row = mysql_fetch_array($result)){ echo “<img src=\”viewimage.php?id=$row[id]\” width=\”55\” height=\”55\” /> <br/>”; } mysql_close($dbconn); ?> Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840498 Share on other sites More sharing options...
kickstart Posted May 23, 2009 Share Posted May 23, 2009 Hi Think the variables highlighted should match:- if(isset($_REQUEST['id'])) { // get the file with the id from database include 'connect.php'; $id = $_REQUEST['id']; $query = "SELECT 'img_name', 'img_type', 'img_size', 'img_data' FROM img_tbl WHERE id = '$id'"; $result = mysql_query($query) or die(mysql_error()); list($name, $type, $size, $content) = mysql_fetch_array($result); header("Content-length: $fileSize"); header("Content-type: $fileType"); print $content; } ?> All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840603 Share on other sites More sharing options...
illusive101 Posted May 23, 2009 Author Share Posted May 23, 2009 After changing them, still nothing has happened. The table's name is img_tbl and the fields are id , img_name, img_type, img_size, img_data. Would anyone be able to find me a working script to load images from this table? (Or figure out why my current one doesn't work?) Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840672 Share on other sites More sharing options...
MadTechie Posted May 23, 2009 Share Posted May 23, 2009 can you post your latest code Viewimages.php and Showimages.php Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840673 Share on other sites More sharing options...
GingerRobot Posted May 23, 2009 Share Posted May 23, 2009 Why do the quotes look funny in the echo? They appear to be MS 'smart' quotes. Somewhere along the line, something got typed in Word. In any case, this should really be over in php help Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840682 Share on other sites More sharing options...
kickstart Posted May 23, 2009 Share Posted May 23, 2009 Hi Just noticed that the column names have quotes around them rather than back ticks. if(isset($_REQUEST['id'])) { // get the file with the id from database include 'connect.php'; $id = $_REQUEST['id']; $query = "SELECT `img_name`, `img_type`, `img_size`, `img_data` FROM img_tbl WHERE id = '$id'"; $result = mysql_query($query) or die(mysql_error()); list($name, $type, $size, $content) = mysql_fetch_array($result); header("Content-length: $fileSize"); header("Content-type: $fileType"); print $content; } ?> All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840764 Share on other sites More sharing options...
illusive101 Posted May 23, 2009 Author Share Posted May 23, 2009 can you post your latest code Viewimages.php and Showimages.php showimages.php <?php include 'connect.php'; $self = $_SERVER['PHP_SELF']; $query = "SELECT id, img_name, img_type, img_size, img_data FROM img_tbl ORDER BY id"; $result = mysql_query($query) or die(’Error, query failed’); while($row = mysql_fetch_array($result)){ echo “<img src=”viewimage.php?id=$row[id]” width=”55” height=”55” /> <br/>”; } mysql_close($dbconn); ?> viewimages.php <? if(isset($_REQUEST['id'])) { // get the file with the id from database include 'connect.php'; $id = $_REQUEST['id']; $query = "SELECT 'img_name', 'img_type', 'img_size', 'img_data' FROM img_tbl WHERE id = '$id'"; $result = mysql_query($query) or die(mysql_error()); list($name, $type, $size, $content) = mysql_fetch_array($result); header("Content-length: $size"); header("Content-type: $type"); print $content; } ?> Still can't figure this out, and sorry for posting in wrong section :-\ Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840874 Share on other sites More sharing options...
chaking Posted May 23, 2009 Share Posted May 23, 2009 the viewimages.php file is using single quotes around the columns instead of back ticks singlequote: ' ... go around the strings... '$id' ... not needed for integers backtick: ` ... go around columns Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840875 Share on other sites More sharing options...
illusive101 Posted May 23, 2009 Author Share Posted May 23, 2009 Thanks guys for the heads up with the single quotes and back ticks, fixed the problem! Now how would I go about loading these images onto a separate page automatically when I upload them? So when I upload it, it automatically appears on that page. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840877 Share on other sites More sharing options...
kickstart Posted May 23, 2009 Share Posted May 23, 2009 Hi Not sure what you mean. You already appear to have a script to generate a page to display the images. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840882 Share on other sites More sharing options...
illusive101 Posted May 23, 2009 Author Share Posted May 23, 2009 Parse error: syntax error, unexpected '?' I get that error for line 8, however here is line 8... I don't see a ? <?php include 'connect.php' $query = ("SELECT `id`, `img_name`, `img_type`, `img_size`, `img_data` FROM img_tbl ORDER BY id"); $result = mysql_query($query) or die(’Error, query failed’); while($row = mysql_fetch_array($result)){ echo “<img src=\”viewimage.php?id=$row[id]\” width=\”55\” height=\”55\” /> <br/>”; } mysql_close($dbconn); ?> Nevermind, now I'm getting a T_Variable error again on the $query line. Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840893 Share on other sites More sharing options...
kickstart Posted May 23, 2009 Share Posted May 23, 2009 Hi You are missing the ; at the end of the include line above it. Not sure about the ? issue. However as mentioned by Ken2k7 the quotes in a few places look rather strange, both around the error message ’Error, query failed’ and also in the echo statement. Especially on the echo statement where you have strange double quotes to delimit the string, but also contained within the string.A guess but not sure if that may be confusing php. Try this:- <?php include 'connect.php' $query = ("SELECT `id`, `img_name`, `img_type`, `img_size`, `img_data` FROM img_tbl ORDER BY id"); $result = mysql_query($query) or die('Error, query failed'); while($row = mysql_fetch_array($result)){ echo "<img src='viewimage.php?id=$row[id]' width='55' height='55' /> <br/>"; } mysql_close($dbconn); ?> All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840897 Share on other sites More sharing options...
thebadbad Posted May 23, 2009 Share Posted May 23, 2009 @Keith - still missing a semicolon after the include. This should work (*fingers crossed*): <?php include 'connect.php'; $query = "SELECT `id`, `img_name`, `img_type`, `img_size`, `img_data` FROM `img_tbl` ORDER BY `id`"; $result = mysql_query($query) or die('Error, query failed'); while ($row = mysql_fetch_array($result)) { echo '<img src="viewimage.php?id=' . $row['id'] . '" width="55" height="55" /> <br />'; } mysql_close($dbconn); ?> Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840911 Share on other sites More sharing options...
illusive101 Posted May 23, 2009 Author Share Posted May 23, 2009 Anyone know why my upload only accepts .gif's? <label for="">Image:</label> <input type="file" name="imgfile"><br> // Loading images into Database if(isset($_REQUEST[submit]) && $_FILES[imgfile][size] > 0) { $fileName = $_FILES[imgfile][name]; // image file name $tmpName = $_FILES[imgfile][tmp_name]; // name of the temporary stored file name $fileSize = $_FILES[imgfile][size]; // size of the uploaded file $fileType = $_FILES[imgfile][type]; // file type $fp = fopen($tmpName, 'r'); // open a file handle of the temporary file $imgContent = fread($fp, filesize($tmpName)); // read the temp file fclose($fp); // close the file handle $query = "INSERT INTO img_tbl (`img_name`, `img_type`, `img_size`, `img_data` ) VALUES ('$fileName', '$fileType', '$fileSize', '$imgContent')"; mysql_query($query) or die('Error, query failed'); $imgid = mysql_insert_id(); // autoincrement id of the uploaded entry echo "<br>Image successfully uploaded to database<br>"; }else die('You have not selected any image'); // End loading image Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840924 Share on other sites More sharing options...
fantomel Posted May 23, 2009 Share Posted May 23, 2009 i've read the whole thread.. i'm gonna try to fix your code or look into it but since you are using mysql to store images my first question if would work with images i would ask somewhere or here on phpfreaks what would be the best solution for storing images for a gallery or something similar. and everyone would answer me the same thing a folder on hdd no database in the end my advice is use folders to store images and mysql to store their for outputting them on your site. P.S i think everyone will agreed with me Quote Link to comment https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/#findComment-840946 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.