...

View Full Version : Simple "IF" Script wont work.



IamHe
03-10-2009, 03:29 PM
ok , so i wanted my script to check the rank collum from the users table.
after finding the users rank

i wanted it to check if it was greater than 3

rank 4 & 5 is mod&admin , and so if it was i was going to include the admin
control pannel.

however, the script dosent seem to work.




<?
$result = mysql_query("SELECT * FROM users LIMIT 1 ;");
while($row = mysql_fetch_array($result))
{
echo $row['pinboard'];
}

if ($row['rank'] >3);
echo "THIS IS WHERE I WILL INCLUDE THE ADMIN PANNEL";

?>



the first part works fine (echo pinboard)
that finds the users individual message within the pinboard row and displays it.

but the rank part just seems to display the message regardless.
any ideas?
thanks in advance.

djm0219
03-10-2009, 03:35 PM
Your if statement isn't properly formatted. Try



if ($row['rank'] >3) {
echo "THIS IS WHERE I WILL INCLUDE THE ADMIN PANNEL";
}

_Aerospace_Eng_
03-10-2009, 03:50 PM
You need something here to determine what user you are trying to check or wise it will always return only the first user.

$result = mysql_query("SELECT * FROM users WHERE userid = $id") or die(mysql_error());
Where $id is a variable from either $_POST or $_GET. userid is your autoincrement column in your table for you database.

IamHe
03-10-2009, 04:34 PM
Thanks allot for the quick replys, my first time using this forum , was really suprised at the speed.

i corrected the formatting as noted by dave in the first reply, and then i took aerospace's advice and added some more information to determain the user.

now im getting.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

my script is in 2 parts, seperated by html.
heres what it looks like.



<?
session_start();
include ('dbc.php');
include ('functions.php');
$result = mysql_query("SELECT * FROM users WHERE id = $id") or die(mysql_error());
while($row = mysql_fetch_array($result))

?>


<?
{
echo $row['pinboard'];
}

if ($row['rank'] >3) {
echo "THIS IS WHERE I WILL INCLUDE THE ADMIN PANNEL";
}

?>


any ideas?
thanks

djm0219
03-10-2009, 04:36 PM
Where is $id being set?

IamHe
03-10-2009, 04:50 PM
i changed it to defineing by the users email. which is what they use to login.




$user_email = $_GET['user_email'];
$result = mysql_query("SELECT * FROM users WHERE user_email = $user_email") or die(mysql_error());




and that posts from the login page. it posts fine. i cant figure it out , im still getting the error.

IamHe
03-10-2009, 05:28 PM
Ive just ran a little test.
logging into 3 diffrent acounts.






<?
$result = mysql_query("SELECT * FROM users LIMIT 1 ;");
while($row = mysql_fetch_array($result))
{
echo $row['pinboard'];
echo $row['rank'];
echo $row['pinboard'];
echo $row['full_name'];
echo $row['user_email'];
}


?>







it finds the users data perfectly , and displays , it , so i really cant understand whats going on. any ideas?

PappaJohn
03-10-2009, 05:40 PM
<?php
session_start();
include ('dbc.php');
include ('functions.php');

$user_email = $_GET['user_email'];
$sql = "SELECT * FROM users WHERE user_email ='" . $user_email ."'";
$result = mysql_query($sql) or die(mysql_error());

if ($result)
{
$row = mysql_fetch_assoc($result);

echo $row['pinboard'];

if ($row['rank'] >3)
{
echo "THIS IS WHERE I WILL INCLUDE THE ADMIN PANNEL";
}
}

?>

I strongly suggest you look into validating the user's input (ie: user_email), never trust ANY input from the user.

You should also handle the situation where $result fails.

ohgod
03-10-2009, 05:46 PM
<?
session_start();
include ('dbc.php');
include ('functions.php');
$query = 'SELECT * FROM `users` WHERE `user_email` = "'.$user_email.'"';
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))

?>


<?
{
echo $row['pinboard'];
}

if ($row['rank'] >3) {
echo "THIS IS WHERE I WILL INCLUDE THE ADMIN PANNEL";
}else{
echo $row['rank'];
}

?>

i changed the format on your query a little, see if that helps.

