Inserting a blob

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
NikBruce
Forum Newbie
Posts: 22
Joined: Wed Dec 23, 2009 5:16 am

Inserting a blob

Post by NikBruce »

Hi,

I've been trying to upload a file to a mysql table all day.

here is the table:

Code: Select all

 
$cv = "CREATE TABLE `$proName` (
`id`   Int Unsigned Not Null Auto_Increment,
`name` VarChar(255) Not Null Default 'Untitled.txt',
`mime` VarChar(50) Not Null Default 'text/plain',
`size` BigInt Unsigned Not Null Default 0,
`data` MediumBlob Not Null,
`createdDate` DateTime Not Null,
`createdBy` VarChar(50) Not Null,
PRIMARY KEY (`id`)
)";
 
and here is the insert:

Code: Select all

 
$ti = "INSERT INTO '$proName' (name, mime, size, data, createdDate, createdBy)
VALUES ('$name', '$mime', '$size', '$data', '$date', '$accName')";
 
$result = "mysql_query($ti, $con)";
 
if ($result)
{ echo "Success. "; }
else
{ echo "Fail. "; }
 
The table gets created just fine, but the insert query doesn't work. It echos "success," but the table remains empty.

I've tried writing the insert query a bunch of different ways, but nothing changes.

please help. thanks
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Inserting a blob

Post by AbraCadaver »

Ummm... Because you have quotes around the mysql_query() function. :roll:
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
NikBruce
Forum Newbie
Posts: 22
Joined: Wed Dec 23, 2009 5:16 am

Re: Inserting a blob

Post by NikBruce »

if the quotes aren't there it echos "Fail"
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Inserting a blob

Post by AbraCadaver »

NikBruce wrote:if the quotes aren't there it echos "Fail"
That's because it actually executes the query and it fails. With the quotes around the function call the function is never called. You have just assigned a string of characters to the $result var. You need to turn on error reporting to see why the query fails.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
NikBruce
Forum Newbie
Posts: 22
Joined: Wed Dec 23, 2009 5:16 am

Re: Inserting a blob

Post by NikBruce »

i used

Code: Select all

<?php error_reporting(-1); ?>
no errors appeared.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Inserting a blob

Post by AbraCadaver »

Code: Select all

$result = mysql_query($ti, $con) or die(mysql_error());
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
NikBruce
Forum Newbie
Posts: 22
Joined: Wed Dec 23, 2009 5:16 am

Re: Inserting a blob

Post by NikBruce »

thanks.

i got a,
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''EU Observatory' (name, mime, size, data, createdDate, createdBy) VALUES ('img3' at line 1
i guess i should have done that first.

Happy new year.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Inserting a blob

Post by AbraCadaver »

NikBruce wrote:thanks.

i got a,
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''EU Observatory' (name, mime, size, data, createdDate, createdBy) VALUES ('img3' at line 1
i guess i should have done that first.

Happy new year.
Yeah, EU Observatory is not a valid table name and also I don't think it will like quotes around it. Maybe backticks `
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
NikBruce
Forum Newbie
Posts: 22
Joined: Wed Dec 23, 2009 5:16 am

Re: Inserting a blob

Post by NikBruce »

the problem was that i hadn't run the variables through a real_escape_string.

Thanks for all your help.
Post Reply