×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Text
Posted by: Scott Hansen
Added: Oct 9, 2017 6:49 PM
Modified: Oct 9, 2017 6:53 PM
Views: 2535
Tags: get;function
  1. function getSomething($id=0){
  2.         //returns data from database
  3.  
  4.             $stmt = $this->dbh->prepare("SELECT * "
  5.                 . "FROM table WHERE Id = ?");  
  6.             $stmt->bind_param("i", $id);
  7.        
  8.         if(!$stmt->execute()){
  9.             $a['status']="fail";
  10.         }
  11.        
  12.             $results = $stmt->get_result();
  13.             $count = $results->num_rows;
  14.            
  15.             if($count == 0){
  16.                                
  17.                 $a['status']="fail";
  18.                 $a['message']="No Results Found";
  19.             }else{
  20.                 $a['status']="success";
  21.                
  22.                 while($row = $results->fetch_assoc()){
  23.                     $a['data'][] = $row;
  24.                 }
  25.             }
  26.             $json = json_encode($a);
  27.             return $json;
  28.         }