php


Merging tables with PHP similar_text and UPDATE


I have two tables in MySQL I'd like to merge:
table_1
--------------------------------------------------------
id | table_1_name | table_1_art
--------------------------------------------------------
1 | Ernest Hemingway   | Writer
--------------------------------------------------------
2 | Mark Twain | Writer
--------------------------------------------------------
3 | Jackson Pollock | Painter
--------------------------------------------------------
4 | Miles Davis | Musician
--------------------------------------------------------
5 | Akira Kurosawa | Cinema
--------------------------------------------------------
table_2
--------------------------------------------------------
id | table_2_name | table_2_art
--------------------------------------------------------
1 | Hemingway, ernest   | Writer
--------------------------------------------------------
2 | Twain, Mark | Writer
--------------------------------------------------------
3 | P., Jackson | Painter
--------------------------------------------------------
4 | Miles Dewey Davis | Musician
--------------------------------------------------------
5 | Bukowski, Charles | Poetry
--------------------------------------------------------
I want to update the field table_2_name with the data of table_1_name, but only if there is a similarity between them. I am using similar_text, and selecting similarities over 40%:
<?php
// Connection
$server = "****";
$user = "****";
$pass = "****";
$db_name = "****";
$conn = new mysqli($server, $user, $pass, $db_name);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connected <br/>";
}
//Begin
$petition = "SELECT * FROM table_1, table_2";
$result = mysqli_query($conn, $petition);
while ($row = mysqli_fetch_assoc($result)) {
$table_1_name = $row['table_1_name'];
$table_1_art = $row['table_1_art'];
$table_2_name = $row['table_2_name'];
$table_2_art =$row['table_2_art'];
//Ask for similarities
similar_text($table_1_name, $table_2_name, $percent);
//Show coincidences over 40%
if ($percent > "40%") {
echo $table_1_name." — ".$table_2_name." — ".$percent."% <br />";
//Update field in table_2_name for this coincidences
$sql = "UPDATE table_2 SET table_2_name = '$table_1_name'";
if ($conn->query($sql) === true) {
echo "updated <br/> <br/>";
} else {
echo "ERROR: " . $conn->error." <br/> <br/>";
}
}
$conn->close();
}
But I can't get results. Instead of this:
table_2
--------------------------------------------------------
id | table_2_name | table_2_art
--------------------------------------------------------
1 | Ernest Hemingway | Writer
--------------------------------------------------------
2 | Mark Twain | Writer
--------------------------------------------------------
3 | Jackson Pollock | Painter
--------------------------------------------------------
4 | Miles Davis | Musician
--------------------------------------------------------
5 | Bukowski, Charles | Poetry
--------------------------------------------------------
I get this:
table_2
--------------------------------------------------------
id | table_2_name | table_2_art
--------------------------------------------------------
1 | Ernest Hemingway   | Writer
--------------------------------------------------------
2 | Ernest Hemingway | Writer
--------------------------------------------------------
3 | Ernest Hemingway | Painter
--------------------------------------------------------
4 | Ernest Hemingway | Musician
--------------------------------------------------------
5 | Ernest Hemingway | Poetry
--------------------------------------------------------
I'm sure it is a very common mistake, at least for beginners, but I can't find it!
Nikita
PD: as #denny said, the sql needs a condition, and I forgot to say that I tried some options, like:
$sql = "UPDATE table_2 SET table_2_name = '$table_1_name' WHERE ($percent > '45%')";
The problem is now there: which condition and how. Can't believe is that difficult…
Change sql
$sql = "UPDATE table_2 SET table_2_name = '$table_1_name'";
To this
$sql = "UPDATE table_2 SET table_2_name = '$table_1_name'" where column="your condition";
Syntax:-
update table_name set column_name = 'value' where column_name ='your_condition'
If you excute
update table_name set column_name = 'value'
It will update whole table row
Ok, I found a way.
First of all, I was making a mistake: I was closing the connection in the middle of the while:
$conn->close();
}
And should be and the very end:
}
$conn->close();
?>
Also, I discovered that is much better to export de data to a third table with INSET INTO than trying to UPDATE the first table.
First I compare cells:
$similar_text($table_1_name, $table_2_name, $percent);
And then export the results to table_3, adding a field for the percentage:
if ($percent_autor > "40%") {
$sql = "INSERT INTO table_3 (table_3_name, table_3_art, percent)
VALUES ('$table_1_name', '$table_1_art, '$percent')";
}
And that's all. At the end there will be some rows with repeated data, but is easy to find and delete them ;)

Related Links

How src property in <img> tag interacts with index.php using pretty URLs
PHP Session/Cookies Not Working - Multiple Servers [closed]
Calling anonymous functions in a PHP class
php send mail functikon not working within a cron job set up with cPanel
php set_include_path drives me crazy
Laravel 4 Artisan drop existing column from table
Sql result returning as a boolean not resource [duplicate]
Apache server does not recognize the htdocs folder
mysql to check if value is more than 0
How to transfer information between pages using onclick but without using the URl
PHP Email validator (form) from brackets
Paypal paying back users
Convert higher bitrate mp3 files to lower bitrate in PHP
Need help creating custom user registration/login script [closed]
Failing at Datatable's w/AJAX+jQuery
Redis as unique atomic id generator - Thread safe way for web app to avoid race condition

Categories

HOME
opengl
openshift
android-studio
generics
yaml
activex
mousemove
semantic-web
vue-resource
azure-mobile-services
fedora
position
pyresttest
xades4j
mailmerge
freertos
rest-assured
boomi
psql
clr
sqlplus
maven-surefire-plugin
batch-rename
backendless
jpa-2.1
tortoisemerge
acrobat
django-import-export
ftp-server
code-rally
recovery
assert
microkernel
viewcontroller
transparency
doctrine-extensions
nothing
winston
gmaps.js
bpms
adjacency-list
okta-api
python-cffi
bayesglm
linuxbrew
nette
cfwheels
gitlab-omnibus
netdatacontractserializer
postal-code
phpredis
n-tier-architecture
encryption-symmetric
cakephp-2.2
cout
windows-rt
back
jack
assetic
pbkdf2
visual-c++-2005
progress-db
smartfox
daap
chronometer
uitextfielddelegate
autosize
servercontrol
css-friendly
downgrade
fxruby
gtktextview
acceleration
webclient.uploaddata

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App