PHP conditional statements not working
Moderator: General Moderators
PHP conditional statements not working
Can't figure out why my PHP conditional statements wouldn't be working. This is stumping my server admins, too. It is almost definitely on the server end of things. I've tested it out on two other servers I know to be configured properly and there are no problems. I've also stripped the statements down as simple as possible and still no dice. Everything else, so far, seems to be functional. Any common errors that I might present to my administrators to check out as far as configuration goes? I know very little about server config, so please bear with me. I should probably just get them to do their own dirty work with forums and troubleshooting!! I guess there are just too few of them. There's also the fact that I seem to be the only one suffering here! (Helps get things done.)
Server has..
PHP 4.3.2
Apache 2.0
Linux 2.4.21
MySQL 3.23.58
Server has..
PHP 4.3.2
Apache 2.0
Linux 2.4.21
MySQL 3.23.58
Sorry, guys.
One of the servers I did have success with:
Apache version 1.3.29 (Unix)
PHP version 4.3.4
MySQL version 4.0.18-standard
The other server was my workstation @ work. I can't verify it now, but as I recall it was an earlier version of PHP than the one on the server that is not working. My troubleshooting included some SIMPLE stuff, as mentioned. Statements like what follows weren't even working properly:
<?
if ( $condition1 == "true")
{
echo ' Hello, number 1 ';
}
elseif ( $condition2 == "true")
{
echo ' Hello, number 2 ';
}
else {
echo '
<P><A HREF="condition.php?condition1=true">Condition 1</A></P>
<P><A HREF="condition.php?condition2=true">Condition 2</A></P>
';
}
?>
Here's an address to see it working:
http://trinity.ws/~elinn/temp/condition.php
One of the servers I did have success with:
Apache version 1.3.29 (Unix)
PHP version 4.3.4
MySQL version 4.0.18-standard
The other server was my workstation @ work. I can't verify it now, but as I recall it was an earlier version of PHP than the one on the server that is not working. My troubleshooting included some SIMPLE stuff, as mentioned. Statements like what follows weren't even working properly:
<?
if ( $condition1 == "true")
{
echo ' Hello, number 1 ';
}
elseif ( $condition2 == "true")
{
echo ' Hello, number 2 ';
}
else {
echo '
<P><A HREF="condition.php?condition1=true">Condition 1</A></P>
<P><A HREF="condition.php?condition2=true">Condition 2</A></P>
';
}
?>
Here's an address to see it working:
http://trinity.ws/~elinn/temp/condition.php
Problem: register_globals is off in the server's php.ini configuration file.
Solution 1: turn it on. (Not recommended)
Solution 2: Use $_GET[varname], $_POST[varname], or $_REQUEST[varname] (covers GET and POST) instead of $varname.
Solution 1: turn it on. (Not recommended)
Solution 2: Use $_GET[varname], $_POST[varname], or $_REQUEST[varname] (covers GET and POST) instead of $varname.
Code: Select all
<?
if ( $_REQUEST[condition1] == "true")
{
echo ' Hello, number 1 ';
}
elseif ( $_REQUEST[condition2] == "true")
{
echo ' Hello, number 2 ';
}
else {
echo '
<P><A HREF="condition.php?condition1=true">Condition 1</A></P>
<P><A HREF="condition.php?condition2=true">Condition 2</A></P>
';
}
?>slim is most correct, keep register globals off as the good people who created php made them off by default for secuirty reasons
however - for future reference, I would use $_GET when pulling variables from the url, or use $_POST global for variables sent via forms.
$_REQUEST could be anything. If you have alot of variables coming to a page from possible different destinations it can help you remember what function/part of the code your getting the vars from for future or if your memory is bad like mine, it'll come in handy.
just my .02.
however - for future reference, I would use $_GET when pulling variables from the url, or use $_POST global for variables sent via forms.
$_REQUEST could be anything. If you have alot of variables coming to a page from possible different destinations it can help you remember what function/part of the code your getting the vars from for future or if your memory is bad like mine, it'll come in handy.
just my .02.
It's not a bad or lazy idea if you have scripts with variables accessed through both GET and POST.feyd wrote:yeah.. I don't use $_REQUEST.. it's just a bad (and lazy) idea.. not to mention, it's likely a security risk..
An example would be a script with "next page" links and a search form. Html anchors have to be GET, and the search would use POST, preferably.
If you were to have an option such as "PerPage," it would be in the link and in a hidden field in the form.
By the way, the only way I can see it being a security risk is if you rely on certain variables coming from $_POST to prove that they used your form and not a typed-in URL. This is a security faux-pas in itself, as anyone can make a form and/or spoof the http_referer.
Fabulous
Thanks to all. You've been so helpful. The issue is resolved by using $_GET in the instance that I posted previously.
I'm so frustrated because I taught myself a very narrow amount of PHP using servers that weren't configured with much in terms of security - very forgiving of syntax and methods. So everything that I did worked and I became lazy and spoiled. Time to go back and fill in the blanks! I'm just a multimedia developer - really!
I have got another one, if anyone cares to take a stab.
The error I'm getting - and again, this is only happening on the one server:
I'm so frustrated because I taught myself a very narrow amount of PHP using servers that weren't configured with much in terms of security - very forgiving of syntax and methods. So everything that I did worked and I became lazy and spoiled. Time to go back and fill in the blanks! I'm just a multimedia developer - really!
I have got another one, if anyone cares to take a stab.
Code: Select all
<?php
$query = "SELECT * FROM table1 ORDER BY whatever asc";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
for ($i=0; $i
'.($row['1']).'
'.($row['2']).'
'.($row['3']).'
'.($row['4']).'
'.($row['5']).'
'.($row['6']).'
';
}
?>Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in ....
-
Illusionist
- Forum Regular
- Posts: 903
- Joined: Mon Jan 12, 2004 9:32 pm
something is wrong with your query. Try echoing it out, and also add or die(mysql_error()) to the end of mysql_query()
the way you are doing it is also wrong... try this instead:
the way you are doing it is also wrong... try this instead:
Code: Select all
<?php
$query = "SELECT * FROM table1 ORDER BY whatever asc";
echo $query;
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
//not sure hwat you want to do, so i'm jsut goignt o echo it out
echo $row['1'];
echo $row['2'];
echo $row['3'];
echo $row['4'];
echo $row['5'];
echo $row['6'];
}
?>