rok259
-
Posts
6 -
Joined
-
Last visited
Content Type
Profiles
Forums
Events
Posts posted by rok259
-
-
DB selection
in PHP
Should the statement variable not be inside some quotes like so?
$sqlCommand = "SELECT * FROM " . $type . " WHERE description LIKE '%$search%' OR username LIKE '%$search%' OR tittel LIKE '%$search%' ";
-
That's a lot of code. What actually happens when you run it, what is the actual problem?Note that setting the error reporting level to 1 will only show fatal runtime errors, nothing else. You can use E_ALL to show all errors.http://www.php.net/manual/en/errorfunc.constants.php
Thanks I have managed to get the script to run it shows; {"result":403,"message":"Forbidden"}
But when I try GET it still doesn't send back the id from the username == password. The issue is from line 177. Also I know the information on GET works to a point as it triggers a callback, but I can't be sure if it sends back the JSON data correctly.
-
Hello all,
Can someone assist me? My knowledge of PHP is slim and I have been learning most of it using W3school over the last couple of weeks. I require my script to use POST and GET to received and send JSON encoded data from my database to my mobile app. I have created the script and have managed to POST, but I can’t get it to return the information I need with GET.
Basically user enters Username & Password -> JSON -> PHP decodes and checks both match and sends back database unique ID for that user.
I can’t be sure if it is my PHP or JSON that’s causing my issue.
<?php $port = '3306'; $link = mysql_connect('localhost', 'username', 'password'); // database connection strings. change these to your DB and Table names. $dbName = "project"; $tableName = "User"; if (!$link) { exit('Error: Could not connect to MySQL server!'); } // connect to the table mysql_select_db($dbName)or die("cannot select DB"); // lets prepare some files to capture what is going on. $incomingJson = 'json.txt'; $fullArray = 'fullArray.txt'; // needed if you enable the debugging secton below $sqlErrorLog = "sqlErrors.txt"; // initialize the string with a blank value $string = ""; // start SEND data if ($_SERVER['REQUEST_METHOD'] === 'POST') { //capture incoming data error_reporting(E_ALL | E_STRICT); $sig = $_POST["sig"]; $jsondata = $_POST["params"]; // this line captures the sent data so you can figure out what you need to send back. file_put_contents($incomingJson,$jsondata); // this line tells the application that the data send was successful. echo '{"Status":"Success"}'; // convert JSON to an array $array = json_decode($jsondata, TRUE); /* // formats the array to view it easier $results = print_r($array,true); file_put_contents($fullArray,$results); */ //get the total number of objects in the array $arrlength = count($array['Children']['1']['Properties']); // set while loop index $i = 0; //loop through array node and get row values while ($i < $arrlength ) { // get row value $value = $array['Children']['1']['Properties'][$i]['Value']."n"; // convert delimited string to an array $arrayPieces = explode("|", $value); // get array values. This section would have to be modified to capture each value you are interested in. $rowName = $arrayPieces[0]; // this variable will be blank if you don't name your rows. $Username = $arrayPieces[2]; $Password =$arrayPieces[3]; // construct SQL statement $sql="INSERT INTO ".$tableName."(username, password)VALUES('$Username', '$Password')"; // insert SQL statement $result=mysql_query($sql); // catch any errors if($result){ // if successful do nothing for now. } else { // if failure, write to custom log $sqlError = "Error writing to databasen"; file_put_contents($sqlErrorLog, $sqlError, FILE_APPEND); } $i++; } } // end of POST if ($_SERVER['REQUEST_METHOD'] === 'GET') { $username = $_GET["username"]; $pass = $_GET["password"]; $query = 'SELECT * FROM User WHERE username="' . mysql_real_escape_string($username) . '" or email="' . mysql_real_escape_string($email) . '"'; $dbresult = mysql_query($query, $link); if (!$dbresult) { //echo "query failed"; $result = array(); $result["result"] = 403; $result["message"] = mysql_error(); echo json_encode($result); mysql_free_result($dbresult); exit; } $player = mysql_fetch_array($dbresult, MYSQL_ASSOC); if (strcmp($player["password"],md5($password)) == 0) { $result = array(); $result["result"] = 200; $result["message"] = "Success"; $result["id"] = $player["id"]; $query = sprintf("UPDATE players SET lastlogin=NOW() WHERE id=%s;", $player["id"]); $uresult = mysql_query($query, $link); if ($uresult) { //code if your update failed. Doesn't really impact what we are doing. so do nothing. } echo json_encode($result); } else { //echo "password mismatch"; $result = array(); $result["result"] = 403; $result["message"] = "Forbidden"; echo json_encode($result); }} else { $result = array(); $result["result"] = 400; $result["message"] = "Bad Request"; echo json_encode($result);}error_reporting(E_ALL | E_STRICT);exit;?>
-
Why? Wouldn't it make more sense to write a before-trigger to fake this?
I’m developing an RTS MMO that will require resources to gather when the player is logged out of the game. Best way I could think of achieving this would be to have the database auto update the field.
Also thanks for the Cron idea, I will check it out.
-
Hello
Is it possible to have my database increase an integer overtime? For instance every 1 minute add +1 to int field.
Only way I could think of getting the result would be to use a PHP script, but not sure if its possible.
Php GET
in PHP
Posted
Hi,
Just wanted to update what I have changed if anyone can help. I am still able to POST to the database, but cant select the information from the database and return what I need. I have added some JSON. Not sure if it would be better to use json_encode()