MYSQL PHP PDO Fill table with database data -
i want give out every line in database. seems working returns first column.
note: there values empty fields in database!
$columns = "ticket, last_modified_date, requester";
heres code:
function getticket($columns){ echo($columns); global $db_host, $db_name, $db_user, $db_pass; $db = new pdo("mysql:host=$db_host;dbname=$db_name;charset=utf8", "$db_user", "$db_pass"); $result = $db->prepare("select $columns tickets"); if ($result->execute()){ echo("<b>successfully!</b><br><br>"); $rows = $result->fetchall(pdo::fetch_assoc); $col_names = explode(',', $columns); foreach($rows $row){ echo("<tr>"); foreach($col_names $col_name){ if (!isset($row[$col_name])){ continue; } echo("<td>".$row[$col_name]."</td>"); } echo("</tr>"); } } else{ echo("<b>failed!</b><br><br>"); } $db = null; }
there couple of issues here, first should use ->fetchall()
return result rows in 1 hit array.
second overwriting $result statement handle row being returned ->fetch
function getticket($columns){ global $db_host, $db_name, $db_user, $db_pass; $db = new pdo("mysql:host=$db_host;dbname=$db_name;charset=utf8", "$db_user", "$db_pass"); $result = $db->prepare("select $columns tickets"); if ($result->execute()){ echo("<b>successfully!</b><br><br>"); // changes here $rows = $result->fetchall(pdo::fetch_assoc); $col_names = explode(',', $columns); //make array of csv foreach($rows $row){ foreach( $col_names $col_name ) { echo '<td>' . $row[$col_name] . '</td>'; } } }else{ echo("<b>failed!</b><br><br>"); } $db = null; }
Comments
Post a Comment