473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

update mysql blob with another blob using php

Hi,

I have a blob field in a mysql database table. I want to copy a blob
from one record to another. I am having trouble transferring the data
via a php variable. Maybe I need to addslashes or convert to Hex or
something. I've tried a few things but can't quite get it. Here is
simplified code.

mysql_select_db($dbname, $connection);

$query = "SELECT blobthing
FROM mytable
WHERE id = 1;";

$results = mysql_query ($query, $connection);
$row = (mysql_fetch_assoc($result));
$varblobthing = $row['blobthing']; //put the blob in a variable

$query = "UPDATE mytable
SET blobthing = ".$varblobthing.",
WHERE id = 2;";

mysql_query ($query, $connection);
I would love any advice I can get. Thanks in advance.
Simon

Sep 10 '05 #1
7 11414
sime said the following on 10/09/2005 18:36:
Hi,

I have a blob field in a mysql database table. I want to copy a blob
from one record to another. I am having trouble transferring the data
via a php variable. Maybe I need to addslashes or convert to Hex or
something. I've tried a few things but can't quite get it. Here is
simplified code.

mysql_select_db($dbname, $connection);

$query = "SELECT blobthing
FROM mytable
WHERE id = 1;";

$results = mysql_query ($query, $connection);
$row = (mysql_fetch_assoc($result));
$varblobthing = $row['blobthing']; //put the blob in a variable

$query = "UPDATE mytable
SET blobthing = ".$varblobthing.",
WHERE id = 2;";

^
^
You forgot quotes
--
Oli
Sep 10 '05 #2
sime wrote:
mysql_select_db($dbname, $connection);
mysql_select_db($dbname, $connection)
or die ( 'Unable to select database.' );
$results = mysql_query ($query, $connection);
$results = mysql_query ($query, $connection)
or die ( 'Unable to execute query.' );
$row = (mysql_fetch_assoc($result));
$varblobthing = $row['blobthing']; //put the blob in a variable


$varblobthing = $row['blobthing']; //put the blob in a variable
mysql_free_result($result);
....
Sep 10 '05 #3
sime wrote:
Hi,

I have a blob field in a mysql database table. I want to copy a blob
from one record to another. I am having trouble transferring the data
via a php variable. Maybe I need to addslashes or convert to Hex or
something. I've tried a few things but can't quite get it. Here is
simplified code.

mysql_select_db($dbname, $connection);

$query = "SELECT blobthing
FROM mytable
WHERE id = 1;";

$results = mysql_query ($query, $connection);
$row = (mysql_fetch_assoc($result));
$varblobthing = $row['blobthing']; //put the blob in a variable

$query = "UPDATE mytable
SET blobthing = ".$varblobthing.",
WHERE id = 2;";

mysql_query ($query, $connection);
I would love any advice I can get. Thanks in advance.
Simon


Sime,

Three problems here. First of all, since $varblobthing is non-numeric,
you need to enclose it with "'" characters. Also, a blob can, by
definition, contain any character - including "special" ones like "'".
You need to use mysql_escape_string (or mysql_real_escape_string,
depending on your version of PHP and MySQL) to escape it.

Finally, get rid of the trailing ';' after the 2. You don't need a ';'
at the end of the SQL statement.

Try something like:

$query = "UPDATE mytable SET blobthing = '" .
mysql_escape_string($varblobthing) .
"' WHERE id=2";

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 10 '05 #4
Jerry Stuckle wrote:
sime wrote:
Hi,

I have a blob field in a mysql database table. I want to copy a blob
from one record to another. I am having trouble transferring the data
via a php variable. Maybe I need to addslashes or convert to Hex or
something. I've tried a few things but can't quite get it. Here is
simplified code.

mysql_select_db($dbname, $connection);

$query = "SELECT blobthing
FROM mytable
WHERE id = 1;";

$results = mysql_query ($query, $connection);
$row = (mysql_fetch_assoc($result));
$varblobthing = $row['blobthing']; //put the blob in a variable

$query = "UPDATE mytable
SET blobthing = ".$varblobthing.",
WHERE id = 2;";

mysql_query ($query, $connection);
I would love any advice I can get. Thanks in advance.
Simon


Sime,

Three problems here. First of all, since $varblobthing is non-numeric,
you need to enclose it with "'" characters. Also, a blob can, by
definition, contain any character - including "special" ones like "'".
You need to use mysql_escape_string (or mysql_real_escape_string,
depending on your version of PHP and MySQL) to escape it.

Finally, get rid of the trailing ';' after the 2. You don't need a ';'
at the end of the SQL statement.

Try something like:

$query = "UPDATE mytable SET blobthing = '" .
mysql_escape_string($varblobthing) .
"' WHERE id=2";


