Created
November 29, 2016 09:40
-
-
Save JaniKibichi/9e9c6fce0af9e8fe0f43cc8d708770e8 to your computer and use it in GitHub Desktop.
How to connect a USSD to a MYSQL DB
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 | |
//Declare the Connection Credentials | |
$servername = 'localhost'; //or IP address for DB hosted elsewhere | |
$username = 'root'; | |
$password = ""; | |
$database = "ussd"; | |
$dbport = 3306; | |
// Create connection | |
$db = new mysqli($servername, $username, $password, $database, $dbport); | |
// Check connection, if there is an error end the USSD connection | |
if ($db->connect_error) { | |
header('Content-type: text/plain'); | |
//log error to USSD call and end call, of course replace this with a graceful error! | |
die("END A Database error was encountered. Contact Admin to rectify!"); | |
} | |
//Using the connection within the USSD Application | |
//Assumption: You have created relevant tables in the Database USSD: How to here -> | |
//Create DB: http://www.w3schools.com/sql/sql_create_db.asp | |
//Create Table: http://www.w3schools.com/sql/sql_create_table.asp | |
//1. | |
//Check the level | |
$level = 0; | |
$sql = "select `level` from `session_levels` where `session_id`='" . $sessionId . "'"; | |
$levelQuery = $db->query($sql); | |
if($result = $levelQuery->fetch_assoc()) { | |
$level = $result['level']; | |
} | |
//2. | |
//check if user is not in db | |
$firstQuery="SELECT * FROM users WHERE `phonenumber` LIKE '%".$phoneNumber."%' LIMIT 1"; | |
$firstResult=$db->query($firstQuery); | |
$userAvail=$firstResult->fetch_assoc(); | |
//3. | |
//Insert session values | |
$s1Query = "insert into `session_levels`(`session_id`, `phoneNumber`,`level`) values('".$sessionId."','".$phoneNumber."', 1)"; | |
$db->query($s1Query); | |
//4. | |
//Update Table | |
$s1levelUpdate = "update `session_levels` set `level`=0 where `session_id`='".$sessionId."'"; | |
$db->query($s1levelUpdate); | |
//5. | |
//DO MORE | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hallo..which tables to i creat in xampp...and what are the field names