如果我理解正确的,你只是想从数据库中读取行返回到一个会话,这样你就可以在以后用它来构建HTML元素:
session_start();
$mysqli = new mysqli('host', 'user', 'pass', 'database');
if($stmt = $mysqli->prepare('SELECT * from FIELDS where 1')):
$stmt->execute();
$fields = $stmt->get_result(); //so we can use fetch array and simplify this process
while($row = $stmt->fetch_array(MYSQLI_ASSOC)): //foreach result set, return an associative array
$_SESSION['fields'][] = $row; //push the array into your fields session
endwhile;
$stmt->close(); //close your statement
endif;
$myslqi->close(); //drop the mysqli connection, we're done
print_r($_SESSION['fields']); //lets see what fields looks like now
$ret = '';
foreach($_SESSION['fields'] as $row): //foreach row in sessions
$ret .= '<div>'; //parent container for all fields in this row
foreach($row as $idx => $field): //foreach field in the row## Heading ##
$ret .= '<span>'. $field . '</span>'; //create a div element to contain the field
endforeach;
$ret . = '</div>'; //close the parent container
endforeach;
echo $ret; //returned the constructed HTML for the fields
当我回声$ F IT回声不过,我想它不会读取该行 – user3317235
哪里是“MySQL的”字段名? – TimWolla
if(mysqli_connect_errno()) echo'无法连接到MySQL:'。 mysqli_connect_error(); } $ result = mysqli_query($ con,'SELECT * FROM customers'); while($ row = mysqli_fetch_array($ result)) { while($ row = mysqli_fetch_array($ result)) { – user3317235