By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,480 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,480 IT Pros & Developers. It's quick & easy.

Warning: mysql_fetch_array(): 5 is not a valid MySQL result resource

P: n/a
Hi, I'm new here and excuse me if this question was be here earlier.

I have a simple code

<html><body>
<?php
<?php
$link = mysql_connect("localhost", "user", "password")
or die("Grr: " . mysql_error());
mysql_select_db("my_dbf") or die("Grr");
$query = "select id from my_table where id < 4 order by id";
$result = mysql_query($query) or die("Grr: " . mysql_error());
$rows = mysql_num_rows($result);
echo "Rows=", $rows, "<br>" ;
list_it($result);

function list_it($result)
{
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo $row["id"], "<br>";
}
}
?>
</body></html>

This code produce this output:

Rows=3
1
2
3
Warning: mysql_fetch_array(): 5 is not a valid MySQL result resource in
F:\webpub\php\test.php on line ...

Why warning?
This is a simple example. In my real code I must use a few different
functions for displaing mysql_fetch_array() result and $query variable I
must create before any html output because I use sessions.
How to pass resource type variable into function as parameter?
Oh, my PHP version is 4.3.7 on Windoze ;-)

--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)


Aug 14 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Hi Petr,
in while loops condition you are fetching the next row each time
when it fetch the 3rd row which is the last row and next time tries to
fetch the row but it don't found any row so it returns the error at
fetching the row you can fecht the rows inside the while loop not in
while condition below could be one option

function list_it($result)
{
$rows = mysql_num_rows($result);
for ($i=0;$i<$rows;$i++)
{
$row = mysql_fetch_array($result, MYSQL_ASSOC)
echo $row["id"], "<br>";
}
}

Thanks
Ninad

Petr Vileta wrote:
Hi, I'm new here and excuse me if this question was be here earlier.

I have a simple code

<html><body>
<?php
<?php
$link = mysql_connect("localhost", "user", "password")
or die("Grr: " . mysql_error());
mysql_select_db("my_dbf") or die("Grr");
$query = "select id from my_table where id < 4 order by id";
$result = mysql_query($query) or die("Grr: " . mysql_error());
$rows = mysql_num_rows($result);
echo "Rows=", $rows, "<br>" ;
list_it($result);

function list_it($result)
{
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo $row["id"], "<br>";
}
}
?>
</body></html>

This code produce this output:

Rows=3
1
2
3
Warning: mysql_fetch_array(): 5 is not a valid MySQL result resource in
F:\webpub\php\test.php on line ...

Why warning?
This is a simple example. In my real code I must use a few different
functions for displaing mysql_fetch_array() result and $query variable I
must create before any html output because I use sessions.
How to pass resource type variable into function as parameter?
Oh, my PHP version is 4.3.7 on Windoze ;-)

--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)
Aug 14 '06 #2

P: n/a
Petr Vileta wrote:
Hi, I'm new here and excuse me if this question was be here earlier.

I have a simple code

<html><body>
<?php
<?php
$link = mysql_connect("localhost", "user", "password")
or die("Grr: " . mysql_error());
mysql_select_db("my_dbf") or die("Grr");
$query = "select id from my_table where id < 4 order by id";
$result = mysql_query($query) or die("Grr: " . mysql_error());
$rows = mysql_num_rows($result);
echo "Rows=", $rows, "<br>" ;
list_it($result);

function list_it($result)
{
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo $row["id"], "<br>";
}
}
?>
</body></html>

This code produce this output:

Rows=3
1
2
3
Warning: mysql_fetch_array(): 5 is not a valid MySQL result resource in
F:\webpub\php\test.php on line ...

Why warning?
This is a simple example. In my real code I must use a few different
functions for displaing mysql_fetch_array() result and $query variable I
must create before any html output because I use sessions.
How to pass resource type variable into function as parameter?
Oh, my PHP version is 4.3.7 on Windoze ;-)
The code you showed should not display this error. Unfortunately, you
left out what's probably the most important piece - the line number of
the failing code.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 14 '06 #3

P: n/a
Hmm, curious ;-)
I'm using while loop in all other cases without error or warning. With one
exception - both
mysql_query() and mysql_fetch_array() is in the same program block. If i
try this

