PDA

View Full Version : I need a php guru to take a look at this code


Dimava
Wed 5th Jun '02, 3:55pm
<?php
// Important! You have to include it before your html code
include_once "/home/virtual/site3/fst/var/www/html/poll/poll_cookie.php";
?>

<html>
<head>
<title>Welcome to NXSupport</title>
</head>
<?php
include("templates/header.htm");


require("admins/config.php");
$link = mysql_pconnect( "localhost", $user, $pass );
mysql_select_db( $db, $link );


include("templates/index/index_start.php");
$result = mysql_query("SELECT * FROM news ORDER BY date DESC, id DESC LIMIT 5");
while(list($id,$title,$date,$username,$description ,$source,$image) = mysql_fetch_row($result)){
$newdate2 = chunk_split($date, 2, '.');
$newdate = substr($newdate2, 0, strlen($newdate2)-1);
$description = substr($description, 0, 500);

include("templates/index/news_template.php");
}

include("templates/index/index_end.php");

mysql_close($link);
include("templates/footer.htm");
?>
</body>
</html>



Time to time I get an error on this line: while(list($id,$title,$date,$username,$description ,$source,$image) = mysql_fetch_row($result)){ and it says that its some invalid query or something, but after about 30 seconds it goes away. The server is using php 4.1.2

The funny thing is that it only happens once in a while, next time I get the error i'll post the exact message that it gives me. It'll load the whole page, but it'll show the error instead of what ever's in the loop

thanks

Dimava

Chen
Thu 6th Jun '02, 2:55am
while ($row = mysql_fetch_array($result) AND list($id,$title,$date,$username,$description,$sour ce,$image) = $row) {
Try that.

scoutt
Thu 6th Jun '02, 7:57pm
why are you using mysql_fetch_row instead of mysql_fetch_array. I would think it was because of that.

megahard
Thu 6th Jun '02, 10:03pm
"the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use (mysql_close() will not close links established by mysql_pconnect())."

thats from the manual, so u may aswell scrap that close

Dimava
Thu 6th Jun '02, 11:15pm
ok thanks firefly, i'll use that

scoutt thats pretty much what firefly changed

megahard, so how do I close it?

scoutt
Thu 6th Jun '02, 11:24pm
I see what firefly changed but you could have used yours as well but change the mysql_fetch_row.

I wouldn't worry about closing it.

megahard
Thu 6th Jun '02, 11:30pm
Originally posted by Dimava
ok thanks firefly, i'll use that

scoutt thats pretty much what firefly changed

megahard, so how do I close it?

use mysql_connect() instead of mysql_pconnect(), unless u'd rather use permanent connections, its up to u

Chen
Fri 7th Jun '02, 5:09am
Originally posted by scoutt
I see what firefly changed but you could have used yours as well but change the mysql_fetch_row.

I wouldn't worry about closing it.
He used list() which expects an array, and when the array isn't there it errors. Which is why you need to use my code, to stop that error from happening.

mysql_fetch_row() could work just like mysql_fetch_array() in this case, as he's not using the associative array.

scoutt
Fri 7th Jun '02, 10:01am
ahh thanks firefly, I thought mysql_fetch_row didn't return an array, but just the row itself. but I just read it is an array as well. thanks

Dimava
Fri 7th Jun '02, 11:00pm
Originally posted by FireFly
while ($row = mysql_fetch_array($result) AND list($id,$title,$date,$username,$description,$sour ce,$image) = $row) {
Try that.

ok cool, thats the code for the while statement, how would i put this statment in your format:


list($description,$username,$title,$olddate,$sourc e,$image) = mysql_fetch_row($result);


thanks

dimava

scoutt
Sat 8th Jun '02, 1:13am
same way if you want to do it that way.


while ($row = mysql_fetch_array($result) AND list($description,$username,$title,$olddate,$sourc e,$image) = $row) {