I have a database in phpmyadmin panel. And I want to see this databases as json format. When I paste my link to http://jsonlint.com/. It says Null. What's wrong with my php script?
<?php
$host = "**";
$user = "**";
$password = "**";
$db = "**";
$sql = "select * from product_info;";
$con = mysqli_connect($host,$user,$password,$db);
$result = mysqli_query($con,$sql);
$response = array();
while($row = mysqli_fetch_array($result)){
array_push($response,array("name"=>$row[0],"email"=>$row[1],"mobile"=>$row[2]));
}
echo json_encode(array("server_response"=>$respose));
mysqli_close($con);
?>
You are missing a letter in the word "response" on the echo line. That's why you are getting NULL.
Replace
echo json_encode(array("server_response"=>$respose));
By this
echo json_encode(array("server_response"=>$response));
Also, you should set the header to application/json
header('Content-Type: application/json');
Final code should be:
header('Content-Type: application/json');
echo json_encode(array("server_response"=>$response));
You have a typo ($respose) in the line
echo json_encode(array("server_response"=>$respose));
which should be
echo json_encode(array("server_response"=>$response));
Related
<?php
$host = "MY_HOST";
$username = "MY_USERNAME";
$password = "MY_PASSWORD";
$connection = mysqli_connect($host, $username, $password);
if(!$connection)
die("TRY AGAIN: " . mysqli_error());
$DBQuery = "USE MY_DB";
mysqli_query($connection, $DBQuery);
$queryResult = $connection->
query("SELECT * FROM MY_TABLE");
$result = array();
while ($fetchdata=$queryResult->fetch_assoc()) {
$result[] = $fetchdata;
}
echo json_encode($result);
?>
When i go to localhost/FILE_PATH/fetch.php
I get the full data in JSON form
When i upload it on server and go to https://www/MYSITE.com/FILE_PATH/fetch.php
I get the message TRY AGAIN:
Is there something im missing out while trying to retrieve message from server?
Im using aws rds mysql
Ive put my file inside var/www/html of my server. Am i supposed to put it in htdocs or something? if so, how do i reference to the file
I need simple web service to get data from local database, but when I've moved files from test server to actual server I just receive blank page with 'null' in top left corner.
My php code is very simple:
<?php
// Include config
require_once 'config/db.php';
$sql = new db();
$conn = $sql->connect();
$task = isset($_GET['task']) ? mysql_real_escape_string($_GET['task']) : "";
if(!empty($task))
{
if($task === "totals")
{
$qur = mysql_query("working query - no problem here;");
$result =array();
while($r = mysql_fetch_array($qur))
{
extract($r);
$result[] = array("total_value" => $total_value, 'orders_date' => $orders_date, 'number_of_orders' => $number_of_orders);
}
$json = $result;
}
}
#mysql_close($conn);
/* Output header */
header('Content-type: application/json');
echo json_encode($json, JSON_PRETTY_PRINT);
?>
And here is db.php code:
<?php
class db
{
// Properties
private $dbhost = 'ip-address';
private $dbuser = 'xxx';
private $dbpass = 'yyy';
private $dbname = 'zzz';
// Connect
public function connect()
{
$conn = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass);
mysql_select_db($this->dbname, $conn);
}
}
?>
May be Json_encode not giving correct answer.
1) Also please make sure JSON_PRETTY_PRINT is only available for PHP versions >= 5.4. It's value is 128, so try replacing JSON_PRETTY_PRINT with 128.
just on error reporting at top of page to debug
2) Also to make sure once try to print $json before encoding it
3) As mentioned in comment just encode only when you are actually getting data
!empty($json){
header('Content-type: application/json');
echo json_encode($json, JSON_PRETTY_PRINT);
}
I am creating a apps with sql stuffs and i am using a online database, everything works fine if i input this header("Access-Control-Allow-Origin: *");
But the next few lines i need this header as well header('Content-Type', 'text/plain');
Once i insert that and run my html and to run this particular php file, i will get this error
error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://www.gamestopica.net/andrew/login.php. This can be fixed by moving the resource to the same domain or enabling CORS.
anyone know what i should do to fit those 2 header under the same php file without the cross-origin error?
my php file
<?php
header("Access-Control-Allow-Origin: *");
include_once('db.php');
session_start();
header('Content-Type', 'text/plain');
$username = $_POST['username'];
$password = $_POST['password'];
$verify = 0;
$result = $db->query("SELECT * FROM `userdetails` WHERE `username` = '".$username."'")
or die("fail");
if(mysqli_num_rows($result)>0)
{
$row = mysqli_fetch_array($result);
$passwordDB = $row["Password"];
if($password == $passwordDB)
{
$_SESSION['user'] = $username;
$result2 = json_encode(
array("type"=>"true", "username"=>$_SESSION['user'])
);
echo $result2;
}
else
{
$result2 = json_encode(
array("type"=>"false")
);
echo $result2;
}
}
else
{
$result2 = json_encode(
array("type"=>"nothing")
);
echo $result2;
}
?>
Your call to header() is not valid syntax for that function. Try changing:
header('Content-Type', 'text/plain');
to
header('Content-Type: text/plain');
and see if that fixes your issue.
I have a website with the following code in the header - but the PHP echos in the body are returning anything:
<?php
session_start();
print_r($_SESSION);
$user = $_SESSION['email'];
$query = "SELECT * FROM first_page_data WHERE email_address= '$user' ";
$result = mysql_query($query);
$row_buyerdetails = mysql_fetch_assoc($result);
?>
The following returns nothing:
<?php echo $row_buyerdetails['phone_number'] ?>
I know the session variable named 'email' is receiving a value from the previous page from the print_r function on line 3. Variable $user is also getting the correct email address.
The database is set up correctly (ive been able to access successfully in other ways, but im trying to modify it to access the data related to a particular email address as shown).
If somebody could point me in the right direction id apprectiate it! Also as a side, how would people suggest debugging PHP other than littering the code with echos and print_r functions? Is there even a way to put breakpoints in for example?
EDITED FOR HELP IN THE ANSWER BELOW
As requested, this is the code with the alterations requested:
<?php
$hostname_first_data = "*****";
$database_first_data = "*****";
$username_first_data = "*****";
$password_first_data = "*****";
$first_data = mysql_pconnect($hostname_first_data, $username_first_data, $password_first_data) or trigger_error(mysql_error(),E_USER_ERROR);
echo mysql_errno($first_data) . ": " . mysql_error($first_data). "\n";
session_start();
print_r($_SESSION);
$user = $_SESSION['email'];
echo $user;
$query = "SELECT * FROM first_page_data WHERE email_address= '$user' ";
$result = mysql_query($query, $first_data);
$row_buyerdetails = mysql_fetch_assoc($result);
print_r($row_buyerdetails);
?>
Aren't you missing a mysql_connect call in your header or includes?
Try adding:
echo mysql_errno($link) . ": " . mysql_error($link). "\n";
$link being the resource you get from mysql_connect.
To debug PHP you have to install or activate an extension that is called Xdebug and use a nice IDE like PHPStorm, then Bob's your uncle :)
You can also use the Zend Debugger but I have limited experience with it.
You can (and should) also have full error reporting on when you are developing. It would tell you for example that the mysql_* functions are deprecated.
If you do not want the errors to appear on your page, you can choose to write to a log file and keep a tail open on that file.
Update for code:
<?php
$hostname_first_data = "*****";
$database_first_data = "*****";
$username_first_data = "*****";
$password_first_data = "*****";
$first_data = mysql_pconnect($hostname_first_data, $username_first_data, $password_first_data) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_first_data, $first_data);
session_start();
print_r($_SESSION);
$user = $_SESSION['email'];
echo $user;
$query = "SELECT * FROM first_page_data WHERE email_address= '$user' ";
$result = mysql_query($query, $first_data);
echo mysql_errno($first_data) . ": " . mysql_error($first_data). "\n";
$row_buyerdetails = mysql_fetch_assoc($result);
print_r($row_buyerdetails);
?>
Tell me what that version outputs...
I am currently using Phonegap along with xcode to make an IPhone App.
I'm just trying a simple Json call to get a query from a database (php) and it's returning Null.
I have Whitelisted the domain in the .plist file for phonegap. Here is m code:
$.getJSON('"http://slc0013.pickaweb.co.uk/~bengeor1/fixtures.php",', function(data) {
alert(data); //uncomment this for debug
//alert (data.item1+" "+data.item2+" "+data.item3); //further debug
$('#resultLog').html("<p>item1="+data.id+" item2="+data.home_team+" item3="+data.away_team+"</p>");
});
PHP code:
<?php
header("Access-Control-Allow-Origin: *");
ini_set('display_errors',1);
error_reporting(E_ALL);
// Set your return content type
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');
$db = "localhost";
$db_name = "xxx";
$db_user = "xxx";
$db_pwd = "xxxx";
$con = mysql_connect($db, $db_user, $db_pwd);
if (!$con) {
$status = 11; //database error
}
$db_selected = mysql_select_db($db_name, $con);
if (!$db_selected) {
}
$query = "SELECT * FROM Fixtures";
$result = mysql_query($query);
mysql_close();
$num = mysql_numrows($result);
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
echo json_encode($rows)
?>
If you run just the php file it displays the correct results.
Would appreciate your help.
Thanks.
Try to replace the first line:
$.getJSON('"http://slc0013.pickaweb.co.uk/~bengeor1/fixtures.php",', function(data) {
to this:
$.getJSON("http://slc0013.pickaweb.co.uk/~bengeor1/fixtures.php", function(data) {
Looks like you are having problem with url '"http://slc0013.pickaweb.co.uk/~bengeor1/fixtures.php",' should be "http://slc0013.pickaweb.co.uk/~bengeor1/fixtures.php" I guess