$result = mysql_query($query)
$rows = mysql_num_rows($result);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo $row["id"], "<br>";
}

I don't get warning. But if I put while() into function, I will get. IMHO
this is because $result is "resource" type variable. But how to pass this
type variable to function? Maybe I temporary switch-off warnings in function
:-)

--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)

"Ninad" <ni****@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Hi Petr,
in while loops condition you are fetching the next row each time
when it fetch the 3rd row which is the last row and next time tries to
fetch the row but it don't found any row so it returns the error at
fetching the row you can fecht the rows inside the while loop not in
while condition below could be one option

function list_it($result)
{
$rows = mysql_num_rows($result);
for ($i=0;$i<$rows;$i++)
{
$row = mysql_fetch_array($result, MYSQL_ASSOC)
echo $row["id"], "<br>";
}
}

Thanks
Ninad

Petr Vileta wrote:
>Hi, I'm new here and excuse me if this question was be here earlier.

I have a simple code

<html><body>
<?php
<?php
$link = mysql_connect("localhost", "user", "password")
or die("Grr: " . mysql_error());
mysql_select_db("my_dbf") or die("Grr");
$query = "select id from my_table where id < 4 order by id";
$result = mysql_query($query) or die("Grr: " . mysql_error());
$rows = mysql_num_rows($result);
echo "Rows=", $rows, "<br>" ;
list_it($result);

function list_it($result)
{
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo $row["id"], "<br>";
}
}
?>
</body></html>

This code produce this output:

Rows=3
1
2
3
Warning: mysql_fetch_array(): 5 is not a valid MySQL result resource in
F:\webpub\php\test.php on line ...

Why warning?
This is a simple example. In my real code I must use a few different
functions for displaing mysql_fetch_array() result and $query variable I
must create before any html output because I use sessions.
How to pass resource type variable into function as parameter?
Oh, my PHP version is 4.3.7 on Windoze ;-)

--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)
Aug 14 '06 #4

P: n/a
Rik
Petr Vileta wrote:
Hmm, curious ;-)
I'm using while loop in all other cases without error or warning.
With one exception - both
mysql_query() and mysql_fetch_array() is in the same program block.
If i try this

$result = mysql_query($query)
$rows = mysql_num_rows($result);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo $row["id"], "<br>";
}

I don't get warning. But if I put while() into function, I will get.
IMHO this is because $result is "resource" type variable. But how to
pass this type variable to function? Maybe I temporary switch-off
warnings in function :-)
Nope, a resource can be handles here perfectly. The following code produces
no errors:

<?php
function check($arg){
while($row = mysql_fetch_assoc($arg)){
print_r($row);
}
}
$link = mysql_connect('localhost','root');
mysql_select_db('testbase',$link);
$result = mysql_query('SELECT * FROM tbl_test');
check($result);
?>

Judging by your output:
>
Rows=3
1
2
3
Warning: mysql_fetch_array(): 5 is not a valid MySQL result resource
in F:\webpub\php\test.php on line ...
Do the 1\n2\n3 mean these rows are shown? If yes, did you give us the entire
code? It would seem a _second_ fetch is performed on the result that we're
not aware of. Could you show us the entire relevant code?

Grtz,
--
Rik Wasmus
Aug 14 '06 #5

P: n/a
"Jerry Stuckle" <js*******@attglobal.netpíše v diskusním příspěvku
news:47******************************@comcast.com. ..

1: <html><body>
2: <?php
3: <?php
4: $link = mysql_connect("localhost", "user", "password")
or die("Grr: " . mysql_error());
6: mysql_select_db("my_dbf") or die("Grr");
6: $query = "select id from my_table where id < 4 order by id";
7: $result = mysql_query($query) or die("Grr: " . mysql_error());
8: $rows = mysql_num_rows($result);
9: echo "Rows=", $rows, "<br>" ;
10: list_it($result);
11:
12: function list_it($result)
13: {
14: while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
15: {
16: echo $row["id"], "<br>";
17: }
18: }
19: ?>
20: </body></html>