i moved the query out of the actual mysql_query call. won't make much difference to you with this specifically, but it will make it easier when you get to more complex functions because you can change the query based on other variables blah blah

anyway, i added an else statement. that way if your if statement isn't doing what you think it should, you might at least be able to figure out why by the output.

also, you might consider using $_SESSION to store things like email instead of trying to pass it with a $_GET string.

IamHe
03-10-2009, 06:55 PM
SOLVED :):):):)

Thanks allot to every one whose helped me out today!
i really appreciate it.

after the last post by oh god. i had a play round, and this is what it became.





<?
$query = 'SELECT * FROM `users` WHERE `user_email` = "'.$_SESSION['user'].'"';
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))

if ($row['rank'] >3) {
echo "THIS IS WHERE I WILL INCLUDE THE ADMIN PANNEL";
}else{
echo "im an idiot";
}

?>



thanks allot for your adivce.
and session user works much better :)

thanks again!

ohgod
03-10-2009, 07:13 PM
change

echo "im an idiot";
to

echo "i\'m an idiot";

:D

IamHe
03-11-2009, 05:26 PM
change

echo "im an idiot";
to

echo "i\'m an idiot";

:D

See :P still an idiot.

thanks allot for all your help :):):):D:thumbsup:

IamHe
03-11-2009, 05:29 PM
i have another problem ,




<?php
session_start();
include"functions.php";
include"dbc.php";
$lr = mysql_query("SELECT * FROM uonline ORDER BY id ASC;");
while($l = mysql_fetch_array($lr))
$mc = 'SELECT * FROM `users` WHERE `username` = "'.$_SESSION['user'].'"';
while($r = mysql_fetch_array($mc))
{
echo $l['session'];
echo " " . $r['defaultpic'];

}


?>




its a users online script
the actual function for users online is in functions.php
that dose all the calculating, adding - taking away ,
counting members online etc. but the idea for this script

is just to diplsay the image and username , of all current members online.
simple enough.... i thought , but im running into

"Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in usersonline.php on line 8""

any ideas?thanks allot in advance

abduraooft
03-11-2009, 05:31 PM
You missed to query your DB

$mc = 'SELECT * FROM `users` WHERE `username` = "'.$_SESSION['user'].'"';
$result=mysql_query($mc ) or die(mysql_error());
while($r = mysql_fetch_array($result)){
.........

Refer the manual http://php.net/mysql_query

IamHe
03-11-2009, 05:51 PM
New error::

Query was empty

but i have no idea what that means.

before i stuck another query in after addding the first query advised above. it displayed the image fine.

but after i added the second query it just shows that message.




<?php
session_start();
include"functions.php";
include"dbc.php";
$lr = mysql_query("SELECT * = 'SELECT * FROM `uonline` LIMIT 3';");
$result2=mysql_query($lr ) or die(mysql_error());

while($l = mysql_fetch_array($lr))


$mc = 'SELECT * FROM `users` WHERE `username` = "'.$l['session'].'"';
$result=mysql_query($mc ) or die(mysql_error());

while($r = mysql_fetch_array($mc)){



{
echo $l['session'];
echo " " . $r['defaultpic'];

}
}

?>

_Aerospace_Eng_
03-12-2009, 04:53 AM
You still aren't doing things correctly. I strongly urge you to read the manual. It takes care of the trivial mistakes that you are making.

<?php
session_start();
include "functions.php";
include "dbc.php";
$lr = "SELECT * FROM `uonline` LIMIT 3";
$result2=mysql_query($lr) or die(mysql_error());

while($l = mysql_fetch_array($result2))
{
$uname = $l['session'];
$mc = "SELECT * FROM `users` WHERE `username` = '$uname'";
$result=mysql_query($mc) or die(mysql_error());

while($r = mysql_fetch_array($mc))
{
echo $l['session'];
echo " " . $r['defaultpic'];

}
}

?>

abduraooft
03-12-2009, 08:06 AM
$lr = mysql_query("SELECT * = 'SELECT * FROM `uonline` LIMIT 3';"); What does that mean?

IamHe
03-12-2009, 04:29 PM
What does that mean?

ah , i must have started typing, stopped and thestarted again and retyped it or something , or gone over something else. missed that:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum