php - Pass table variable to SELECT function -
i have getdata()
function, , database 2 tables: employers , members. pass variable containing table name, inside "if" execute appropriate select statement. problem believe after "if" $stmt->bind_param();
doesn't know $stmt
bind take. ideas on how achieve this?
thanks
public function getdata($table) { if ($table == "employers") { $stmt = $this->link->prepare("select * employers "); } else { $stmt = $this->link->prepare("select * members "); } $stmt->bind_param(); if ($stmt->execute()) { $result = $stmt->get_result(); while($row = $result->fetch_array(mysqli_assoc)) { $row = array_map('stripslashes', $row); $dataarray[] = $row; } } return $dataarray; }
no, since aren't binding anything, ->bind_param
method superfluous. take off.
public function getdata($table) { $dataarray = array(); $t = ($table === 'employers') ? 'employers' : 'members'; $query = "select * $t"; $stmt = $this->link->prepare($query); if($stmt->execute()) { $result = $stmt->get_result(); while($row = $result->fetch_assoc()) { $row = array_map('stripslashes', $row); $dataarray[] = $row; } } return $dataarray; }
sample usage:
$data = $aministrator_query->getdata('members'); $tbody = ''; foreach($data $row) { $tbody .= "<tr><td>{$row['user_id']}</td><td>{$row['user_password']}</td><td>{$row['user_first_name']}</td><td>{$row['user_last_name']}</td></tr>"; } $table = sprintf('<table><thead><tr><th>id</th><th>password</th><th>first name</th><th>last name</th></tr></thead><tbody>%s</tbody></table>', $tbody); echo $table;
Comments
Post a Comment