TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Mysql_fetch_array echo only 1 result (http://www.talkphp.com/general/5346-mysql_fetch_array-echo-only-1-result.html)

CΛSTΞX 03-12-2010 09:02 AM

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




sketchMedia 03-12-2010 10:42 AM

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.

CΛSTΞX 03-12-2010 10:47 AM

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.

sketchMedia 03-12-2010 11:38 AM

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)

sketchMedia 03-12-2010 11:45 AM

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)

CΛSTΞX 03-12-2010 12:40 PM

Thanks a lot, I can't believe that only a dot fixes all my code, lol :D


All times are GMT. The time now is 01:04 PM.

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