I am trying to convert data from my DB to JSON with a PHP script.
Everything works great on my local environment (MAMP).
As soon as I am trying to put things together on my server, my JSON File is empty :(
What I have so far
my php script
<?php
$connection = mysqli_connect("localhost","root","root","angulardb") or die("Error " . mysqli_error($connection));
$sql = "select * from postings";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
$fp = fopen('menue.json', 'w');
fwrite($fp, json_encode($emparray));
fclose($fp);
echo json_encode($emparray);
?>
By uploading this script to my server (& update the data), I receive an empty JSON file with no response.
Any Idea what can be wrong???
Is there additional configuration necessary on the server side?
Thanks in advance
Ok,
got it .
the missing chsrset=utf8 caused my problem!
$pdo=new PDO("mysql:dbname=db239256x2330361;host=mysql.webhosting38.1blu.de;charset=utf8","s239256_2330361","20dj93lpav+dd");
Related
I am trying to receive data back from a mysql db but in my results I receive escape characters. How can I get rid of these and just return raw data (I'm a noob in this realm of coding so please keep that in mind)?
Here is a sample of my result (it's a json signature):
{"signature":"\n\t\t\t[{\"lx\":16,\"ly\":16,\"mx\":16,\"my\":15},{\"lx\":16,\"ly\":17,\"mx\":16,\"my\":16},{\"lx\":16,\"ly\":18,\"mx\":16,\"my\":17},{\"lx\":16,\"ly\":20,\"mx\":16,\"my\":18},{\"lx\":16,\"ly\":21,\"mx\":16,\"my\":20},{\"lx\":16,\"ly\":22,\"mx\":16,\"my\":21},{\"lx\":16,\"ly\":25,\"mx\":16,\"my\":22},{\"lx\":16,\"ly\":26,\"mx\":16,\"my\":25},.....]\n\t\t\t"}
I would like to remove the: {"signature":"\n\t\t\t on the beginning and \n\t\t\t"} on the end automatically.
Any help is greatly appreciated!
<?php
{
// Connect to MySQL
$mysqli = new mysqli( 'localhost', 'root', 'rootpassword', 'crs' );
//Check our connection_aborted
if ($mysqli->connect_error ) {
die( 'Connect Error: ' . $mysqli->connect_error );
}
//Read the signature
$sql = "SELECT signature FROM rescue_forms WHERE id=53";
$idresult = $mysqli->query($sql);
while($row = mysqli_fetch_assoc($idresults))
$id = $row;
//Just printing this so I can see the results.
print json_encode($id);
//sending this to a statement below.
$json = json_encode($id);
//Close connection
$mysqli->close();
}
?>
You are storing json in a database, which you shouldn't,
and for some reason you are encoding your json again, which is pointless.
In a round about way of down voting my question and basically telling me I'm an idiot, it helped! (which is the point of the forum) In hopes that this might help someone else that might just be starting out coding this kind of thing. I was able to change my array around to get the raw data as I was expecting, here's my example:
<?php
{
// Connect to MySQL
$mysqli = new mysqli( 'localhost', 'root', 'rootpassword', 'crs' );
//Check our connection_aborted
if ($mysqli->connect_error ) {
die( 'Connect Error: ' . $mysqli->connect_error );
}
//Read the signature
$sql = "SELECT signature FROM rescue_forms WHERE id=53";
$idresult = $mysqli->query($sql);
while($sig=mysqli_fetch_assoc($idresult)){
echo "<tr>";
echo "<td>".$sig['signature']."</td>";
echo "</tr>";
}
//Close connection
$mysqli->close();
}
?>
Thank you to all that responded it was greatly appreciated! Also thank you #YourCommonSense for showing me the proper way to format my code in this window with your edit.
my php web service is not giving me json result from my database when i run it in web browser or i try to connect to it from my android app. When i test it through postman it gives me back results i json.
Here is my php code:
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
include 'connection.php';
showStudent();
}
function showStudent()
{
global $connect;
$query = " Select * FROM demo; ";
$result = mysqli_query($connect, $query);
$number_of_rows = mysqli_num_rows($result);
$temp_array = array();
if($number_of_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$temp_array[] = $row;
}
}
header('Content-Type: application/json');
echo json_encode(array("demo"=>$temp_array));
mysqli_close($connect);
}
?>
here is my result from database through postman:
whil in web browser im not getting any of this:
can anybody see any problem with this, im actually not a php developer i tried to wrote this on my own for a project.
Thanks
You are checking for the method of submission first which is POST. while when you hit direct url it work as GET. So for getting data in web change POST to GET.
<?php
if($_SERVER["REQUEST_METHOD"]=="GET"){
include 'connection.php';
showStudent();
}
Using Johnboy's tutorial on importing .csv files to MySQL, I tried to make a script that would take exchange rate data from Yahoo finance, and write it to the MySQL database.
<?php
//connect to the database
$host = "****"
$user = "****"
$password = "****"
$database = "****"
$connect = mysql_connect($host,$user,$password);
mysql_select_db($database,$connect);
//select the table
if ($_FILES[csv][size] > 0) {
//get the csv file
$symbol = "ZARINR"
$tag = "l1"
$file = "http://finance.yahoo.com/d/quotes.csv?e=.csv&f=$tag&s='$symbol'=x";
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
mysql_query("INSERT INTO ZAR_to_INR(exchange_rate) VALUES
(
'".addslashes($data[0])."',
)
");
echo "done";
}
} while ($data = fgetcsv($handle,1000,",","'"));
//redirect
header('Location: import.php?success=1'); die;
}
else{
echo "nope";
}
?>
I added the echos in the hope that they'd tell me whether or not the script worked. It doesn't work at all. There are no error messages or anything. When I run the script by opening it in my webhost, it simply does not run.
I'd appreciate any advice on how to make this script work (or even an alternate way of solving the problem).
try using mysql debugs :
mysql_select_db($database) or die('Cant connect to database');
$result = mysql_query($query) or die('query fail : ' . mysql_error());
$connect = mysql_connect($host,$user,$password)
or die('Cant connect to server: ' . mysql_error());
to find this outputs you need to check your php error_log : where-does-php-store-the-error-log
Hello friends please help me for following question
How to pass data in JSON and how to get JSON data in php using
following code
eg:
$.getJSON("url",function(data)
{
alert(data);
});
php file:::::
<?php how to get JSON data here? ?>
The below is a sample server side code i.e. php code (you asked for php)
This includes fetching data from sql and encoding it into JSON format and returning it to client.
File content of testAJAX_getJson.php
$con =
mysql_connect("localhost","peter","abc123");
if (!$con) { die('Could not
connect: ' . mysql_error()); }
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM
Persons");
while($row =
mysql_fetch_array($result)) {
$data[] = $row; }
echo json_encode($data);
Not sure if I can do it this way. I want to get current snapshot of the database and send it via FTP Server, both of this functionality should be implemented in PHP scripts.
Here are the steps I am thinking on right now.
In my php scripts(basically am extending an PDO into my Dao class and then preparing the query),
$qry = SELECT * FROM MyTablename;
$stmt = $this->prepare($qry);
$stmt = $this->execute();
Now I will store $stmt in csv file using fputcsv or I will execute the sql command from the script itself and than try to store the result in the $file(csv file) note here that I do not have any csv file with me at this point to basically I will have to create one and let's say its $file, so then
$file = fputcsv($stmt); or $file = exec("Select * from MyTablename");
Will this put all records in the file ? If yes, then I will use FTP Functionality to transfer file to the FTP Folder.
I am not sure if this approach would work and also have concerns regarding the need of preparing the $qry
Any suggestions or different approach advised would be highly appreciated.
Thanks !!!
try something like this:
mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$list = array();
$q = mysql_query("SELECT * from table");
while($row = mysql_fetch_assoc($q)){
$list[] = $row;
}
$fp = fopen("file.csv", 'w+');
foreach ($list as $line) {
fputcsv ($fp, implode(',', $line));
}
then try:
$conn_id = ftpconnect('host',21);
$login_result = ftp_login($conn_id, 'user_name', 'user_pass');
ftp_fput($conn_id, 'file.csv', $fp, FTP_ASCII);
ftp_close($conn_id);
fclose($fp);
I write the code without testing it but should be something like that ;)
good luck.