Solved

PHP query returned value problem...

Posted on 2003-12-09
4
236 Views
Last Modified: 2006-11-17
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!
0
Comment
Question by:John Account
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
ASCII_Man earned 500 total points
ID: 9910677
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
 
LVL 2

Expert Comment

by:ASCII_Man
ID: 9910691
>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
 
LVL 6

Expert Comment

by:DoppyNL
ID: 9910726
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
 

Author Comment

by:John Account
ID: 9913910
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now