Last active
August 29, 2015 13:59
-
-
Save Deathnerd/10564698 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Created by PhpStorm. | |
* User: Deathnerd | |
* Date: 4/12/14 | |
* Time: 9:16 PM | |
*/ | |
function get_key_values_from_database($key){ | |
$db = mysqli_connect('localhost', 'root', 'root', 'db'); | |
$table = 'keysToValues'; | |
//check if table exists | |
if(!(mysqli_num_rows(mysql_query($db, "SHOW TABLES LIKE '$table'")) > 0)){ | |
die(mysqli_error($db)); | |
} | |
$query = mysqli_query($db, "SELECT * FROM `$table` WHERE keys='$key';"); | |
if(mysql_num_rows($query) === 0){ //no rows match | |
return false; | |
} | |
return mysqli_fetch_array($query); | |
} | |
if(isset($_GET['key'])){ | |
header('Content-type: application/json'); | |
echo json_encode(get_key_values_from_database($_GET['key'])); | |
exit("Success"); | |
} | |
exit("Key not set"); |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> | |
</head> | |
<body> | |
<label for="value">Value: </label> | |
<input type="text" name="value" id="value"/> | |
<br/> | |
<label for="key">Key: </label> | |
<input type="text" name="key" id="key"/> | |
<br/> | |
<input value="set" id="setButton" type="button" onclick="actions.set()"/> | |
<br/> | |
<label for="get_key">Key to get: </label> | |
<input type="text" name="get_key" id="get_key"/> | |
<br/> | |
<input id="getButton" type="button" value="get" onclick="actions.get()"/> | |
</body> | |
<script type="text/javascript"> | |
actions = { | |
set: function(){ | |
var v = $("#value").val(); | |
var k = $("#key").val(); | |
$.ajax({ | |
url: "set.php", | |
data: { | |
key: k, | |
value: v | |
}, | |
success: function(response){ | |
alert(response); | |
} | |
}) | |
}, | |
get: function(){ | |
var k = $("#get_key").val(); | |
$.ajax({ | |
url: "get.php", | |
success: function(response){ | |
alert("Key = "+ response.key); | |
alert("Value = "+ response.value); | |
}, | |
data: { | |
key: k | |
} | |
}) | |
} | |
} | |
</script> | |
</html> |
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 | |
/** | |
* Created by PhpStorm. | |
* User: Deathnerd | |
* Date: 4/12/14 | |
* Time: 9:19 PM | |
*/ | |
function key_value_to_database($array){ | |
if(!is_array($array)){ | |
trigger_error("key_value_to_database requires the argument to be an array", E_USER_ERROR); | |
} | |
$db = mysqli_connect('localhost', 'root', 'root', 'db') or die(mysqli_error($db)); | |
$key = mysqli_real_escape_string($db, $_GET['key']); | |
$val = mysqli_real_escape_string($db, $_GET['value']); | |
$table = "keysAndValues"; | |
//if the table doesn't exist, create it | |
if(!(mysqli_num_rows(mysqli_query($db, "SHOW TABLES LIKE `$table`")) > 0)){ | |
mysqli_query($db, "CREATE TABLE `$table` (keys VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci, vals VARCHAR(255)) COLLATE utf8_general_ci CHARACTER SET utf8;") or die(mysqli_error($db)); | |
} | |
//if the key already exists, update it | |
if(mysqli_num_rows(mysqli_query($db, "SELECT * FROM `$table` WHERE keys='$key'")) > 0){ | |
$query = mysqli_query($db, "UPDATE `$table` SET vals='$val' WHERE keys='$key';") or die(mysqli_error($db)); | |
} | |
mysqli_query($db, "INSERT INTO `$table` (keys, vals) VALUES ('$key', '$val');") or die(mysqli_error($db)); | |
} | |
if(isset($_GET)){ | |
key_value_to_database($_GET); | |
exit("Success"); | |
} | |
exit("GET not set"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment