php can't respond json - php

Why a php file does not respond as json, but it responds to the connected file.
file respond
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
include '../connect.php';
mysqli_set_charset($conn, "utf8");
$sql="SELECT confer_name, confer_begin_time, confer_end_time, confer_value, con_img FROM conference";
$outp = "";
$result = mysqli_query($conn,$sql);
if(mysqli_num_rows($result)>0){
while($rs = $result->fetch_array(MYSQLI_ASSOC)){
if($outp != ""){$outp .=",";}
$outp .='{"cname":"' .$rs["confer_name"]. '",';
$outp .='"cbt":"' .$rs["confer_begin_time"] . '",';
$outp .='"cet":"' .$rs["confer_end_time"] . '",';
$outp .='"cvalue":"' .$rs["confer_value"] . '",';
$outp .='"cimg":"' .$rs["con_img"] . '"}';
}
$outp = '{"records":['.$outp.']}';
echo $outp;
}
?>//***
file connect This file is used to connect.
<?php
$servername = "localhost";
$username = "----";
$password = "----";
$database = "----";
$conn = new mysqli($servername,$username,$password,$database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
This result

modify your code as below
$outp = json_encode($outp);

Related

problem to show data mysql to json with php

i have a Persian data base that use collection utf8-general-ci but when i run below code i face with ???? instated of Persian words .
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$conn = new mysqli("localhost", "root", "", "testinsta");
$result = $conn->query("SELECT m_1, m_2, m_3 FROM codecode");
mysqli_query($conn, "SET NAMES utf8");
$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
if ($outp != "") {$outp .= ",";}
$outp .= '{"Name":"' . $rs["m_1"] . '",';
$outp .= '"City":"' . $rs["m_2"] . '",';
$outp .= '"Country":"'. $rs["m_3"] . '"}';
}
$outp ='{"records":['.$outp.']}';
$conn->close();
echo($outp);
?>
mysql to json with php problem
You can do like this,
$conn = new mysqli("localhost", "root", "", "testinsta");
$conn->set_charset("utf8");
and remove below line,
mysqli_query($conn, "SET NAMES utf8");
and now check.
Here is brief details about Whether to use “SET NAMES”.

Problem getting results with php and mysqli

I've a problem by running this php script:
<?php
$link = mysqli_connect("localhost", "root", "*******", "adsb");
/* check connection */
if ($conn->connect_error) {
die("Connection failed: " . $link->connect_error);
}
$query = "SELECT aircrafts.id, heli.reg, heli.hex, heli.typ, heli.opp, aircrafts.flight, aircrafts.altitude, aircrafts.lat, aircrafts.lon, aircrafts.squawk, aircrafts.message_date FROM `aircrafts` JOIN `heli` ON aircrafts.hex=heli.hex WHERE aircrafts.id='2414';";
$query .= "SELECT aircrafts.id, plane.reg, plane.hex, plane.typ, plane.opp, aircrafts.flight, aircrafts.altitude, aircrafts.lat, aircrafts.lon, aircrafts.squawk, aircrafts.message_date FROM `aircrafts` JOIN `plane` ON aircrafts.hex=plane.hex WHERE aircrafts.id='2414'";
$result = mysqli_multi_query($query);
/* execute multi query */
if ($result->num_rows > 0) {
echo "<h1>INFO ABOUT FLIGHT RECORD " . $id . "</h1>";
echo "<table><th>Registratie</th><th>ICAO24</th><th>Type</th><th>Operator</th><th>Callsign</th><th>Squawk</th><th>Time</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td><a href='aircraft.php?hex=" . $row["hex"] . "'>" . $row["reg"] . "</a></td><td>" . $row["hex"] . "</td><td><img src='/database/SilhouttesLogos/" . $row["typ"] . ".bmp' /></td><td><img src='/database/OperatorFlags/" . $row["opp"] . ".bmp' /></td><td>" . $row["flight"] . "</td><td>" . $row["squawk"] . "</td><td>" . $row["message_date"] . "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
/* close connection */
mysqli_close($link);
When I run the 2 query's in phpmyadmin I get 1 result.
(When I run 1 query on my site I get a result too, see code at the bottom of this post)
But when I run it on my site it's shows "0 results".
So... There must be a fould in the $result section.
Who can help? :-)
<?php
$servername = "localhost";
$username = "root";
$password = "*****";
$dbname = "adsb";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT aircrafts.id, heli.reg, heli.hex, heli.typ, heli.opp, aircrafts.flight, aircrafts.altitude, aircrafts.lat, aircrafts.lon, aircrafts.squawk, aircrafts.message_date FROM `aircrafts` JOIN `heli` ON aircrafts.hex=heli.hex WHERE aircrafts.id=2414";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><th>Registratie</th><th>ICAO24</th><th>Type</th><th>Operator</th><th>Callsign</th><th>Squawk</th><th>Time</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td><a href='aircraft.php?hex=" . $row["hex"] . "'>" . $row["reg"] . "</a></td><td>" . $row["hex"] . "</td><td><img src='/database/SilhouttesLogos/" . $row["typ"] . ".bmp' /></td><td><img src='/database/OperatorFlags/" . $row["opp"] . ".bmp' /></td><td>" . $row["flight"] . "</td><td>" . $row["squawk"] . "</td><td>" . $row["message_date"] . "</td></tr>";
}
} else {
echo "0 results";
}
$conn->close();
This is probably your specific problem.
$result = mysqli_multi_query($query);
Here is the PHP Doc
You need to establish the connection.
$result = mysqli_multi_query($link, $query);
Like the others mentioned in the comments, I would refactor this code.

