0

I have an associative array. After I select my records from my table (with two columns: objName,objCost), I want to save them in my array like this:

array(
      'objName'=>$row['objName'],
      'objCost'=>$row['objCost']
)

How should I do this?

This is my code:

$output = '';
$arr = array();
$sql = "SELECT * FROM obj WHERE objName LIKE '%" . $_POST["search"] . "%'";
$result = $db->query($sql) or die(mysql_error());
if ($result->rowCount() != 0) {
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
   //here i should insert my rows into my array        
}
$json_arr = json_encode($arr,JSON_UNESCAPED_UNICODE);
echo $json_arr;
} else {
echo 'Data Not Found';
}
ArK
  • 20,698
  • 67
  • 109
  • 136
mr_simti
  • 9
  • 1
  • 9
  • **WARNING**: When using PDO you should be using [prepared statements](http://php.net/manual/en/pdo.prepared-statements.php) with placeholder values and supply any user data as separate arguments. In this code you have potentially severe [SQL injection bugs](http://bobby-tables.com/). Never use string interpolation or concatenation and instead use [prepared statements](http://php.net/manual/en/pdo.prepared-statements.php) and never put `$_POST` or `$_GET` data directly in your query. Refer to [PHP The Right Way](http://www.phptherightway.com/) for guidance with this and other problems. – tadman Nov 02 '16 at 00:44

1 Answers1

0

According to your code,

$arr[] = ['objName'=>$row['objName'],'objCost'=>$row['objCost']];

Then you can encode the array and pick up the object on the other side with javascript or php. Which ever suits you

json_encode($arr);

In you Ajax success, get the objects and use the values as you deem fit

success: function (data) {data = JSON.parse(data); 

  for(var i = 0; i < data.length; i++){
    alert(data[i].objName);
   }
}

see jQuery loop over JSON result from AJAX Success? on how to loop through your results in jquery

Community
  • 1
  • 1
Mueyiwa Moses Ikomi
  • 1,069
  • 2
  • 12
  • 26
  • actually i wanna use this in a live search using ajax.i try ur code.but it returns just one record – mr_simti Nov 01 '16 at 22:51
  • This has to do with the way you are fetching. Have you tried to Fetch All? – Mueyiwa Moses Ikomi Nov 01 '16 at 22:54
  • i tried this way :$arr[$row['objName']] = $row['objCost']; the output is this: Object {benz: 11111, lamborgini: 400} – mr_simti Nov 01 '16 at 23:07
  • the way you told has this output: Object {objName: "benz", objCost: 11111} – mr_simti Nov 01 '16 at 23:09
  • see, you're to fetch all results and not just on with while ($row = $result->fetchAll(PDO::FETCH_ASSOC)) { $arr[] = ['objName'=>$row['objName'],'objCost'=>$row['objCost']]; json_encode($arr); } You now have to pick up the object in your ajax javascript result. Can i see that code? – Mueyiwa Moses Ikomi Nov 01 '16 at 23:13
  • – mr_simti Nov 01 '16 at 23:22
  • on success, get the objects and use them eg: success: function (data) {data = JSON.parse(data); } – Mueyiwa Moses Ikomi Nov 01 '16 at 23:27
  • sorry.i'm beginner.how can I use it?for example I want to get objName – mr_simti Nov 01 '16 at 23:35
  • i just added an example to my answer above – Mueyiwa Moses Ikomi Nov 01 '16 at 23:36
  • i tried this:console.log(data); data = JSON.parse(data); console.log(data); the output: [Object, Object, Object, Object, Object, Object] VM2973:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1(…) – mr_simti Nov 01 '16 at 23:41
  • it's ok now. i used console.log(JSON.stringify(data)); thanks alot for ur help – mr_simti Nov 01 '16 at 23:47