I moved my files from my Xampp server over to a live server and now the PHP doesn't seem to be working correctly. This file is the problem
<?php
require ("../Android/connect.php");
require ("../Android/queries.php");
if ($query_run = mysql_query($questions_query)) {
if (mysql_num_rows($query_run) == NULL) {
$response["success"] = 0;
echo json_encode($response);
} else {
$response ['questions'] = array();
while ($row = mysql_fetch_assoc($query_run)) {
$info = array();
$info ['display_name'] = $row['display_name'];
$info ['field_type'] = $row['field_type'];
$info ['option_value'] = $row['option_value'];
array_push($response["questions"], $info);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
}
}
?>
My Android app that was getting JSON from this file now gets null JSONObjects. When I open it in my browser it gives me this error:
"Server error
The website encountered an error while retrieving http://mysite.com/Android/myfile.php. It may be down for maintenance or configured incorrectly."
My first thought was my connect.php or queries.php that are included were at fault. However they work fine. I can get them to display in my browser or echo out messages to me. The file above however will not work. Anyone know what the problem is?
if ($query_run = mysql_query($questions_query)) {
Be sure to define $questions_query- looks like that's your problem right there
Related
I am developing a PHP script that allows me to modify tags in an XML file and move them once done.
My script works correctly but I would like to add error handling: So that if the result of my SQL query does not return anything display an error message or better, send a mail, and not move the file with the error and move to the next.
I did some tests but the code never displays the error and it moves the file anyway.
Can someone help me to understand why? Thanks
<?php
}
}
$xml->formatOutput = true;
$xml->save($source_file);
rename($source_file,$destination_file);
}
}
closedir($dir);
?>
Give this one a try
$result = odbc_fetch_array($exec);
if ($result === false || $result['GEAN'] === null) {
echo "GEAN not found for $SKU_CODE";
// continue;
}
$barcode = (string) $result['GEAN'];
echo $barcode; echo "<br>"; //9353970875729
$node->getElementsByTagName("SKU")->item(0)->nodeValue = "";
$node->getElementsByTagName("SKU")->item(0)->appendChild($xml->createTextNode($result[GEAN]));
I want to fetch data in my flutter app from my own PHP file where I did echo jsonEncode(object);
but it giving following error :
Error: XMLHttpRequest error.
dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 906:28 get current
packages/http/src/browser_client.dart 71:22 <fn>
dart-sdk/lib/async/zone.dart 1612:54 runUnary
dart-sdk/lib/async/future_impl.dart 152:18 handleValue
dart-sdk/lib/async/future_impl.dart 704:44 handleValueCallback
dart-sdk/lib/async/future_impl.dart 733:13 _propagateToListeners
dart-sdk/lib/async/future_impl.dart 530:7 [_complete]
dart-sdk/lib/async/stream_pipe.dart 61:11 _cancelAndValue
dart-sdk/lib/async/stream.dart 1219:7 <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 324:14 _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 329:39 dcall
dart-sdk/lib/html/dart2js/html_dart2js.dart 37307:58 <fn>
at Object.createErrorWithStack (http://localhost:42101/dart_sdk.js:5347:12)
at Object._rethrow (http://localhost:42101/dart_sdk.js:39349:16)
at async._AsyncCallbackEntry.new.callback (http://localhost:42101/dart_sdk.js:39343:13)
my flutter code where is giving the error ..
getdata() async {
typesOfc = await http.get(Uri.parse("http://localhost:4545/"));
print(jsonDecode(typesOfc));
}
I'm using http plugin
my php code ..
if($action == 'fetchData'){
$fetchQuery = "SELECT `NAME` , `PASSWORD` FROM `REGISTER`";
$FetchResult = mysqli_query($connectLhost , $fetchQuery);
$data = array();
if($FetchResult->num_rows > 0){
while($row = $FetchResult->fetch_assoc()){
$data[] = $row;
}
echo json_encode($data);
}else{
echo "Cant FetchData";
}
}
I have big problem because, when I'm trying to show status of my ts3 server I have blank page... What am I doing wrong?
require_once('libraries/TeamSpeak3/TeamSpeak3.php');
try
{
// connect to server, authenticate and grab info
$ts3 = TeamSpeak3::factory("serverquery://query_admin:query_pass#host:10011/?server_port=9987");
// show server as online
$serverinfo[$j]['hostname'] = $ts3->virtualserver_name;
$serverinfo[$j]['online'] = 'online';
$serverinfo[$j]['players'] = $ts3->virtualserver_clientsonline;
$serverinfo[$j]['max'] = $ts3->virtualserver_maxclients;
}
catch(Exception $e)
{
// grab errors and show server as offline
$serverinfo[$j]['online'] = 'offline';
$serverinfo[$j]['players'] = '-';
$serverinfo[$j]['max'] = '-';
}
When I comment this code the page shows as normal...
EDIT:
I see it now, if I only add this
require_once('libraries/TeamSpeak3/TeamSpeak3.php');
and nothing more to my code it shows blank page... Is it possible, that library from here doesn't work properly?
You forgot to add echo before each info so the code becomes :
require_once('libraries/TeamSpeak3/TeamSpeak3.php');
try
{
// connect to server, authenticate and grab info
$ts3 = TeamSpeak3::factory("serverquery://query_admin:query_pass#host:10011/?server_port=9987");
// show server as online
echo $serverinfo[$j]['hostname'] = $ts3->virtualserver_name;
echo $serverinfo[$j]['online'] = 'online';
echo $serverinfo[$j]['players'] = $ts3->virtualserver_clientsonline;
echo $serverinfo[$j]['max'] = $ts3->virtualserver_maxclients;
}
catch(Exception $e)
{
// grab errors and show server as offline
echo $serverinfo[$j]['online'] = 'offline';
echo $serverinfo[$j]['players'] = '-';
echo $serverinfo[$j]['max'] = '-';
}
So I have this program that allows a user to enter information into a form and upon submission turns that information into a JSON file. When a user goes to a different part of the program, the programs retrieves the JSON file and builds a questionnaire out of it.
The building of the JSON file works fine but whenever I try to retrieve the file I'm getting an error that the JSON is returning as ASCII and as NULL. I've done my homework and saw that this usually happens when their is an encoding conflict(even though ASCII is a subset of UTF-8...).
So I made sure that when creating the file I'm using using mb_convert_encoding($x, 'UTF-8', 'auto');
to ensure that the JSON is properly being encoded as UTF-8.
I was also using mb_convert_encoding when retrieving the JSON, but saw that double encoding can cause issues so when I removed that piece it no longer echoed out what the encoding was(using mb_detect_encoding) but it is still NULL.
I even went so far as to pull down the JSON file, save it as UTF-8 and re-upload it.
Any and all help on this is much appreciated it. I've banged my head for two days over this. This is built in Code Ignitor, if that makes a difference
Here is the code to create the JSON file:
$thisClient = $this->input->cookie('client');
$date = "%m-%Y";
$date = mdate($date);
$clientDir = *********PATH TO CREATE THE DIRECTORIES IN;
$dialogDir = $clientDir."/".$date;
$d_file_name = $thisClient.'-'.$date;
//check to see if client directory exists, if it doesn't then it creates it
if(!is_dir($clientDir)){
mkdir($clientDir, 0755, TRUE);
echo "Client Directory Created!<br>";
} else{
echo "No Client Directory Created!<br>";
}
//check to see if client directory exists, if it doesn't then it creates it
if(!is_dir($dialogDir)){
mkdir($dialogDir, 0755, TRUE);
echo "DIALOG Directory Created!<br>";
} else{
echo "No DIALOG Directory Created!<br>";
}
$custDialog = array();
if(isset($_POST['cust-dialog-title'])){
function encodeMe($x){
//this ensure proper encoding
return mb_convert_encoding($x, 'UTF-8', 'auto');
}
$customDialog = array();
for($i = 0; $i < count($_POST['cust-dialog-title']); $i++){
$customDialog[$i]["title"] = encodeMe($_POST['cust-dialog-title'][$i]);
$customDialog[$i]["intro"] = encodeMe($_POST['cust-dialog-intro'][$i]);
for($ii = 0; $ii < count($_POST['cust-dialog-quest-'.$i]); $ii++){
$customDialog[$i]["questions"]["q".$ii] = encodeMe($_POST['cust-dialog-quest-'.$i][$ii]);
if($_POST["cust-dialog-pos-".$i."-".$ii] == "TRUE"){
//if the question is a true positive
$customDialog[$i]["questions"]["agree"] = -5;
$customDialog[$i]["questions"]["disagree"] = 5;
} else{
//if the question is a false positive
$customDialog[$i]["questions"]["agree"] = 5;
$customDialog[$i]["questions"]["disagree"] = -5;
}
}
$jsonDIALOG = json_encode($customDialog);
$jsonDIALOG = str_replace("[", " ", str_replace("]", " ", $jsonDIALOG));
if ( ! write_file($dialogDir."/".$d_file_name.".json", $jsonDIALOG )) {
echo 'Unable to write the file';
} else {
echo 'File written!';
}
//save Custom DIALOG info in database
***********DATABASE INFO**************
}
}
Here is the code to retrieve the JSON object:
if($row["custom"] !== null){ //If the Dialog is a Custom Dialog
$path = str_replace(*****removes an unnecessary portion from the path string**);
$thisDialog = file_get_contents(****PATH TO JSON FILES*****);
//THE FOLLOWING helps debug issues with the JSON -- displays order number and dialog being called -- uncomment to use
//echo $i.' is '.$curDialog[$i]. '<br>';
//$thisDialog = substr($thisDialog,1);
//echo $thisDialog;
//THIS IS THE CODE FOR DEBUGGING ENCODING ISSUES
//$thisDialog = mb_convert_encoding($thisDialog, 'UTF-8', 'ASCII');
//echo mb_detect_encoding($thisDialog);
$jsonDialog = json_decode($thisDialog, true);
echo var_dump($jsonDialog);
if($jsonDialog){
$allDialogs = $jsonDialog;
} else {
echo "Error: Invalid Dialog. Call Order# 0<br>" ;
}
return $allDialogs;
}
I've included some debugging things that I've tried and commented out. Thanks!!
You should probably add JSON_UNESCAPED_UNICODE as an option to json_encode. Keep in mind that this constant is available since PHP 5.4.0
For some reason I got this script to work without fault but once i tried to decode the JSON the script started messing up. I reverted back to the old script and it still isn't working. Here it is below. $result keeps giving me 0 which if the PHP documentation is correct means that the verification failed. I have double checked public_key.pem to see if it is correct and it is. I have also checked to see if signedData and signature are being sent and they are being sent. What could possibly be wrong with this script?
<?php
$signed_data = $_POST['signedData'];
$signature = $_POST['signature'];
$fp = fopen("./public_key.pem", "r");
$cert = fread($fp, 451);
fclose($fp);
$pubkeyid = openssl_get_publickey($cert);
$signed_data = trim($signed_data);
$signature = base64_decode($signature);
$result = openssl_verify($signed_data, $signature, $pubkeyid, OPENSSL_ALGO_SHA1);
if ($result == 1) {
echo 'correct';
} else if ($result == 0) {
echo 'fail';
echo '||';
echo $pubkeyid;
echo '||';
echo $signed_data;
} else {
echo 'epic fail';
}
openssl_free_key($pubkeyid);
?>
There is a bug with openssl it seems which was reported here https://bugs.php.net/bug.php?id=60936 which looks like it could be the same issue you're having.