mysqli - Printing an entire SQL result from a prepared statement in PHP -


i'm trying build function allows me abstract use of mysqli functions in code. have working version using standard sql (no prepared statements).

function mysqlquerytoarray($con, $sql){     // process sql query     $result = mysqli_query($con, $sql);      if(mysqli_error($con)){         out($sql . "\n");         exit("error: mysql error: " . mysqli_error($con));     }      // put result  2d array     $output = array();     while($row = mysqli_fetch_assoc($result)) {         array_push($output, $row);     }      return $output; } 

now i'm trying translate code can use prepared statements research has revealed when using prepared statements need bind each column different variable using mysqli_stmt::bind_result causes problem because point of function work arbitrary sql query.

my main question this: there way print out entire output sql query in 2d array same function above using prepared statements?

here's current code using prepared statements, has bind_result in there.

//creates mysql query, gets result , returns 2d array results of query function mysqlquerytoarray($con, $sql, $params){     // prepare sql query     $stmt = $con->prepare($sql);      if(mysqli_error($con)){         echo "$sql=>\n" . print_r($params, true) . "\n";         exit("$sql=>\n" . print_r($params, true) . "\nerror: mysql error: " . mysqli_error($con));     }      // bind parameters     foreach($params $param){         $type = "s";         if(gettype($param) == 'integer'){             $type = "i";         }         $stmt->bind_param($type, $param);     }      //execute query     $stmt->execute();      // put result  2d array     $output = array();     // ??? need bind results , them array somehow      //close prepared statement     $stmt->close();      return $output; } 

pdo turns out answer. feel should post code created solve problem. @don'tpanic tip.

function pdoquery($sql, $params){     $db = new pdo('mysql:host=localhost;dbname=my_database;charset=utf8', 'username', 'password', array(pdo::attr_emulate_prepares => false, pdo::attr_errmode => pdo::errmode_exception));     try {         $stmt = $db->prepare($sql);         $stmt->execute($params);         $rows = $stmt->fetchall(pdo::fetch_assoc);         return $rows;     }catch(pdoexception $ex){         echo "error: sql error: $sql";         // logerror("error: sql error: $sql");     } } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -