TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   need help with retrieving (http://www.talkphp.com/advanced-php-programming/3715-need-help-retrieving.html)

tony 12-04-2008 10:38 PM

need help with retrieving
 
I've used this code before, and has worked. But now I can't understand what is the problem.

I have a code to test my queries for a website I am doing. This is the code:

PHP Code:

$a=array();
try{
    
$q=mysql_query('SELECT ArticleID FROM articles') or die(mysql_error());
    echo 
mysql_affected_rows();
    echo 
'<br />';
    
var_dump($q);
    echo 
'<br />';
    while(
$res=mysql_fetch_assoc($q));
        
array_push($a$res);
    
var_dump($a);
    echo 
'<br />';
}catch(
Exception $e){
  echo 
$e->getMessage();
  exit();


the echo and var_dump statements I used them to see what is the problem, but still can't figure out. The output I get is this:

Code:

8
resource(15) of type (mysql result)
array(1) { [0]=> bool(false) }

I know I have 8 entries in the table, because I also ran the query in the mysql console and got this:

Code:

mysql> SELECT ArticleID FROM articles;
+----------------------------------+
| ArticleID                        |
+----------------------------------+
| 32d9b19297370a3f53f22d5d5715c847 |
| 35d97569c36ade68499ea6f51d88a20a |
| 6981086eb26c319ad7631b7d2f30618d |
| 81967e633319c72e44284884157ad432 |
| 874820c3267d7f0a94bf9a89baf83508 |
| a7b39088eb9a13de840383ff27e24ac4 |
| cf47d07a4f6d371f863a41e8e24bc285 |
| e13d693f6ecd117aeb5ad9d04324e558 |
+----------------------------------+
8 rows in set (0.00 sec)

I still don't know why I don't get an array of arrays that are the fetched rows. any idea?

If you need more info this is the articles table:

Code:

mysql> SHOW COLUMNS FROM articles;
+-------------+--------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| ArticleID  | varchar(32)  | NO  | PRI | NULL    |      |
| Title      | varchar(255) | NO  |    | NULL    |      |
| Description | text        | YES  |    | NULL    |      |
| Content    | text        | NO  |    | NULL    |      |
| MediaID    | varchar(32)  | NO  | UNI | NULL    |      |
| UserID      | varchar(32)  | NO  |    | NULL    |      |
| DateTime    | datetime    | NO  |    | NULL    |      |
+-------------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

This used to be simple to me, but now it is stressing me out and delaying the final product. thanks for the help in advance.

Wildhoney 12-04-2008 11:18 PM

I believe it is the following line that's causing all your troubles:

php Code:
while($res=mysql_fetch_assoc($q));

There shouldn't be a semi-colon at the end of that line, it should be either nothing, or curly braces. I think curly braces should always be present, even if the inner code of the conditional is a mere one line. It saves on headache-inducing problems like this!

zxt3st 12-04-2008 11:39 PM

PHP Code:

$a=array();
try{
    
$q=mysql_query('SELECT ArticleID FROM articles') or die(mysql_error());
    echo 
mysql_affected_rows();
    echo 
'<br />';
    
var_dump($q);
    echo 
'<br />';
    while(
$res=mysql_fetch_assoc($q)) #####Check wildhoney posted!!
        
array_push($a$res);
    
var_dump($a);
    echo 
'<br />';
}catch(
Exception $e){
  echo 
$e->getMessage();
  exit();



tony 12-05-2008 01:39 AM

Wow how could I have missed that!
I guess I needed to relearn to look at the code after a break with a clear mind. :P saves headaches like wildhoney said.
Thanks guys.

sketchMedia 12-05-2008 10:02 AM

Sorry to nit-pick here, but your try..catch block isn't doing much, you need to throw and exception before its caught:
PHP Code:

    if(!$q=mysql_query('SELECT ArticleID FROM articles'))
    {
        throw new 
Exception(mysql_error());
    } 

would do it, also there is no need for the 'exit' as and exception stops the current execution anyway.
I agree with Wildhoney about the curly braces, although not needed for PHP to execute it, it will make debugging harder in the future

tony 12-05-2008 02:16 PM

I thought the point of try and catch is that try automatically throws an error if there is any.
Maybe not, I come from a c++ and java background and I thought it would be the same. I am going to look more about it.
And now I can see the need of braces for debugging, this is my first large project so I am learning more each time.


All times are GMT. The time now is 04:36 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0