直接参数VS. 保存在变量中的值

Should this function:

function get_article_info(){
        $id = $_GET['id'];
        $data = array();
        $q = "SELECT * FROM articles WHERE article_id = $id";
        $qry = mysql_query($q);
        while($row = mysql_fetch_assoc($qry)){
            $data[] = $row;
        }
        return $data;
    }

be the same as this one:

function get_article_info2(){
        $id = $_GET['id'];
        $data = array();
        $q = mysql_fetch_assoc(mysql_query("SELECT * FROM articles WHERE article_id = $id"));

        while($row = $q){
            $data[] = $row;
        }
        return $data;
    }

If I try to use get_article_info2 I get this error:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 35 bytes)

Can any tell me why it's not working ? Thx ;)

    $q = mysql_fetch_assoc(mysql_query("SELECT * FROM articles WHERE article_id = $id"));

    while($row = $q){
        $data[] = $row;
    }

There is no end to this loop, because you assign a value to $q once before, and it never changes. So the same row is appended to $data over and over again, and finally your script runs out of memory.

You have to call the fetch function for every record separately (as in the first code).

First of all, you should always escape or sanitize your SQL parameters:

$sql = sprintf('SELECT * FROM articles WHERE article_id = %d', (int)$_GET['id']);
$res = mysql_query($sql);

Second, you should perform mysql_fetch_assoc() inside the loop; otherwise it will cause an endless loop:

while (($row = mysql_fetch_assoc($req)) !== false) {
    $data[] = $row;
}

That said, you should stop using the old mysql_* functions; use PDO or mysqli instead and harness the power of prepared statements.