0

Hi kinda new on JSON so please bear with my stupidty.. I'm developing a chat application and I'm trying to use json on my working php script.. What I've tried so far is that after I execute my database query I encode the result as a JSON then after that I use json_decode then Insert it on my foreach statement Here is the php code with both json_encode and json_decode with it

    <?php
session_start();
$username=$_SESSION['username'];
$user_id = $_SESSION['id'];
  require "config.php";
  $con = new PDO("mysql:host=".db_host.";dbname=chat_db",db_username,db_password);
  $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $sql="SELECT id,firstname,lastname,status,flag,flag2 FROM users WHERE id != :uid";
  $stmt=$con->prepare($sql);
  $stmt->bindValue("uid", $user_id, PDO::PARAM_STR);
  $stmt->execute();

  $sql2="UPDATE users SET status= 1 WHERE status = 0 AND id = :uid";
  $stmt2=$con->prepare($sql2);
  $stmt2->bindValue("uid", $user_id, PDO::PARAM_STR);
  $stmt2->execute();
$rows =$stmt->fetchAll();
$str = json_encode($rows);
$str2 = json_decode($str);
  foreach($str2 as $row){
  $uid = $row['id'];

  $firstname = $row['firstname'];
  $lastname = $row['lastname'];
  $sts = $row['status'];
  $flg = $row['flag'];
  $flg2 = $row['flag2'];
?>
<li>
  <a href="#" data-usernem="<?php echo $username; ?>" data-suid="<?php echo $user_id; ?>" data-userid="<?php echo $uid; ?>" data-role="button"><?php echo $firstname . PHP_EOL . $lastname; ?> </a>

  <span class="typing-stats idle" data-type-status="<?php echo $flg2; ?>"> Typing... </span>
  <span class="mail msg" data-flag="<?php echo $flg; ?>"> <a href="#" data-role="button"><i class="fa fa-envelope-o"></i></a> </span>
  <span class="bullet" data-status="<?php echo $sts; ?>"> &nbsp; </span>
</li>
<?php
  }
?>

If this script executes it gives me an error Fatal error: Cannot use object of type stdClass as array in D:\site_deploy\chat\includes\loadusers.php on line 22

Am I using json_encode and json_decode correctly? I'm pretty sure I followed whats written on php manual under json_decode

I have tried to var_dump it and it gives me the set of data that was encoded FYI

7
  • Why are you encoding and decoding? You could just loop over $rows directly. Commented Apr 8, 2014 at 1:33
  • because I checked my firebug console under its net tab the size it transfers is already in 1.1kb I'm trying to minimize its size.. can this be done using json_decode and json_encode? because some says json is lighter which is why I'm converting all my scripts into JSON format.. am I wrong on my theory? Commented Apr 8, 2014 at 1:37
  • Yes, you are wrong. JSON is "lighter" if you just send the data to the browser (just the row data using ajax for example) and then build the html in javascript. However, the output you are sending to the browser is html and is exactly the same whether you use JSON or not. It will just be a bit slower now due to two extra function calls. Enabling compression (if it is not enabled already) would do a lot more here. Commented Apr 8, 2014 at 1:38
  • oh sorry... so what can you suggest to me? Because I thought JSON can help me solve my problem Commented Apr 8, 2014 at 1:41
  • 1
    No problem. By the way, the size of the html you send to the browser is probably nothing compared to the images you include (in general, I don't know your site...) so make sure you optimize the right things and don't waste time where you can hardly win anything. Commented Apr 8, 2014 at 1:45

1 Answer 1

1

Use the parameter true to convert it to an array.

$str2 = json_decode($str, true);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.