how can i make a json with category in mysql?

i need to make a page which show all the items and when clicked bring me all the places where it is available.
i need to make this in mysql
app.controller('namesCtrl', function($scope) {
$scope.items = [
{name: 'item1', place: ['place1', 'place2']},
{name: 'item2', place: ['place2', 'place3']},
{name: 'item3', place: ['place1', 'place2', 'place3']},
{name: 'item4', place: ['place1']},
{name: 'item5', place: ['place1', 'place2']}
];
});
my php is
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$conn = new mysqli("localhost", "user", "pass", "table");
$result = $conn->query("SELECT * FROM `especialidades`");
$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
if ($outp != "") {$outp .= ",";}
$outp .= '{"id":"' . $rs["id"] . '",';
$outp .= '"nombre_especialidad":"' . $rs["nombre_especialidad"] . '",';
$outp .= '"aguadilla":"' . $rs["aguadilla"] . '",';
$outp .= '"arecibo":"' . $rs["arecibo"] . '",';
$outp .= '"bayamon":"' . $rs["bayamon"] . '",';
$outp .= '"caguas":"' . $rs["caguas"] . '",';
$outp .= '"carolina":"' . $rs["carolina"] . '",';
$outp .= '"guayama":"' . $rs["guayama"] . '",';
$outp .= '"hato_rey":"' . $rs["hato_rey"] . '"}';
}
$outp ='{"especialidades":['.$outp.']}';
$conn->close();
echo($outp);
?>
im getting a lot of error.
how is the correct whay to make this in mysql and get it in json.
Your code should be like this
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$conn = new mysqli("localhost", "user", "pass", "table");
$result = $conn->query("SELECT id, nombre_especialidad,aguadilla,arecibo, bayamon,caguas,carolina,guayama,hato_rey FROM `especialidades`");
$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
$outArray["especialidades"][] = $rs;
}
$conn->close();
$outp = json_encode($outArray);
echo $outp;
?>
Hope this will help you!!

PHP CSV export from stored MySQL Procedure

