Jump to content

strange error with while()


elite_prodigy

Recommended Posts

I have been having the strangest problem. When I try and build a list of things from MySQL, using a while() statement, strange, strange things have been happening. First, my stats:

 

I'm running a WAMP off line server version 2.0

PHP version 5.2.7

MySQL version 5.1.30

phpMyAdmin 3.0.1.1

Windows 2000 Professional Edition

 

My site is fully MySQL driven, mainly because the ToS of my host says that I am the only one who may edit files or access my cPanel. So, I designed my site to avoid a one Webmaster syndrome. So, certain people with the right permissions can add, remove, and edit pages. Thats all good.

 

So, now I have to build a navigation bar for this site. Okay, my code is error free, I've dotted my I's and crossed my T's so to speak. That code uses a while statement such as:

 


<?php

include 'config.php';

mysql_select_db('exembar_site');

$query 		= "SELECT * FROM `blog_basics`";
$result 		= mysql_query($query);

while($list = mysql_fetch_array($result)){

$id				= $list['id'];
$staffId		= $list['staffId'];

$sql			= "SELECT `username` FROM `staff` WHERE `id`='{$staffId}'";
$result			= mysql_query($sql) or die(mysql_error());
$info			= mysql_fetch_array($result);

$username		= $info['username'];

@$bloglist 	.= '<a href="'.$root.'blog/about.php?id='.$staffId.'">'.$username.'</a>';

}

echo $bloglist;

?>

 

So, now you're thinking, „That code looks horrifying!”

 

You're damn right it does, and I'm at a loss as to how to fix it!

 

This line for example:

 

<?php

@$bloglist 	.= '<a href="'.$root.'blog/about.php?id='.$staffId.'">'.$username.'</a>';

?>

 

Why is that @ sign there? Because if I remove it, I get an undefined variable error.

 

And whats weird about the @ sign is if I echo a variable from an included file I get an undefined variable error in index.php even though the variable is defined in the included file. When I place an @ symbol beside the variable in the included file, the error in the index goes away. Strange, right?

 

Now, you might think, why are you echoing $bloglist in the file instead of including the file at the top of the page and echoing $bloglist where you need it in the page. Well, when I do that, PHP happily gives me an undefined variable error, after it has been defined earlier in the script. I seem to be able to do this with some files and not others, even though the code is exactly the same, I've just re-written the query within the file to get something from another table.

 

The above script creates a list of my staff blogs. It works, sort of. It only outputs one blog link. Now, before you ask, the first thing I did was check my SQL query and to make sure I'm getting stuff back from the database. I am. I even added echo statements to echo each variable in the while() statement. What should have happened was for the wile() statement to echo a bunch and bunch of stuff but it only gets one record.

 

This is the first time I’ve ever had this problem and it seems to all revolve around the while() statement.

 

The only thing that would come to mind is the thing that phpMyAdmin is putting out at the bottom of the home page:

 

Your PHP MySQL library version 5.0.51a differs from your MySQL server version 5.1.30. This may cause unpredictable behavior.

 

This is a clean install of WAMP and I’ve made no changes to any *.ini files or anything. Any and all help is appreciated. If the above is causing this, could you point me in the right direction to fix it? And if my code is flawed, could you help me fix that as well?

 

Thanks guys and gals, you’ve been and continue to be an amazing help to me.

Link to comment
https://forums.phpfreaks.com/topic/137044-strange-error-with-while/
Share on other sites

Define $bloglist as an empty string before the while loop, that should let you get rid of the @ before $bloglist within the loop.

 

$result is being redefined by the secondary database query within the while loop.

Use $result2 or some other variable instead

 

Even better, join your two db queries into a single query

But if $bloglist is being defined within the while(), why define it outside? I'm not saying it's not worth a try, I'm just trying to figure this out and understand why this is happening so I can prevent it in the future.

 

 

But if $bloglist is being defined within the while(), why define it outside?

It's not being defined within the while: you're trying to append to it in the while by using $bloglist .= ..., and the first time round the loop it doesn't exist to be appended to, hence the undefined variable error. The @ suppresses the error. PHP is quite forgiving, and will create it for you at that point, but it still reports the error to indicate that it has had to do so.

 

The real error is still your issue with $result

How do I do two queries in one? In all the time I've been working with PHP and MySQL I've never done that.

 

You use a join, in this case a straight join:

SELECT S.username,
       B.* 
  FROM blog_basics B,
       staff S
WHERE S.id=B.staffId

 

If in doubt, test the all your queries in phpmyadmin (substituting in values for variables where appropriate) before using them in your code. It saves hours of heartache and debugging

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.