在PHP中,以数组形式获取MySQL查询结果的最佳方法是什么?
我是PHP新手。我有一个select语句,它为特定记录返回100个值。我想将这100个值存储在一个数组中。这是将从select语句获取的值存储到数组中的正确命令吗在PHP中,以数组形式获取MySQL查询结果的最佳方法是什么?,php,mysql,arrays,Php,Mysql,Arrays,我是PHP新手。我有一个select语句,它为特定记录返回100个值。我想将这100个值存储在一个数组中。这是将从select语句获取的值存储到数组中的正确命令吗 $result = mysql_query("select * from processed1 where record = ('$id') "); $data = array(); while($row = mysql_fetch_array($result)) { $data[] = $row;
$result = mysql_query("select * from processed1 where record = ('$id') ");
$data = array();
while($row = mysql_fetch_array($result))
{
$data[] = $row; //IS THIS CORRECT?
}
有没有一种方法可以避免为我的表键入100个属性?示例:$row[1]$行[100]我觉得您的代码很好。但是我建议使用mysql\u fetch\u assoc()而不是mysql\u fetch\u array(),这样键就会映射到它们的值。另外,使用mysql\u real\u escape\u string()防止SQL注入
$query = "Select * from processed1 where record = '".mysql_real_escape_string($id)."'";
$result = mysql_query($query);
$data = array();
while($row = mysql_fetch_assoc($result))
{
$data[] = $row;
}
我觉得你的代码很好。但是我建议使用mysql\u fetch\u assoc()而不是mysql\u fetch\u array(),这样键就会映射到它们的值。另外,使用mysql\u real\u escape\u string()防止SQL注入
$query = "Select * from processed1 where record = '".mysql_real_escape_string($id)."'";
$result = mysql_query($query);
$data = array();
while($row = mysql_fetch_assoc($result))
{
$data[] = $row;
}
如果您试图将所有数据库行存储到一个数组中,是的,该代码应该可以做到这一点。不过有几点评论:
mysql\u fetch\u assoc()
可以通过名称引用单行中的列。(例如:foreach($row形式的数据){echo$row['columnname'];}
)$id
。这可以防止SQL注入攻击如果您试图将所有数据库行存储到一个数组中,是的,该代码应该可以做到这一点。不过有几点评论:
mysql\u fetch\u assoc()
可以通过名称引用单行中的列。(例如:foreach($row形式的数据){echo$row['columnname'];}
)$id
。这可以防止SQL注入攻击PDO::FETCH_NUM
FETCH样式将所有行作为数字数组提取到一个数组中。如果您希望通过数字索引引用每一列(注意,从零开始),请切换到。否则,这看起来是正确的
如果您决定切换到PDO,可以使用简便的方法,使用PDO::FETCH_NUM
FETCH样式将所有行作为数字数组提取到一个数组中。如果你打算在2011年学习PHP,那就让我们把它做好 首先,mysql\u query或mysql\u anything代码不推荐使用。不要再使用它了 别担心-我建议的方法适用于mysql数据库,但也适用于任何数据库:
- PDO是PHP社区继续添加的特性,因此我将使用它
- PDO的功能也更加强大,使以后切换数据库更加容易
- MYSQLi(i代表改进的)取代了不推荐的基于mysql的查询,但我肯定会直接使用PDO
- 您还可以轻松创建一个数组 以后只需更改一行即可更改对象的数量李>
<?php
/* Execute a prepared statement by binding PHP variables */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);
$sth->execute();
/* Fetch all of the rows into an array */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
?>
如果你打算在2011年学习PHP,那就让我们把它做好 首先,mysql\u query或mysql\u anything代码不推荐使用。不要再使用它了 别担心-我建议的方法适用于mysql数据库,但也适用于任何数据库:
- PDO是PHP社区继续添加的特性,因此我将使用它
- PDO的功能也更加强大,使以后切换数据库更加容易
- MYSQLi(i代表改进的)取代了不推荐的基于mysql的查询,但我肯定会直接使用PDO
- 您还可以轻松创建一个数组 以后只需更改一行即可更改对象的数量李>
<?php
/* Execute a prepared statement by binding PHP variables */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);
$sth->execute();
/* Fetch all of the rows into an array */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
?>
这是正确的方法:
while($rows[] = mysqli_fetch_assoc($result));
array_pop($rows); // pop the last row off, which is an empty row
这是正确的方法:
while($rows[] = mysqli_fetch_assoc($result));
array_pop($rows); // pop the last row off, which is an empty row