Trying to use the below code to call a stored procedure on MySQL and export to CSV. It generates a csv file however it is blank. The stored procedure is called DATAEXPORT1
Any ideas would be grateful!
<?php
$host = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
$file = 'export';
$connection = mysqli_connect($host, $username, $password, $dbname) or die("Connection Error " . mysqli_error($connection));
/*
* execute sql query
*/
$sql = "Call DATAEXPORT1()";
$result = mysqli_query($connection,$sql) or die("Selection Error " . mysqli_error($connection));
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$csv_output .= $row['Field'].", ";
$i++;
}
}
$csv_output .= "\n";
$values = mysqli_query($connection,$sql);
while ($rowr = mysqli_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j].", ";
}
$csv_output .= "\n";
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $csv_output;
mysqli_close($connection);
exit;
?>
Used the following code which works nicely:
<?php
$host = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
$connection = mysqli_connect($host, $username, $password, $dbname) or die("Connection Error " . mysqli_error($connection));
// fetch mysql table rows
$sql = "CALL DATAEXPORT1()";
$result = mysqli_query($connection, $sql) or die("Selection Error " . mysqli_error($connection));
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="output1.csv"');
$file = fopen('php://output', 'w');
while($row = mysqli_fetch_assoc($result))
{
fputcsv($file, $row);
}
fclose($file);
//close the db connection
mysqli_close($connection);
exit();
I only modify little your script
<?php
$host = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
$file = 'export';
$connection = mysqli_connect($host, $username, $password, $dbname) or die("Connection Error " . mysqli_error($connection));
/*
* execute sql query
*/
$sql = "Call DATAEXPORT1()";
$result = mysqli_query($connection,$sql) or die("Selection Error " . mysqli_error($connection));
$num_rows = count($result);
$csv_output = '';
if( $num_rows > 0) {
foreach($result as $item => $value){
$csv_output .= $item . ", " . $value . "\n";
$i++;
}
}
$filename = $file . "_" . date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $csv_output;
mysqli_close($connection);
exit;
?>

Web services login page in PHP

if($_SERVER['REQUEST_METHOD'] == "GET"){
// Get post data`
$username = isset($_POST['username']) ? mysql_real_escape_string($_POST['username']) : "";
$password = isset($_POST['password']) ? mysql_real_escape_string($_POST['password']) : "";
//$status = 1;
// Here we set by default status In-active.
// Save data into database
//$resultarray = array();
$query = "select admin_user , admin_password from doc_admin where admin_user = '".$username."' and admin_password = '".$password."'";
$insert = mysql_query($query) or die (mysql_error());
if (mysql_num_rows($insert)>0)
{
while ($rs = mysql_fetch_assoc($insert))
{
$outp .= '{"id":"' . $rs["id"] . '",';
$outp .= '"admin_user":"' . $rs["admin_user"]. '",' ;
$outp .= '"admin_password":"' . base64_encode($rs["admin_password"]). '",' ;
$outp .= '"admin_status":"' . $rs["admin_status"] . '",';
$outp .= '"admin_data":"' . $rs["admin_data"] . '",';
$outp .= '"admin_calender":"' . $rs["admin_calender"] . '",';
$outp .= '"admin_suggestion":"' . $rs["admin_suggestion"] . '",';
$outp .= '"admin_agenda":"'. $rs["admin_agenda"] . '"}';
// array_push($resultarray,$result);
}
}
}
mysql_close($conn);
/* JSON Response */
header("Content-type: application/json");
echo json_encode($outp);
I want to show data of logged in person but it showing null value, but if it get null value in database it showing result in JSON format.
<?php
if($_SERVER['REQUEST_METHOD'] == "GET"){
// Get post data`
$username = isset($_POST['username']) ? mysql_real_escape_string($_POST['username']) : "";
$password = isset($_POST['password']) ? mysql_real_escape_string($_POST['password']) : "";
//$status = 1;
// Here we set by default status In-active.
// Save data into database
//$resultarray = array();
$query = "select * from doc_admin where admin_user = '".$username."' and admin_password = '".$password."'";
$insert = mysql_query($query) or die (mysql_error());
if (mysql_num_rows($insert)>0)
{
$rs = mysql_fetch_assoc($insert);
$outp = json_encode($rs);
} else {
$outp = json_encode(array());
}
}
mysql_close($conn);
/* JSON Response */
header("Content-type: application/json");
echo json_encode($outp);
?>

Categories