PHP SQL查询结果 [英] PHP SQL query results

查看:63
本文介绍了PHP SQL查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,尽管我可能会发疯,但我很自信我几天前才这样做。例如,我尝试遍历SQL结果数组。

Alright, I'm pretty confident I did this only a few days ago, although I may be going crazy. I am attempting to loop through an SQL result array for example..

$query = mysql_query("SELECT * FROM `my_table`");
$result = mysql_fetch_assoc($query); 

现在$ result应该返回多行..如果我使用while循环遍历它,它就会执行。不幸的是,我试图通过foreach循环访问此数据,由于某种原因,它将无法工作。它只给我第一行,而print_r($ result)也只给我第一行。

Now $result should return multiple rows.. and it does if I loop through it using a while loop. Unfortunately, Im trying to access this data with a foreach loop, and for some reason it will not work. Its only giving me the first row and print_r($result) only gives me the first row as well.

foreach($result as $name => $value)
    echo "$name = $value\n"; 

任何建议将不胜感激!

Any suggestions would be appreciated!

**编辑:

我喜欢所有聪明的答案。.我知道php的网站手册,我知道mysql_fetch_assoc()返回什么。这是我的解决方案:

I love all of the smart answers.. I know the website for the php manual and I know what mysql_fetch_assoc() returns. Here is my solution:

function returnSQLArray() { 
    $returnArray = array(); 
    $row = 0; 
    $query = mysql_query("some sql"); 
    while($result = mysql_fetch_assoc($query)) { 
        $returnArray[$row] = $result; 
        $row++; 
    } 
    return $returnArray; 
}


推荐答案

弗拉德森讽刺地指出了什么尽管如此,这是非常正确的。我对PHP编程的尝试(很多年值得)一直散布在php.net网站上的大量阅读文章中。我将其称为现有的最佳在线编程文档,远远超过了我20年来使用的任何其他语言。主要是因为社区贡献的惊人才能。

What Vladson is sarcastically pointing out is nonetheless very true. My forays into PHP programming (many years' worth) have been ever-sprinkled with a great many readups on the php.net site. I'd call it the best online programming documentation in existence, far beating any other language I've used in 20 years.. mostly because of the amazing calibre of the community contributions.

此外,我强烈建议将您要讨论的内容抽象到db helper类中。请参考 PHPBB 代码作为示例。 PHPBB代码的面向对象可能不理想,但是它仍然是体系结构的良好参考点。而且,不仅要这样做,因为您可能会切换数据层或更改版本,而且因为引入普通的错误报告,查询日志记录,数据缓存以及许多其他有用的功能而变得微不足道。这样也可以更轻松地处理多个连接。

Also, I'd highly recommend abstracting what you're talking about into a db helper class. Reference perhaps the PHPBB code for an example. PHPBB code may be less OO than is ideal, but it's still a good reference point for architecture. And, don't just do this because you may switch out your data layer or change the version, but because it makes it trivial to introduce common error reporting, query logging, data caching, and many other such useful features. This also makes it easier to juggle more than one connection.

示例可能是这样,您可以更像地公开一个接口:(请注意,这里的ADODB本质是这样,

Example might be so that you can expose an interface more like: (excuse the very ADODB nature here, but it's still a nice way to think of MySQL, too)

include "db.inc.php";    
$SQL = "SELECT * FROM user WHERE id=123";
$oDB = new Database("localhost", "database", "user", "password");
$oRS = $oDB->NewRecordSet($SQL);
while( $data = $oRS->Read() ) {
    // do stuff
}

通过这种方式,页面不必担心访问数据的繁琐工作,而只需考虑如何过滤数据以及如何处理数据。

In this manner, the pages have to worry less about the tedium of accessing the data, and can just think more about how to filter the data and what to do with it.

这篇关于PHP SQL查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