undefined variable errors

Hi I am hoping someone can help.

My hosting company has just moved my website to a different server, and I noticed that it was giving me blan pages, I added in some debug and now I am getting undefined variable errors and I don’t know why or how to fix it.

The Page which has broken currently gets its information from the database from the url.
RewriteRule ^reviews/([^/]*)/$ /bingosite.php?h1=$1 [L]

The code in the broken page that is throwing out undefined variable errors is the following:

<? $query = mysql_query("SELECT * FROM link where linktype='bingo' and h1 ='".StripUrl101(ucwords($h1))."'"); { while($row =mysql_fetch_assoc($query)) extract($row);?> <? if ($linkType=="bingo") { ?> <?php echo "$h1"; ?> <? } } ?>

I have used this code for years so it could be that it is old but as I only know basic PHP I am very stuck and the hosing company isn’t being very helpful.

If anyone can help it would be appreciated.

Thanks

Rachael

Those errors were always there, but most servers don’t have error reporting on.

That first bit doesn’t do anything other than make urls pretty, has nothing to do with the errors.

Basically, php is reading the variables before they’re used, so they’re not defined. You can fix it in a couple of different ways.

1 - on the top of the page some where, pre-define the variables, $h1 = “”, $linktype = “”, etc.
2 - wrap the effected lines with isset(), like if(isset($linktype) == "bingo) {}

} } will not return the undefined error, those are closing brackets.

Do not use short tags, always use <?php ?> or <?= ?>, some servers turn off short tags as a security measure. They might’ve upgraded ur version of php too.

I mine as well say it and get it over with. If you want expand your learning a bit, switch MySQL over to pdo or mysqli. If not, no worries.

What he said, except this. Please ignore his “I don’t care” attitude about staying updated and secure.

mysql_* is deprecated and will be removed from PHP soon. Also it’s about time to upgrade, mysqli/PDO was released 10 years ago, and using either with parameterized queries properly secures you against sql injections :slight_smile:

if he has the proper injection precautions then its not an issue. its not that I dont care, it just not an answer to the question at hand. I just went ahead and got it over with.

Wow, people like to do things their own way. But, Richei is right, not helping.
We can open a new thread discussion if someone should change to PDO or not.
(I am not in my site, not needed for what it does. There, added my 2 cents…)

So, to the problem we are discussing. First, when you move code to a new server, you must remember that this means a new system, perhaps Linux or Windows which might make a difference and of course the PHP system may not be the same version. Therefore, there could be a lot of differences in the system setups.

Next, in the query Rachael showed there is a function. Perhaps that function is inside a file that was not copied to the new server. There are many possible problems that can cause this error. You need to debug your pages a bit.

First, I would check the new server and make sure all of your files exist where they should be.
Next, I would send your page thru the W3-Validator to see if your page has lost some code or if it has some invalid coding that the newer server does not allow. ( it is here: http://validator.w3.org/ ) Just enter your address of the site…
Lastly, you showed this:

<? $query = mysql_query("SELECT * FROM link where linktype='bingo' and h1 ='".StripUrl101(ucwords($h1))."'"); { while($row =mysql_fetch_assoc($query)) extract($row);?>
Now, if it is a variable undefined, this it must be $h1 or the output of StripUrl101(). You should place this line just above the $query= line: [php] die("$h1=" . $h1 . "
ucwords($h1)=" . ucwords($h1) . "
StripUrl101()=" . StripUrl101(ucwords($h1))"); [/php] What this will do is show if the data passed to the query is correct or not. If correct, then you have to display the output from the query with error checking added in. Your query system does not have error-checking in it. It simply queries without checking for errors. So, this query altered just a little would show you an error if it happens inside the query system itself. Something like: [php] $query = mysql_query("SELECT * FROM link where linktype='bingo' and h1 ='".StripUrl101(ucwords($h1))."'") or die(mysql_error()); [/php] This is not the preferred way, but, it will die there and show the error for debugging purposes. Another better way would be to use an if-clause to check if "mysql_error()" is empty or not after the query. If so, then display an error message, if not, continue on to grab the $row of data.

So, debug your page a little and ask us further questions… Good luck…

Sponsor our Newsletter | Privacy Policy | Terms of Service