PHP query returned value problem...

Hi again Experts.... Julia here...

I have the following query on a page, which returns a chat room name.  According to that name, I assign a value to another variable to say if that chat room is either public or private.  Now, this works for certain rooms and not for others and it 's very unstable.  Sometimes it returns the right thing, sometimes it doesn't, so I'm baffled.  I tried everything, single = , double ==, if/else statements, if/elseif/else statements... Nothing works, so I come to you...

here is the code on the page.  Keep in mind that I'm just a little amateure programmer, so this might be all very confusing cause it's not necessarilly as neet as it should be ;)

//------------------------------------------------

$IP;


$Sql = "Select avail, Login, Member from PM where IpAddress = '$IP'";
$res = mysql_query($Sql);
$row = mysql_fetch_assoc($res);
$result= $row['avail'];
$result2= $row['Login'];
$result3= $row['Member'];

$Sql4 = "Select picture from pictures where member = '$result3'";
$res4 = mysql_query($Sql4);
$row4 = mysql_fetch_assoc($res4);
$result4= $row4['picture'];

$Sql5 = "Select Broadcasting from PM where IpAddress = '$IP'";
$res5 = mysql_query($Sql5);
$row5 = mysql_fetch_assoc($res5);
$result5= $row5['Broadcasting'];

$Sql7 = "Select info2, info4 from profile where ID = '$result3'";
$res7 = mysql_query($Sql7);
$row7 = mysql_fetch_assoc($res7);
$result7= $row7['info2'];
$result8= $row7['info4'];

$Sql8 = "Select room from chat_users where name = '$result2'";
$res8 = mysql_query($Sql8);
$row8 = mysql_fetch_assoc($res8);
$room = $row8['room'];


echo $result;
echo ',';
echo $result2;
echo ',';
echo $result3;
echo ',';
if ($result4) {
echo 'http://www.mysite.com/pictures/'; echo $result4;
echo ',';

} else {

echo 'http://www.mysite.com/pictures/nopic.gif';
echo ',';
}

if ($result5 == 1) {
echo 'Broadcasting,';
}
else {
echo 'Not Broadcasting,';
}

if ($room == 'The Lounge' || $room == 'The Penthouse' || $room == ' The Bar' || $room == 'Friends' || $room == 'Couples' || $room == 'Swingers' || $room == 'Married' || $room == 'Single Parents') {
echo 'Public';
}
else {
echo 'Private';
}

echo ',';
echo $result7;
echo ',';
echo $result8;

?>



----------------------------------------------------------------------

So, what is going on here?  Why is it that when I'm in my room "The Bar" it still says i'm in a private room when, as far as I can see, it should say PUBLIC, no?  I also tried it like this...

if ($room = 'The Lounge' || $room = 'The Penthouse' || $room = ' The Bar' || $room = 'Friends' || $room = 'Couples' || $room = 'Swingers' || $room = 'Married' || $room = 'Single Parents') {
echo 'Public';
}
else {
echo 'Private';
}

in which case it says that i'm in a public room even when I'm in a private one... ARGHHHH... Help!
John AccountAsked:
Who is Participating?
 
ASCII_ManConnect With a Mentor Commented:
Hi Julia,

You missed the target by a bit in your second attempt - A single equal is an assignment operand, not comparison.
****************
$variable = 'value'; // GOOD
if($variable == 'value){<do something>} // GOOD
****************
$variable == 'value'; // BAD
if($variable = 'value){<do something} //BAD
****************

I don't see where this $room variable is been assigned anywhere in the code, so I assume it's coming from higher up or from another script.
Perhaps the value of $room isn't clean (white space before or after the value you want to play with).
Try cleaning up the variable before comparing it by changing this:
****************
if ($room == 'The Lounge' || $room == 'The Penthouse' || $room == ' The Bar' || $room == 'Friends' || $room == 'Couples' || $room == 'Swingers' || $room == 'Married' || $room == 'Single Parents') {
echo 'Public';
}
****************

To this:
****************
$room = trim($room);
if ($room == 'The Lounge' || $room == 'The Penthouse' || $room == ' The Bar' || $room == 'Friends' || $room == 'Couples' || $room == 'Swingers' || $room == 'Married' || $room == 'Single Parents') {
echo 'Public';
}
****************

If you still aren't getting back the right info, try echo'ing out $room.

If it /is/ working after a trim, I'd recommend you alter the structure of the code for easier administration - change this:
****************
$room = trim($room);
if ($room == 'The Lounge' || $room == 'The Penthouse' || $room == ' The Bar' || $room == 'Friends' || $room == 'Couples' || $room == 'Swingers' || $room == 'Married' || $room == 'Single Parents') {
echo 'Public';
}
****************

To this:
****************
$public_roms = array(
 'The Lounge',
 'The Penthouse',
 'The Bar',
 'Friends',
 'Couples',
 'Swingers',
 'Married',
 'Single Parents'
);
$room = trim($room);
if ($public_rooms, $room) {
echo 'Public';
}
****************

Perhaps you could, later on, setup a table in mysql with all the rooms and their types, e.g:
****************
$sql = '
SELECT
 room_type,
 room_open
FROM
 rooms
WHERE
 room_name = '.$room.'
';
$result = mysql_query($Sql5);
$room_details = mysql_fetch_assoc($res5);

if($room_details['room_open'] == true){
 if($room_details['room_type'] == 'public'){
  <do stuff>
 }
 else{
  <authenticate and do stuff>
 }
}
else{
 <room closed>
}
****************
0
 
ASCII_ManCommented:
>I don't see where this $room variable is been assigned anywhere in the code

Oh, I see it now :)
None the less, perhaps the value in the database isn't clean.
0
 
DoppyNLCommented:
simple suggestion:

change the variables
$result1
$result2
$result3
$result4
$result5

to variablenames with a meaning, that way your code will be more easy to read
Get the idea?
the name $result2 doesn't say a thing to me, but if it would be $username I would expect a username to be in there, wich helps in understanding the code.
0
 
John AccountAuthor Commented:
Thank YOu SOOOO much ASCII man...

THAT --->

 $room = trim($room);
if ($room == 'The Lounge' || $room == 'The Penthouse' || $room == ' The Bar' || $room == 'Friends' || $room == 'Couples' || $room == 'Swingers' || $room == 'Married' || $room == 'Single Parents') {
echo 'Public';
}

Did the trick just fine.  Eveything now is peachy!  Thanks again for your help, you saved me once again!

Julia
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.