Skip to content

Instantly share code, notes, and snippets.

@JaniKibichi
Created November 29, 2016 09:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save JaniKibichi/9e9c6fce0af9e8fe0f43cc8d708770e8 to your computer and use it in GitHub Desktop.
Save JaniKibichi/9e9c6fce0af9e8fe0f43cc8d708770e8 to your computer and use it in GitHub Desktop.
How to connect a USSD to a MYSQL DB
<?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
?>
@ogatopro
Copy link

ogatopro commented Mar 5, 2020

Hallo..which tables to i creat in xampp...and what are the field names

@BruceTushabe
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment