I have two tables without common key (property and demand). I want to write a PHP script that first selects all lines from property and foreach line from property will select lines from demand comparing specific field values from property to demand.
For example; table property has:
Area, Price
table demand has
AreaFrom, PriceFrom
a line from property will be
Area=50, Price=100
and for that line I want to match it with all lines from demand which has AreaFrom=50 and PriceFrom=100.
This is the script you want. I wrote it with the PDO api because first the mysql normal api has deprecated and no longer supported and second PDO provides more security..
And one more thing.. Always name your Tables and Databases with all lowercase letters. because Uppercase letters cause problems in queries.. so I used areafrom instead of Areafrom.
<?php
try {
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$qry = $conn->prepare('SELECT * FROM property');
$qry2 = $conn->prepare('SELECT * FROM demand WHERE areafrom = :areafrom AND pricefrom = :pricefrom');
$qry->execute();
while($row = $qry->fetch(PDO::FETCH_OBJ)) {
$qry2->execute(array(':areafrom' => $row->area, ':pricefrom' => $row->price));
$row2 = $qry2->fetch(PDO::FETCH_OBJ);
//Do all you want with the $row2. If u want to echo the areafrom, do echo $row2->areafrom
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>