TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-04-2008, 10:38 PM   #1 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default 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.
tony is offline  
Reply With Quote
Old 12-04-2008, 11:18 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
tony (12-05-2008)
Old 12-04-2008, 11:39 PM   #3 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default

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();

__________________
Serenity Project - 5% (Layout) - Ongoing....
Project Serenity Free Life!....
zxt3st is offline  
Reply With Quote
The Following User Says Thank You to zxt3st For This Useful Post:
tony (12-05-2008)
Old 12-05-2008, 01:39 AM   #4 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

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.
tony is offline  
Reply With Quote
Old 12-05-2008, 10:02 AM   #5 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 12-05-2008, 02:16 PM   #6 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

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.
tony is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
retrieving data KingOfTheSouth MySQL & Databases 11 12-02-2008 07:10 AM
Retrieving Files from a Backup in Vista CMellor The Lounge 2 06-24-2008 02:34 PM
Sending and Retrieving Data WinSrev Javascript, AJAX, E4X 11 02-18-2008 12:18 AM
Interfaces Revisited Karl Advanced PHP Programming 5 11-15-2007 02:40 PM


All times are GMT. The time now is 06:51 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design