Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/159344-unexpected-t_variable/
Share on other sites

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);
   ?>

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?

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?

<?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.

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);
?>

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);
?>

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

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!

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

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  :-\

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 :)

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.

 

 

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

@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);
?>

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.