Jump to content

PHP - MySQL - JSON wrong JSON format

Joveice
Go to solution Solved by Mr_KoKa,

Look, I will explain you step by step what you are doing:

 

if($is_query_run=mysql_query($query)){ //Here you execute the query
    while($query_execute=$query_execute=mysql_fetch_assoc($is_query_run)){ //Here you get one row from execution result

       $rows = array(); //You create array into $rows, what was in $rows before will be wiped
       $rows[] = $query_execute; // You insert one row you just fetch from result to $rows

       print json_encode($rows); // You encode $rows (which always has only one element as you wipe it every iteration)

   }
}

So, create array before loop, and encode that array after loop, adding elements to array inside a loop is ok.

I need to create a JSON string from my database request

do this code returns this :  [{"id":"1","weight":"0"}][{"id":"2","weight":"7"}][{"id":"3","weight":"1.5"}]  I need it to return  [{"id":"1","weight":"0"},{"id":"2","weight":"7"},{"id":"3","weight":"1.5"}] (the commas instead of ][) How can I do this?

<?php
require 'dbConnect.script.php';

$query="SELECT * FROM `trash`";
if($is_query_run=mysql_query($query)){
    while($query_execute=$query_execute=mysql_fetch_assoc($is_query_run)){

       $rows = array();
       $rows[] = $query_execute;

       print json_encode($rows);

   }
}

else{

   echo "query notexecuted";

}
?>

 

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

Do print json_encode after loop, not every iteration, it has no sense at it grows every iteration.

 

Just noticed that you wipe rows every iteration too. So what you are doing is clear $rows, then add one element to $rows and then json_encode it, so no surprise it endodes bunch of one element arrays.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Mr_KoKa said:

Do print json_encode after loop, not every iteration, it has no sense at it grows every iteration.

Okey so where do I put it?

I just tryed to place it right below the } for while but that just prints the last request

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

Look, I will explain you step by step what you are doing:

 

if($is_query_run=mysql_query($query)){ //Here you execute the query
    while($query_execute=$query_execute=mysql_fetch_assoc($is_query_run)){ //Here you get one row from execution result

       $rows = array(); //You create array into $rows, what was in $rows before will be wiped
       $rows[] = $query_execute; // You insert one row you just fetch from result to $rows

       print json_encode($rows); // You encode $rows (which always has only one element as you wipe it every iteration)

   }
}

So, create array before loop, and encode that array after loop, adding elements to array inside a loop is ok.

Link to comment
Share on other sites

Link to post
Share on other sites

Mysql has been dropped so I suggest you don't use it for any live website. You don't need to loop you can just fetch all and encode.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     $result= json_encode($sql->$result->fetch_assoc());
     echo $result;
} else {
     echo "0 results";
}

$conn->close();
?>  

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, vorticalbox said:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
     $result= json_encode($sql->$result->fetch_assoc());
     echo $result;
} else {
     echo "0 results";
}

$conn->close();
?>  

MYsql has been dropped so I suggest you don't use it for any live website. You don't need to loop you can just fetch all and encode.

Yea I have noticed this MySQLi things, tho I tryed to add it since the php is version 5.6.24 but that dident work, so I went back to MySQL. If this is due to the database then I guess it's becouse I use mariadb. if not how to fix?

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Joveice said:

Yea I have noticed this MySQLi things, tho I tryed to add it since the php is version 5.6.24 but that dident work, so I went back to MySQL. If this is due to the database then I guess it's becouse I use mariadb. if not how to fix?

 

Either way, the encoding fetch assoc should work even on mysql. I personally use PDO for my websites which as a slight different syntax but does the same thing.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, vorticalbox said:

Either way, the encoding fetch assoc should work even on mysql. I personally use PDO for my websites which as a slight different syntax but does the same thing.

By MySQL has been dropped what do you mean by that?

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

MySQL is deprecated, no future updates, dropped support. From what I have read marriadb should work as mysql db so all PHP connectors should support it, you can echo phpinfo(); to check if you have mysql or pdo installed

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, Mr_KoKa said:

MySQL is deprecated, no future updates, dropped support. From what I have read marriadb should work as mysql db so all PHP connectors should support it, you can echo phpinfo(); to check if you have mysql or pdo installed

API Extensions mysql,mysqli,pdo_mysql

Okey thank you, dident know about that :P

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, Joveice said:
API Extensions mysql,mysqli,pdo_mysql

Okey thank you, dident know about that :P

 

You can use my code and just change the details and the top and the SQL query and it should be good to go. You can move the database information to an external file once you have it working. 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, vorticalbox said:

You can use my code and just change the details and the top and the SQL query and it should be good to go. You can move the database information to an external file once you have it working. 

No worrys, when I now visit read.php it returns  [{"id":"1","weight":"4"},{"id":"2","weight":"7"},{"id":"3","weight":"1.5"}].

I have the connection in a db_connect.php file.

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Joveice said:

No worrys, when I now visit read.php it returns  [{"id":"1","weight":"4"},{"id":"2","weight":"7"},{"id":"3","weight":"1.5"}].

I have the connection in a db_connect.php file.

 

So problem solved then? 9_9

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, vorticalbox said:

So problem solved then? 9_9

Yep :)

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

On 2016-11-11 at 8:10 PM, Mr_KoKa said:

MySQL is deprecated, no future updates, dropped support. From what I have read marriadb should work as mysql db so all PHP connectors should support it, you can echo phpinfo(); to check if you have mysql or pdo installed

I had no idea of this, do you have a source for this? I'm not saying you're wrong just wanted to see if there are any articles on the matter since I had not heard of it before. When did they stop supporting it?

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, lubblig said:

I had no idea of this, do you have a source for this? I'm not saying you're wrong just wanted to see if there are any articles on the matter since I had not heard of it before. When did they stop supporting it?

I mean php extension not mysql server itself. http://php.net/manual/en/migration55.deprecated.php

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Mr_KoKa said:

I mean php extension not mysql server itself. http://php.net/manual/en/migration55.deprecated.php

Oh, thank god! I have just started (a couple of weeks ago) a project myself with a MySQL DB and it took me a while to setup and follow a bunch of different guides to secure the DB and the server itself. I'm using the MySQLi one so I guess I'm fine? Thank you for the explanation!

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×