Fernanditos
asked on
Loop to update column in mysql database
Hi!
I have a database named "books_bok" and I want, from a php file update all values from colum "description_bok"
Can some expert help me with the right php code, to connect to database and loop the query ?
The new value for description_bok will be the same but filtered by a function, example
description_bok = clean($description_bok)
Any help with be greatly appreciated.
Thank you.
I have a database named "books_bok" and I want, from a php file update all values from colum "description_bok"
Can some expert help me with the right php code, to connect to database and loop the query ?
The new value for description_bok will be the same but filtered by a function, example
description_bok = clean($description_bok)
Any help with be greatly appreciated.
Thank you.
ASKER
Sorry but it does not answer my question. Thank you anyway.
Hello Fernanditos,
Sorry but it answers following
1. How to connect to database
2. partly to loop the query.
Regarding Looping query, I would like to ask following
1. what does a clean function do? is it PHP function?
if yes than you will need to fetch records first and loop them to update all one by one as shown in following code.
In code above you will need to replace database name, table name, fields name and primary key as per your current settings.
Hope this will help.
Thank You.
Amar Bardoliwala.
Sorry but it answers following
1. How to connect to database
2. partly to loop the query.
Regarding Looping query, I would like to ask following
1. what does a clean function do? is it PHP function?
if yes than you will need to fetch records first and loop them to update all one by one as shown in following code.
<?php
$con = mysql_connect("localhost","username","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM mytable");
while($row = mysql_fetch_assoc($result))
{
mysql_query("update mytable set description_bok = '". clean($row['description_bok']) ."' where primary_key = " . $row['primary_key']);
}
mysql_close($con);
?>
In code above you will need to replace database name, table name, fields name and primary key as per your current settings.
Hope this will help.
Thank You.
Amar Bardoliwala.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@amar, your solution returns this error: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in...
@venloft, your solution is very close. It has updated all fields but with empty value.
We need to get first the current value of description_bok to pass it to my function and get the cleaned result.
why description_bok pass empty ?
Please check.
@venloft, your solution is very close. It has updated all fields but with empty value.
We need to get first the current value of description_bok to pass it to my function and get the cleaned result.
why description_bok pass empty ?
Please check.
Hello Fernanditos,
if it is giving following error
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean
it means that something is wrong with your query.
If you can paste your current code than it might help.
Look at following link for more help with mysql_fetch_assoc()
http://php.net/manual/en/function.mysql-fetch-assoc.php
Regarding problem with the code of venloft
I think you might need to change following line
$dbConsulta[$copyCount][de scription_ bok] = clean($dbConsulta[$copyCou nt][descri ption_bok] );
with following
$dbConsulta[$copyCount]['d escription _bok'] = clean($dbConsulta[$copyCou nt]['descr iption_bok ']);
Hope this will help.
Thank You.
Amar Bardoliwala
if it is giving following error
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean
it means that something is wrong with your query.
If you can paste your current code than it might help.
Look at following link for more help with mysql_fetch_assoc()
http://php.net/manual/en/function.mysql-fetch-assoc.php
Regarding problem with the code of venloft
I think you might need to change following line
$dbConsulta[$copyCount][de
with following
$dbConsulta[$copyCount]['d
Hope this will help.
Thank You.
Amar Bardoliwala
ASKER
@venloft your solution works great, it was a problem with my function. I will award it.
the empty values were at the clean function?
Try following link should help you.
http://www.w3schools.com/php/php_mysql_update.asp
Hope it will help you.
Thank You.
Amar Bardoliwala