Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Problem with if & else statements (I think)

Codes here !

Moderators: macek, egami, gesf

Problem with if & else statements (I think)

Postby amp » Wed Apr 02, 2003 10:17 am

Hi
I'm having a few problems with getting a part of a script working.
Here is the relevant code
Code: Select all
<?php
include("config.php");
include("header.php");
$dbh = mysql_connect ($dbhost, $dbuname, $dbpass) or die ( 'I cannot connect to the database because: ' . mysql_error());
mysql_select_db ($dbname) or die (mysql_error());

// Perform query on travel table
$query = "SELECT *  FROM travel";
$result = mysql_query($query) or die("Error:" .mysql_error());

//Authentication, user will not see results unless logged on
    global $user, $sitename;
    cookiedecode($user);
    $username = $cookie[1];
    if ($username == "") {
#        $username = "Anonymous";

    echo "You need to Logon";
    exit;
}
else
{
echo "OK, Your logged on, now checking to see are you a directorate";

//Perform query on names of directorates
$query2 = "SELECT name  FROM managers";
$result2 = mysql_query($query2) or die("Error:" .mysql_error());
while ($row2 = mysql_fetch_row($result2)) {
if (!$username == $row2[0]) {
echo "Your logged on but your not a directorate, bye!";
exit;
} else {
echo "<br>";
echo 'Your a Directorate!';
}}}

echo OpenTable();
// Retrieve values
while ($row = mysql_fetch_row($result)) {
###### Rest of Script goes here #######

Now my problem is with this part (I think)
Code: Select all
$query2 = "SELECT name  FROM managers";
$result2 = mysql_query($query2) or die("Error:" .mysql_error());
while ($row2 = mysql_fetch_row($result2)) {
if (!$username == $row2[0]) {
echo "Your logged on but your not a directorate, bye!";
exit;

If I am not logged on to my PHPnuke site I get the error "You need to Logon" & it exits as expected, this part works fine.
I want it so that if a user is logged on, it checks to see is their name in the managers table & if it's not they get the error "Your logged on but your not a directorate, bye!".
The problem is any user who is logged on to the site is told Your a Directorate! when they are not in my managers table :cry:
Can one of you experts take a look at my code & tell me if I'm doing something obviously wrong :wink:

Also, notice the way I have added a 2 after the variable names for the 2nd query, do I need to do that :?:

Thanks in advance for any replies :D
User avatar
amp
New php-forum User
New php-forum User
 
Posts: 15
Joined: Tue Apr 01, 2003 3:12 am
Location: Ireland

Postby mammal » Thu Apr 03, 2003 3:47 am

Try this:

$query2 = "SELECT name FROM managers WHERE name = '$username'";
$result2 = mysql_query($query2) or die("Error:" .mysql_error());
$result2_rows = mysql_num_rows ($result2);

if ($result2_rows == 0) {
echo "Your logged on but your not a directorate, bye!";
exit;
}

Your main problem is that the ! symbol was placed before the $username variable when it shoud have been $username !== $row2[0] but the script above is better because it searches for a match in the SQL and then ouputs the number of matches as mysql_num_rows. If therefore the number of matches is 0, then the person is not a directorate.

Hope this makes sense

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

Oh and the answer to your 2 question - you dont have to do this as you no longer need to refer to the previous $result variable although its best practice to differentiate you variable names i.e. $result1 can be $CheckUser and $result2 could be $CheckDirecorate - it makes better sense when you come back to your script after a few months...
User avatar
mammal
New php-forum User
New php-forum User
 
Posts: 37
Joined: Wed Apr 02, 2003 1:31 am
Location: Hythe, UK

Postby amp » Fri Apr 04, 2003 2:18 am

mammal, thank you again for solving my problem :mrgreen:
I really appreciate your explanations along with your replies as it's helping me see things more clearly :wink:
And thanks for clarifying my second question, it makes perfect sense.

mammal, if your stuck for a bit of webspace or anything give me a shout :wink:

Cheers
amp
User avatar
amp
New php-forum User
New php-forum User
 
Posts: 15
Joined: Tue Apr 01, 2003 3:12 am
Location: Ireland

No worries

Postby mammal » Fri Apr 04, 2003 2:31 am

Thanks amp, if you get stuck in future feel free to email me.

Mammal
User avatar
mammal
New php-forum User
New php-forum User
 
Posts: 37
Joined: Wed Apr 02, 2003 1:31 am
Location: Hythe, UK

Postby amp » Sat Apr 05, 2003 4:31 am

Thanks amp, if you get stuck in future feel free to email me.


Thanks mammal, I might have to take you up on that sometime :wink: but for now I think youve answered all my questions :D

amp....
User avatar
amp
New php-forum User
New php-forum User
 
Posts: 15
Joined: Tue Apr 01, 2003 3:12 am
Location: Ireland


Return to mySQL & php coding

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron