I just submitted my work and my course leader has asked me to use PHP functions to query my database. How can I do this? Is it possible to add just “function getRooms” before my queries, and return the desired result. I need thorough briefing on functions with MySQL.
For example:
How will I add a function to this code:
<?php
// By Kelvin
include ("player.php");
$result = mysql_query("SELECT * FROM phplogin WHERE username = '$username'");
$row = mysql_fetch_assoc($result);
$medipack = $row['medipack'];
if ($_POST['object'] == "Use Medipack") {
if ($medipack != 0) {
mysql_query("UPDATE phplogin SET score = score + 50, health = health + 50, medipack = medipack - 1
WHERE username = '$username'");
echo ("<P>Medipack Used!</P>");
$RoomNumber =5;
}
else {
echo ("<P>You're all out of medipacks!</P>");
}
}
?>
If he’s looking for a level of abstraction between the rest of your code and the code accessing the database, wrapping the database logic in functions should be on the right path:
Wrapping these functions into a class, and accessing them by methods is another step, as the instantiated object can manage its own connection, etc., but that may be a step further than necessary. Other functions (based on what I can guess) might be: