Created
May 29, 2014 11:38
-
-
Save navarroaxel/56875ea2b9706f38fd04 to your computer and use it in GitHub Desktop.
How to use sql params in PHP with MySQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace solbs\bookingengine\services; | |
| use mysqli; | |
| use solbs\bookingengine\model\Reservation; | |
| class LoggingService | |
| { | |
| /** | |
| * @param MyObject $myobject | |
| * @param string $pass | |
| * @param bool $success | |
| * @return void | |
| */ | |
| public static function log($myobject, $pass, $success) | |
| { | |
| $query = "INSERT INTO records (model, pass, success) VALUES (?, ?, ?)"; | |
| $mysqli = self::getConnection(); | |
| $stmt = $mysqli->prepare($query); | |
| $stmt->bind_param("s", self::getModel($myobject)); | |
| $stmt->bind_param("S", $pass); | |
| $stmt->bind_param("i", intval($success)); | |
| $stmt->execute(); | |
| $mysqli->close(); | |
| } | |
| /** | |
| * @param MyObject $myobject | |
| * @return string | |
| */ | |
| static function getModel($myobject) | |
| { | |
| return json_encode($myobject); | |
| } | |
| static function getConnection() | |
| { | |
| $dbUser = "user"; | |
| $dbPassword = "pass"; | |
| $dbName = "dump"; | |
| $dbServer = "127.0.0.1:3306"; | |
| $mysqli = new mysqli($dbServer, $dbUser, $dbPassword, $dbName); | |
| $mysqli->set_charset("utf8"); | |
| return $mysqli; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment