Question:
php mysql how to see uploaded image?
Mit Chauhan
2010-12-28 00:47:00 UTC
JAI MATADI ..

i m able to upload image into mysql using php code . .but when i m trying to fetch it by typing user's name .. m getting error ..

i have upload (folder) as a directory and through that i m uploading image into database ..

so in my database the image value looks like this "upload/imagename.gif"

here is my code .. through which i m trying to fetch the image ..

---------------------------------------
try.php
---------------

Enter the Name :




if(isset($_POST["submit"]))
{
$uname = $_POST["t1"];
mysql_connect("localhost","root","");
mysql_select_db("alldatabase");
$qry = "select * from signup where username='$uname' ";
if(!mysql_query($qry))
{
mysql_error();
}
$row = mysql_fetch_array($qry);
$path=$row['ppassportphoto'];
echo "";
}
?>
-------------------------------------------

m getting warning like this

mysql_fetch_array() expects parameter 1 to be resource, string given in C:\wamp\www\login and etc database\try2.php on line 25

where in line 25 the code is this

$row = mysql_fetch_array($qry);

please tell me wats wrong i m doing ..

tell me how to retrieve an image from mysql database ..
Four answers:
just "JR"
2010-12-28 01:49:03 UTC
You do not deal properly with the query, and there are syntax errors.

Here is the code, corrected:



$uname = $_POST["t1"];

$link = mysql_connect("localhost","root","");

mysql_select_db("alldatabase");

$qry = "select * from `signup` where username = '" . $uname . "' ";

$res = mysql_query($qry) or die (mysql_error());

$row = mysql_fetch_array($qry); // this is only correct if there is only ONE entry for that username.

$path=$row['ppassportphoto'];

echo "\"\"";

mysql_free_result($res);

mysql_close($link);



@auddrey: read the question: "How to retrieve an image from database". You do not answer it.

And your procedure is incorrect as well: you forgot to check if the file has been uploaded. (function "is_uploaded_file()")
auddrey_nw
2010-12-28 09:48:56 UTC
PHP Form Image Upload



This tutorial shows you how to upload an image on your server using PHP and html forms. You will also learn how to verify if the uploaded file is an image (checking the extension), if it doesn't overtakes a size limit and how to change the uploaded file name.



Important: You must set the enctype form atribute to "multipart/form-data", otherwise the form will not show you any errors, but it will just not upload your images!







Step 1: Create a folder named images located in the path you are planning to place the php script you are about to create. Make sure it has write rights for everybody or the scripts won't work ( it won't be able to upload the files into the directory).



Step 2: Paste the following code into a php file.



Please read carefuly the comments. All steps are explained there.




//define a maxim size for the uploaded images in Kb

define ("MAX_SIZE","100");



//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.

function getExtension($str) {

$i = strrpos($str,".");

if (!$i) { return ""; }

$l = strlen($str) - $i;

$ext = substr($str,$i+1,$l);

return $ext;

}



//This variable is used as a flag. The value is initialized with 0 (meaning no error found)

//and it will be changed to 1 if an errro occures.

//If the error occures the file will not be uploaded.

$errors=0;

//checks if the form has been submitted

if(isset($_POST['Submit']))

{

//reads the name of the file the user submitted for uploading

$image=$_FILES['image']['name'];

//if it is not empty

if ($image)

{

//get the original name of the file from the clients machine

$filename = stripslashes($_FILES['image']['name']);

//get the extension of the file in a lower case format

$extension = getExtension($filename);

$extension = strtolower($extension);

//if it is not a known extension, we will suppose it is an error and will not upload the file,

//otherwise we will do more tests

if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))

{

//print error message

echo '

Unknown extension!

';

$errors=1;

}

else

{

//get the size of the image in bytes

//$_FILES['image']['tmp_name'] is the temporary filename of the file

//in which the uploaded file was stored on the server

$size=filesize($_FILES['image']['tmp_name']);



//compare the size with the maxim size we defined and print error if bigger

if ($size > MAX_SIZE*1024)

{

echo '

You have exceeded the size limit!

';

$errors=1;

}



//we will give an unique name, for example the time in unix time format

$image_name=time().'.'.$extension;

//the new name will be containing the full path where will be stored (images folder)

$newname="images/".$image_name;

//we verify if the image has been uploaded, and print error instead

$copied = copy($_FILES['image']['tmp_name'], $newname);

if (!$copied)

{

echo '

Copy unsuccessfull!

';

$errors=1;

}}}}



//If no errors registred, print the success message

if(isset($_POST['Submit']) && !$errors)

{

echo "

File Uploaded Successfully! Try again!

";

}



?>















?
2010-12-28 09:56:16 UTC
because you don't get the result of the query



if(isset($_POST["submit"]))

{

$uname = $_POST["t1"];

mysql_connect("localhost","root","");

mysql_select_db("alldatabase");

$qry = "select * from signup where username='$uname' ";

if( !( $result=mysql_query($qry) ) ) // <------- $result get the query result

{

mysql_error();

}

$row = mysql_fetch_array($result); // <---- you must fetch the result instead of $qry





I advice you to try rainframework it implements a database class which simplify all of this to

$db=new mysql();

$db->connect();

$row = $db->getRow("your query");
?
2010-12-28 08:48:01 UTC
yes


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...