Link to home
Start Free TrialLog in
Avatar of Fernanditos
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.
Avatar of Amar Bardoliwala
Amar Bardoliwala
Flag of India image

Hello Fernanditos,

Try following link should help you.

http://www.w3schools.com/php/php_mysql_update.asp

Hope it will help you.

Thank You.

Amar Bardoliwala

Avatar of Fernanditos
Fernanditos

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.

 
<?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);
?>

Open in new window


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
Avatar of venloft
venloft

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@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.

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][description_bok] = clean($dbConsulta[$copyCount][description_bok]);

with following

$dbConsulta[$copyCount]['description_bok'] = clean($dbConsulta[$copyCount]['description_bok']);

Hope this will help.

Thank You.

Amar Bardoliwala






@venloft your solution works great, it was a problem with my function. I will award it.
the empty values were at the clean function?