在 PHP 中以数组形式获取 MySQL 查询结果的最佳方法是什么?
我是 PHP 新手。我有一个 select 语句,它为特定记录返回 100 个值。我想将这 100 个值存储在一个数组中。这是将从 select 语句获取的值存储到数组中的正确命令吗?
$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] ... $row[100]
I'm new to PHP. I have a select statement that returns 100 values for a particular record. I'd like to store these 100 values in an array. Is this the right command to store values that I get from a select statement into an array?
$result = mysql_query("select * from processed1 where record = ('$id') ");
$data = array();
while($row = mysql_fetch_array($result))
{
$data[] = $row; //IS THIS CORRECT?
}
Is there a way where I can avoid typing in the 100 attributes for my table? example : $row[1] ... $row[100]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果你打算在 2011 年学习 PHP,那就好好学吧。
首先,不推荐使用 mysql_query 或 mysql_ Anything 代码。不要再使用它了。
别担心 - 我建议的内容非常适用于 mysql 数据库,但它也适用于任何数据库:
稍后对象的一行更改!
其次,Phil 提到了 fetchAll()。这是最终目标。其他方式只需一次一行地移动即可。这使用推土机而不是铲子。注意:这不是选择大量数据的最佳方法,因为它会耗尽内存。否则的话,也没关系。
为此,请使用准备好的过程来保护您的代码免受 SQL 注入攻击。
If you are going to learn PHP in 2011, let's do it right.
First off, mysql_query or mysql_ anything code is deprecated. Don't use it anymore.
Don't worry - what I am suggesting works great with mysql databases, but it will also work great with any database:
of objects later with one line change!
Secondly, Phil mentioned fetchAll(). This is the end goal. The other ways simply move thru it one row at a time. This uses a bulldozer instead of a shovel. Note: not the best way of selecting really large amounts of data, as it will use up memory. Otherwise, it is fine.
To get there, use prepared procedures to protect your code from SQL injection attacks.
你的代码对我来说看起来不错。但我建议使用 mysql_fetch_assoc() 而不是 mysql_fetch_array(),以便将键映射到它们的值。另外,使用 mysql_real_escape_string() 来防止 SQL 注入。
Your code looks fine to me. But I would suggest to use mysql_fetch_assoc() instead of mysql_fetch_array(), so that keys are mapped to their values. Also, use mysql_real_escape_string() to prevent SQL injection.
如果您尝试将所有数据库行存储到一个数组中,是的,该代码应该可以做到这一点。不过,有一些评论:
$id
如果您必须继续使用 mysql 扩展。这可以防止 SQL 注入攻击。If you're trying to store all the database rows into an array, yes, that code should do it. A few comments, though:
mysql_fetch_assoc()
to be able to refer to columns in an individual row by their names. (ex:foreach ($data as $row) { echo $row['columnname']; }
)$id
throughmysql_real_escape_string()
if you have to continue using the mysql extension. This prevents SQL injection attacks.切换到
mysql_fetch_row()
如果您想通过数字索引(注意,从零开始)引用每列。否则,这看起来是正确的。如果您决定切换到 PDO,您可以使用方便的
PDOStatement::fetchAll( )
方法,使用PDO::FETCH_NUM
获取样式将所有行作为数值数组获取到数组中。Switch to
mysql_fetch_row()
if you want to reference each column by a numeric index (note, zero-based). Otherwise, that looks correct.If you decide to switch to PDO, you can use the handy
PDOStatement::fetchAll()
method, using thePDO::FETCH_NUM
fetch style to fetch all rows as numeric arrays into an array.这是正确的方法:
This is the correct way: