PHP: Fetching records after mysql query -


this elementary question, finding confusing. earlier used bind results , fetch them using while loop. using * in sql statement, hence doubt. here code:

$mysql = new mysqli("localhost", "user", "password", "database"); $sql = "select * mytable id = ?"; $stmt = $mysql->prepare($sql); $prm = $_post['txt']; $stmt->bind_param("i",$prm); $stmt->execute(); $stmt->store_result(); if ($stmt->num_rows > 0) {  } 

there around 20 columns in table, hence want avoid including column names in sql. how echo out columns of each record?

use get_result() instead of store_result(), , use result object's ->num_rows property check if returns row or not, this:

$mysql = new mysqli("localhost", "user", "password", "database"); $sql = "select * mytable id = ?"; $stmt = $mysql->prepare($sql); $prm = $_post['txt']; $stmt->bind_param("i",$prm); $stmt->execute();  $result = $stmt->get_result(); if ($result->num_rows > 0) {     while($row = $result->fetch_assoc()){          // code      } } 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -