After mseancole's answer
I just realized that I was using json_id
for 3 of the 4 queries after reading mseancole's hint about putting them into an array. Neat!
Are there more improvements I could make here?
function query(PDO $conn, $query, $params) {
$result = $conn->prepare($query);
foreach ($params as $key => $value) {
$result->bindValue(":$key", $value);
}
$result->execute();
// does this seem 'correct'? This is my solution to queries
// with more than 1 result and then using foreach() instead of while($row = ...)
if($result->rowCount() > 1) {
return $result->fetchAll(PDO::FETCH_ASSOC);
} else {
return $result->fetch(PDO::FETCH_ASSOC);
}
}
$conn = new PDO("mysql:host=$host;dbname=$db", $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$queries['general'] = "SELECT people.id, people.surname, people.firstname, people.email,
leases.room_id,
DATE_FORMAT(leases.in_date, '%e %M %Y') as in_date,
DATE_FORMAT(leases.out_date, '%e %M %Y') as out_date,
leases.rent_type FROM people, leases
WHERE people.id = :json_id AND leases.person_id = :json_id";
// find all bills charged to tenant
$queries['owed'] = "SELECT SUM(amount) as amount FROM money_due WHERE person_id = :json_id";
// find all money paid into the system for the bills charged to tenant
$queries['paid'] = "SELECT sum(money_paid.amount) as amount FROM money_paid, money_due WHERE
money_paid.money_for = money_due.id AND money_due.person_id = :json_id";
foreach ($queries as $label => $query) {
${$label} = query($conn, $query, array("json_id" => $json_id));
}
$query = "SELECT num as room_num, wg, rent FROM rooms WHERE id = :room_id";
$room_info = query($conn, $query, array("room_id" => $general['room_id']));
// merge lease info with general info
$all_info = array_merge($general, $room_info);
$all_info['rent_type'] = $rent_types[$all_info['rent_type']];
// calculate money owed to the house
$all_info['balance'] = $paid['amount'] - $owed['amount'];
Original post:
I'm trying to improve my coding practices and I was just wondering if (I'm definitely sure there are) there are better ways of doing the following task.
I'm rewriting the rent system of the building I live in, which currently (still) uses an Access '97 database. This is the schema that I've constructed and the following code fetches the data to be used in the section just below the nav.
I just have a few questions.
Am I doing too many queries? I'm pretty sure I cannot get all the information needed in just a single query (I've tried).
Naming conventions, should I be using $query1, $result1 etc or $person_query, $result_query. My logic is that, I don't need the resource variable again, why not reuse it? It's not like I create a new $conn each time.
Is there a simpler way of getting data, without using any framework. I'm trying to solidify my own practices first before learning CakePHP. But perhaps CakePHP will force me to do that anyway.
Thanks for reading!
Nay
<?php header('content-type: application/json; charset=utf-8');
//$_POST = json_decode(file_get_contents('php://input'));
$host = "127.0.0.1:3306";
$db = "bettenhaus";
$user = "root";
$pass = "";
$rent_types[0] = "Mieter";
$rent_types[1] = "Untermieter";
$rent_types[2] = "Zwischenmieter";
$json_id = $_GET['id'];
$conn = new PDO("mysql:host=$host;dbname=$db", $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
// get basic info
$query = "SELECT people.id, people.surname, people.firstname, people.email, leases.room_id, DATE_FORMAT(leases.in_date, '%e %M %Y') as in_date, DATE_FORMAT(leases.out_date, '%e %M %Y') as out_date, leases.rent_type FROM people, leases WHERE people.id = :json_id AND leases.person_id = :json_id";
$result = $conn->prepare($query);
$result->bindValue(":json_id", $json_id);
$result->execute();
// prepare to merge
$person_info = $result->fetch(PDO::FETCH_ASSOC);
// get lease info
$query = "SELECT num as room_num, wg, rent FROM rooms WHERE id = :room_id";
$result = $conn->prepare($query);
$result->bindValue(":room_id", $person_info['room_id']);
$result->execute();
$room_info = $result->fetch(PDO::FETCH_ASSOC);
// merge two results
$all_info = array_merge($person_info, $room_info);
$all_info['rent_type'] = $rent_types[$all_info['rent_type']];
// get all bills charged to this tenant
$query = "SELECT SUM(amount) as debt FROM money_due WHERE person_id = :json_id";
$result = $conn->prepare($query);
$result->bindValue(":json_id", $json_id);
$result->execute();
$debt_info = $result->fetch(PDO::FETCH_ASSOC);
// get total amount of money paid into the system by tenant
$query = "SELECT sum(money_paid.amount) as sum FROM money_paid, money_due WHERE money_paid.money_for = money_due.id AND money_due.person_id = :json_id";
$result = $conn->prepare($query);
$result->bindValue(":json_id", $json_id);
$result->execute();
$paid_info = $result->fetch(PDO::FETCH_ASSOC);
// calculate money owed to the house
$all_info['balance'] = $paid_info['sum'] - $debt_info['debt'];
$conn = null;
echo json_encode($all_info);
?>