gpt4 book ai didi

php - 关联数组键值对或Mysql别名效率更高

转载 作者:行者123 更新时间:2023-11-29 05:18:01 24 4
gpt4 key购买 nike

关联数组键名或Mysql别名效率更高

我有一个名为 student 的 mysql 表,其中包含 id、name 和 age 列

我需要得到这样的结果

student_id student_name age

我有两个选择:

$sql = "SELECT id as student_id, name AS student_name, age FROM student";
$result = mysqli_query($link, $sql) or die(mysqli_error($link));
if (mysqli_num_rows($result) > 0) {
while ($studentRow = mysqli_fetch_assoc($result)) {
$studentInfo[] = $studentRow;
}
}

$sql = "SELECT id, name, age FROM student";
$result = mysqli_query($link, $sql) or die(mysqli_error($link));
if (mysqli_num_rows($result) > 0) {
$i=0;
while ($studentRow = mysqli_fetch_assoc($result)) {
$studentInfo[$i]['student_id'] = $studentRow['id'];
$studentInfo[$i]['student_name'] = $studentRow['name'];
$studentInfo[$i]['age'] = $studentRow['age'];
$i++;
}
}

Note: Don't put solutions to alter the table column names.

最佳答案

我觉得第一种效率更高,也更合适。因为在第二个选项中,您需要将值从一个数组恢复到另一个数组,这可能需要更多时间。

所以在我看来,第一个选项会更合适。

关于php - 关联数组键值对或Mysql别名效率更高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29958906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com