P.S. You should check the result of each mysql operation per the manual.
In this case I suspect the mysql_query is returning false and
checking the error with mysql_error() would give you information on why
it didn't work.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 10 '05 #5
On 10 Sep 2005 10:36:06 -0700, "sime" <ge*************@hotmail.com> wrote:
I have a blob field in a mysql database table. I want to copy a blob
from one record to another. I am having trouble transferring the data
via a php variable. Maybe I need to addslashes or convert to Hex or
something. I've tried a few things but can't quite get it. Here is
simplified code.

mysql_select_db($dbname, $connection);
You've said you are "having trouble", yet you have not described what
"trouble" you're having, you've only posted some code.
mysql_query ($query, $connection);

I would love any advice I can get.
MySQL and PHP can give you advice for starters; make sure error_reporting is
set to E_ALL, and check the result code of every mysql_* call; if it returns
false, use mysql_error() to print a full error message.
$query = "UPDATE mytable
SET blobthing = ".$varblobthing.",
WHERE id = 2;";


You're missing quotes around the value, escaping using mysql_escape_string,
and what's that comma at the end of the second line for?
Or, an alternative approach:
(http://dev.mysql.com/doc/mysql/en/ex...variables.html)

mysql> create table t (id int, b blob);
Query OK, 0 rows affected (0.09 sec)

mysql> insert into t values (1, 'one');
Query OK, 1 row affected (0.06 sec)

mysql> select * from t;
+------+------+
| id | b |
+------+------+
| 1 | one |
| 2 | two |
+------+------+
2 rows in set (0.00 sec)

mysql> insert into t values (2, 'two');
Query OK, 1 row affected (0.00 sec)

mysql> select @b:=b from t where id=1;
+-------+
| @b:=b |
+-------+
| one |
+-------+
1 row in set (0.08 sec)

mysql> update t set b=@b where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from t;
+------+------+
| id | b |
+------+------+
| 1 | one |
| 2 | one |
+------+------+
2 rows in set (0.00 sec)

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Sep 10 '05 #6
Jerry, you set me straight. mysql_escape_string() did the trick. I
had read about it at the mysql site but couldn't fathom it - especially
when they start talking about magic quotes.

Can I please apologise to you and other polite posters for the other
flaws in my sample. This is the poor form of a php newbie.

Many Thanks, Simon

Sep 11 '05 #7
sime wrote:
Jerry, you set me straight. mysql_escape_string() did the trick. I
had read about it at the mysql site but couldn't fathom it - especially
when they start talking about magic quotes.

Can I please apologise to you and other polite posters for the other
flaws in my sample. This is the poor form of a php newbie.

Many Thanks, Simon


No problem, Glad to help.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 11 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: barmaley | last post by:
how do I allow access to mySQL from another computer on local network? when I am trying to connect from MySQL Studio on Windows XP, I get error "Host '192.168.0.100' is not allowed to connect to...
4
by: James E Koehler | last post by:
I can't get the WHILE statement to work in MySQL. The version of MySQL that I am using is: Ver 12.16 Distrib 4.0.6-gamma, for Win95/Win98 (i32) running on Windows MX. Here is the relevant...
4
by: whitemoss | last post by:
Hi, I've made some changes to my coding..but unfortunately, there were errors when compiling it..dunno how to solve it..hope anyone can help me...the errors: client.c: In function senddata:...
4
by: geof | last post by:
Hi ! this is my first post where is my error code in line 15 ($sql ) <html> <head><title> update MySQL Data </title></head> <body bgcolor="#FFFFFF"> <?
1
by: shaji66 | last post by:
how to update values in HTML page using java script: hi all , I want to update some text fields in html , and display these values in another page .... can you please help me in writing...
3
by: ma | last post by:
Hello, I have two update Panel in my page. I want to update one of these update panels from another one. How can I do this? How can I force the whole page to be updated from a button inside an...
11
by: OllyJ | last post by:
Hi experts, I'm hoping you can help me with this.. I have recently found out how to use the INSERT INTO statement to add multiple records from a listbox to a table, I now need to UPDATE records in...
1
by: krishemanth | last post by:
sir, I want to update a value in dropdown using ajax i had a listing of some categories in dropdown along with that i had an add category in that dropdown box when select that addcategory option...
0
by: Serenityquinn15 | last post by:
Hi! I have two Table names: tblupdate tblDetails What i want to do is to update the table tblupdate from tblDetails using the button in Visual basic Interface.
11
donilourdu
by: donilourdu | last post by:
hi, I am trying to retrive data from MYSQL database.I am using limit to fetch the data.It will fetch two rows instead of fetching ten rows.But when i try to debug it fetches eight rows instead...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.