-3

我有一个同样的问题,从 mysql 读取数据并将其存储在一个数组中。我有这样的桌子

id     engineering     science      language
_____________________________________________

1      chemical        bio           english
2      electrical      chemistry     malay 
3      mechanical      astronomy     spanish 

例如,如何读取工程中的所有数据并将其存储在数组中?我可以在 PHP 中做任何散列,或者关联数组已经包含这个散列函数。请帮忙。

4

1 回答 1

0

使用MySQLi的准系统示例:

// connect to your database
$mysqli = new mysqli("hostname", "username", "password", "database");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

// setup your query
$query = "SELECT * FROM engineering ";
$results = array();
// execute your query
if ($result = $mysqli->query($query)) {
    // the query was successful; iterate through each result fetching an
    // associative array and add it to the `$results` list
    while ($row = $result->fetch_assoc()) {
        $results[] = $row;
    }
    $result->free();
}
$mysqli->close();

// process all results in `$results` array! (simple output)
foreach ($results as $result) {
    echo $result['id'] . ' ' . $result['engineering'] . '<br />';
}
于 2012-10-16T05:28:07.740 回答