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 03-12-2010, 09:02 AM   #1 (permalink)
The Acquainted
 
Join Date: Feb 2008
Posts: 107
Thanks: 3
CΛSTΞX is on a distinguished road
Plugin/Addon Mysql_fetch_array echo only 1 result

Hello, my function echo only 1 result. I want to echo more results with using this function. Can you please help me, thanks!

PHP Code:
function data_dump($query) {

$nt=mysql_query($query);
echo 
mysql_error();

$results = array();

while(
$row=@mysql_fetch_array($nt))
    
$results[] = $row;

foreach(
$results as $key=>$val) {

$downloadiclink "<h3><a href='http://www.downloadic.com/$val[id]-$val[alt_name].html'>$val[title]</a></strong></h3>$val[short_story]<br>";

}
return 
$downloadiclink;

}

echo 
data_dump($query);


__________________
Downloadic
infolizer

Last edited by CΛSTΞX : 03-12-2010 at 10:49 AM.
Send a message via MSN to CΛSTΞX
CΛSTΞX is offline  
Reply With Quote
Old 03-12-2010, 10:42 AM   #2 (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

We need more info.
What is the sql you are using? What is the table structure? How many records have you got in the table?

Is the fact its only echoing 1 result due to the fact that there is only 1 row in the table being queried? Or has the sql got a LIMIT statement in it?

On another note, @ is evil. Every time you use it a small cute fluffy <insert appropriate animal with adequate cute fluffiness here> dies!

For the love god please use braces on control structures, namely the while loop. Same as above applies here.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 03-12-2010, 10:47 AM   #3 (permalink)
The Acquainted
 
Join Date: Feb 2008
Posts: 107
Thanks: 3
CΛSTΞX is on a distinguished road
Default

Hello, I tried this but it doesnt give any error. By the way if I don't put @, it gives error. And I cant understand why animal die when I use that :)

If I use this it echos more and more

PHP Code:
function data_dump($query) {

$nt=mysql_query($query);
echo 
mysql_error();

$results = array();

while(
$row=@mysql_fetch_array($nt))
    
$results[] = $row;

foreach(
$results as $key=>$val) {

$downloadiclink "<h3><a href='http://www.downloadic.com/$val[id]-$val[alt_name].html'>$val[title]</a></strong></h3>$val[short_story]<br>";
echo 
$downloadiclink
}
return 
$downloadiclink;

}

data_dump($query); 
But I dont want to echo it in function. I must echo the function and get the results because of my script.
__________________
Downloadic
infolizer
Send a message via MSN to CΛSTΞX
CΛSTΞX is offline  
Reply With Quote
Old 03-12-2010, 11:38 AM   #4 (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

What error does it give? I still could do with knowing more about the under lying structure and sql that you are using. this kind of debugging is akin to fumbling around in the dark.

The @ (or error suppression symbol) is evil because it makes debugging your code an absolute nightmare. A better solution is to test the code that may create an error before hand, instead of just hiding the error. Also it may hide errors that are useful to the debug process, its all very well and good when its on live but when you are developing it you need errors to be displayed in order for you to be able to fix them, after all you can't fix what you don't know about. In short there are a number of ways to do it without the '@' evil.

Take this example:

PHP Code:
$pDbConnection mysql_connect('localhost''root''');
mysql_select_db('test');

$sSql 'SELECT * FROM `categorides` c WHERE `c`.`cat_id`BETWEEN 1 and 3';
$pResult mysql_query($sSql$pDbConnection);

while(
$row = @mysql_fetch_array($pResult))
{
    echo 
var_dump($row);

The sql is incorrect because there isnt a table 'categorides'

This will just hide the fact the query is incorrect and fail silently.

However if we take the @ off we get:
Code:
Warning:  mysql_fetch_array(): supplied argument is not a valid  MySQL result resource in
Here is the correct way to do it (or at least on of the ways):
PHP Code:
$pDbConnection mysql_connect('localhost''root''');
mysql_select_db('test');

$sSql 'SELECT * FROM `categorides` c WHERE `c`.`cat_id` BETWEEN 1 and 3';
$pResult mysql_query($sSql$pDbConnection);

if(!
$pResult)
{
    die(
'Error in query: ' mysql_error($pDbConnection));
}

while(
$row mysql_fetch_array($pResult))
{
    echo 
var_dump($row);

Now here we are anticipating that we may get an error from the query and we are catching it appropriately, now we can handle it as we need.

Error produced:
Code:
Error in query: Table 'test.categorides' doesn't exist
See clear and it tells you exactly where the problem is. Obviously you may want to control this with some logic that denotes whether the errors get printed depending on the environment (i.e. live/development or staging) Alternatively you could use trigger_error and then when on live use error_reporting to hide the errors that may occur in the live environment (which shouldn't really happen but its a good fail safe, as its a bit insecure to let your application fart errors out on a live site)
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 03-12-2010, 11:45 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

I have actually noticed what may be the problem (not had my coffee yet, gimme a break), you arnt appending to $downloadiclink but re assigning the value every iteration, thus only one being returned.

PHP Code:
$downloadiclink "<h3><a href='http://www.downloadic.com/$val[id]-$val[alt_name].html'>$val[title]</a></strong></h3>$val[short_story]<br>"
Should be
PHP Code:
$downloadiclink .= "<h3><a href='http://www.downloadic.com/$val[id]-$val[alt_name].html'>$val[title]</a></strong></h3>$val[short_story]<br>"
My comments above still stand however (in terms of good programming standards)
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
CΛSTΞX (03-12-2010)
Old 03-12-2010, 12:40 PM   #6 (permalink)
The Acquainted
 
Join Date: Feb 2008
Posts: 107
Thanks: 3
CΛSTΞX is on a distinguished road
Default

Thanks a lot, I can't believe that only a dot fixes all my code, lol :D
__________________
Downloadic
infolizer
Send a message via MSN to CΛSTΞX
CΛSTΞX 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
Advance Pagination Class Rendair Advanced PHP Programming 13 02-01-2013 11:08 AM
World of Warcraft Armory xml Grabber with cURL mortisimus Show Off 144 06-25-2012 12:34 PM
MySql Add to favorites - almost functional - help micma909 MySQL & Databases 1 12-19-2009 08:18 PM
print() vs echo() allworknoplay Absolute Beginners 9 05-27-2009 01:46 PM
My dynamic menu lesP Advanced PHP Programming 7 01-21-2008 10:28 PM


All times are GMT. The time now is 09:06 PM.

 
     

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