Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql blob "cannot be null" error 1

Status
Not open for further replies.

frumpus

Programmer
Aug 1, 2005
113
US
When this runs on my php page mysql returns an error "column image_data cannot be null". However, I can copy and paste the query from the output of 'echo $dbQuery' and run that directly on the mysql server and it accepts it with no problems.

I have also tried this without 'addslashes' but get the same error, this time on the mysql server as well.

Code:
$fullPath = addslashes(realpath('upload/' . $image));
$dbQuery = "INSERT INTO LIT_REQ_IMAGES (FILENAME, WIDTH, HEIGHT, MIME_TYPE, IMAGE_DATA) VALUES('$image',$newWidth, $newHeight,'$imageMimeType',LOAD_FILE('$fullPath'))";
echo $dbQuery;
mysql_query($dbQuery);
echo mysql_error();

any idea what is going on here?

the output of the echo statement is

INSERT INTO LIT_REQ_IMAGES (FILENAME, WIDTH, HEIGHT, MIME_TYPE, IMAGE_DATA) VALUES('testfile.png',171, 300,'image/png',LOAD_FILE('C:\\Inetpub\\vhosts\\mydomain.net\\httpdocs\\lit_request\\upload\\testfile.png'))

The path and file are valid (as I said this works fine when the query is run on the server.)
 
the load_file command takes a filename that is valid for the SERVER, not the calling client. so the file in question must exist at the uri on the server.

from your post, it seems that your client and server are running on separate physical or virtual systems.

addslashes is almost certainly confusing things too.

I would do this instead (although I would not normally save binary data into a database)

Code:
$contents = mysql_real_escape_string(file_get_contents(  realpath('upload/' . $image)));
$dbQuery = "INSERT INTO LIT_REQ_IMAGES (FILENAME, WIDTH, HEIGHT, MIME_TYPE, IMAGE_DATA) VALUES ('$image',  $newWidth, $newHeight,'$imageMimeType', '$contents')";
echo $dbQuery;
$result = mysql_query($dbQuery);
if (!$result) die(mysql_error());
 
They are running on the same server and the file does exists in the path given, (otherwise copy/pasting the output of the echo statement into sql server would not work.)

I will give your code a try, thanks!
 
Getting the contents of the file did the trick, thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top