Warning: mysql_fetch_array(): 5 is not a valid MySQL result resource in
F:\webpub\php\test.php on line 14.
The code you showed should not display this error. Unfortunately, you
left out what's probably the most important piece - the line number of the
failing code.
No error but warning, but output before warning is as I expect ;-)

--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)
Aug 14 '06 #6

P: n/a
"Rik" <lu************@hotmail.compíše v diskusním příspěvku
news:c2***************************@news1.tudelft.n l...
Judging by your output:
>>
Rows=3
1
2
3
Warning: mysql_fetch_array(): 5 is not a valid MySQL result resource
in F:\webpub\php\test.php on line ...

Do the 1\n2\n3 mean these rows are shown?
Yes
It would seem a _second_ fetch is performed on the result that we're
not aware of.
Hmm, I do to study my code :-) This could be the reason.
Could you show us the entire relevant code?
No. The code is not top-secret but is too big and if I delete some parts it
could be not runable.

--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)
Aug 14 '06 #7

P: n/a
Petr Vileta wrote:
"Jerry Stuckle" <js*******@attglobal.netpíše v diskusním příspěvku
news:47******************************@comcast.com. ..

1: <html><body>
2: <?php
3: <?php
4: $link = mysql_connect("localhost", "user", "password")
or die("Grr: " . mysql_error());
6: mysql_select_db("my_dbf") or die("Grr");
6: $query = "select id from my_table where id < 4 order by id";
7: $result = mysql_query($query) or die("Grr: " . mysql_error());
8: $rows = mysql_num_rows($result);
9: echo "Rows=", $rows, "<br>" ;
10: list_it($result);
11:
12: function list_it($result)
13: {
14: while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
15: {
16: echo $row["id"], "<br>";
17: }
18: }
19: ?>
20: </body></html>

Warning: mysql_fetch_array(): 5 is not a valid MySQL result resource in
F:\webpub\php\test.php on line 14.
>The code you showed should not display this error. Unfortunately, you
left out what's probably the most important piece - the line number of
the failing code.


No error but warning, but output before warning is as I expect ;-)
OK, if this is your entire code, then the problem shouldn't occur. I've
done similar many times with ho problems.

Either you're freed the result before calling your function, or you're
calling it from someplace else. But either way you aren't passing a
valid result object.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 15 '06 #8

P: n/a
Hi Petr
what i want to say in my code is don't put mysql_fetch_array()
funcstion in while loop condition put it inside the loop your problem
will be solved

Thanks
Ninad

Petr Vileta wrote:
"Rik" <lu************@hotmail.compíše v diskusním příspěvku
news:c2***************************@news1.tudelft.n l...
Judging by your output:
>
Rows=3
1
2
3
Warning: mysql_fetch_array(): 5 is not a valid MySQL result resource
in F:\webpub\php\test.php on line ...
Do the 1\n2\n3 mean these rows are shown?

Yes
It would seem a _second_ fetch is performed on the result that we're
not aware of.

Hmm, I do to study my code :-) This could be the reason.
Could you show us the entire relevant code?
No. The code is not top-secret but is too big and if I delete some parts it
could be not runable.

--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)
Aug 21 '06 #9

P: n/a
Ninad wrote:
Petr Vileta wrote:
>>"Rik" <lu************@hotmail.compíše v diskusním příspěvku
news:c2***************************@news1.tudelft .nl...

>>>Judging by your output:

Rows=3
1
2
3
Warning: mysql_fetch_array(): 5 is not a valid MySQL result resource
in F:\webpub\php\test.php on line ...
Do the 1\n2\n3 mean these rows are shown?

Yes

>>>It would seem a _second_ fetch is performed on the result that we're
not aware of.

Hmm, I do to study my code :-) This could be the reason.

>>>Could you show us the entire relevant code?

No. The code is not top-secret but is too big and if I delete some parts it
could be not runable.

--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)


Hi Petr
what i want to say in my code is don't put mysql_fetch_array()
funcstion in while loop condition put it inside the loop your problem
will be solved

Thanks
Ninad
(Top posting fixed)

I almost always place mysql_fetch_array() in the while loop condition.
It works very well there.

P.S. Please don't top post.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 21 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.