$query = mysqli_query($con, $sql); if (!$query && !mysqli_num_rows($query)) { throw new Exception('Error in SQL'); } $i = 1; $ret = array(); while ($row = mysqli_fetch_assoc($query)) { if ($row['ct'] > $row['lt']) { $direction = 'up'; $color = 'green'; } else { $direction = 'down'; $color = 'red'; } $arr[$i] = array($row['ct'], $direction, $color); array_push($ret, $arr[$i]); $i++; } mysqli_close($con); return $ret; } try { $arr = getstockprice(); for ($i = 0; $i < 10; $i++) { $stock_price[$i] = $arr[$i][0]; $direction[$i] = $arr[$i][1]; $color[$i] = $arr[$i][2]; } } catch (Exception $ex) { echo 'Caught exception: ', $ex->getMessage(), "\n"; }
<?php include 'db_connect.inc.php'; try { global $flag; $flag = 0; if (isset($_POST['sid'])) { $stock_id = $_POST['sid']; //$arr = getstockprice($stock_id); $arr = getstockprice($stock_id); $json = json_encode($arr); echo $json; } } catch (Exception $ex) { echo 'Caught exception: ', $ex->getMessage(), "\n"; } function getstockprice($par) { global $con; $sql = 'SELECT `current_stock_price` as ct , `last_stock_price` as lt from stocks where stock_id =' . $par; $query = mysqli_query($con, $sql); if (!($row = mysqli_fetch_assoc($query))) { throw new Exception('Error in SQL.'); } mysqli_close($con); if ($row['ct'] > $row['lt']) { $direction = 'up'; } else { $direction = 'down'; } $ret = array($row['ct'], $direction);