Comparison operator (newb question)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iandunn
Forum Newbie
Posts: 16
Joined: Fri Dec 03, 2004 7:44 am
Contact:

Comparison operator (newb question)

Post by iandunn »

Hi, I'm new to PHP (but not programming) and I can't get this comparison to work. $IDLevel is pulled from a MySQL database, and if I echo it it displays as "Administrator" (without the quotes), but for some reason the if($IDLevel == "Administrator") keeps returning false. I've tried using single quotes and no quotes, and also setting up a second variable for the right-hand operand, but nothing works. What am I doing wrong?

Code: Select all

$result = $dblink->query("select level from accounts where id=$loginID");
$row = $result->fetch_row();
$IDlevel = $rowї0];
$result->close();

Code: Select all

<?php if($IDLevel == "Administrator")
    &#123; ?>
	<div class="articles-admin">&#1111;<a href="edit-article.php?id=<?php echo $row&#1111;0] ?>">edit</a>]</div>
	<?php
    &#125;
    ?>

Thanks :)
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post by Archy »

I dont use -> in PHP and MySQL, I type it all out, I find it easier.

Code: Select all

<?PHP
...
...

$row = mysql_fetch_assoc($result);
$IDLevel = $row[0];

     /* I usually use the column name, as if you change the order, or add a new column in,
         then you have to recode everything again. If you do, do this, then remember to add
         single quotes around column name, so it would be: $IDLevel = $row['column'];
     */

if($IDLevel == "Administrator"){
     ?>

     <div class="articles-admin">[<a href="edit-article.php?id=<?php echo"$IDLevel"; ?>">edit</a>]</div>

     <?PHP
}
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Try:

Code: Select all

echo "|$IDLevel|";
You might have some whitespace surrounding $IDLevel - this should show you.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
iandunn
Forum Newbie
Posts: 16
Joined: Fri Dec 03, 2004 7:44 am
Contact:

Post by iandunn »

There isn't any whitespace in the variable. Here's the entire script and output. I can't figure out why the comparison on line 48 isn't returning true.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>CPS310 Project :: View articles</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css" />
<body>

<?php

include 'dblink.inc.php';

$loginID = 1; /* temp until login system in place */
$result = $dblink->query("select level from accounts where id=$loginID");
$row = $result->fetch_array(MYSQLI_ASSOC);
$IDlevel = $row["level"];
echo $IDlevel; // temp
$result->close();
?>

<div class="container">

	<div class="articles-header">
		<div class="articles-header-title">Title</div>
		<div class="articles-header-author">Author</div>
		<div class="articles-header-date">Date</div>
		<?php if($IDLevel == $IDLevel)
		    { ?>
			<div class="articles-header-admin">Admin</div>
			<?php
		    }
		    ?>
	</div> <!-- end articles-header -->

	<div class="articles">
	<?php
	$result = $dblink->query("select id, title, author, postdate from articles");
	while($row = $result->fetch_array(MYSQLI_ASSOC))
	{
	    $temp = $row["author"];
	    $author = $dblink->query("select username from accounts where id=$temp");
	    $authorRow = $author->fetch_array(MYSQLI_ASSOC);
	    $author->close();
	?>

	<div class="articles-title"><a href="view-article.php?id=<?php printf($row["id"]); ?>"><?php printf($row["title"]); ?></a></div>
	<div class="articles-author"><a href="view-account.php?id=<?php printf($row["author"]); ?>"><?php printf($authorRow["username"]); ?></a></div>
	<div class="articles-date"><?php printf($row["postdate"]); ?></div>
	<?php if($IDLevel == "Administrator") //or author?
    { ?>
	<div class="articles-admin">[<a href="edit-article.php?id=<?php printf($row["id"]); ?>">edit</a>]</div>
	<?php
    }
    ?>
	<div class="articles-separator"></div>

	<?php
	}

	$result->close();
?>
	</div> <!-- end articles -->

</div> <!-- end container -->

<?php $dblink->close(); ?>

</body>
</html>
Image
(image is a link to larger version)
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

i noticed this

Code: Select all

<?php if($IDLevel == $IDLevel)

i dont know if thats just temporary for debugiing,
but that will always be true, and as such is pointless. but thats def not your problem here. just wanted to point it out.



what happens when you replace this

Code: Select all

<?php if($IDLevel == "Administrator") //or author?
w/ this

Code: Select all

echo $IDLevel;
<?php if($IDLevel == "Administrator") //or author?
whats the output of the echo?


and to make it match Author too, you can do like this

Code: Select all

<?php if($IDLevel == "Administrator" || $IDLevel == "Author") //or author?

keep in mind those comparisons ARE case sensitive
iandunn
Forum Newbie
Posts: 16
Joined: Fri Dec 03, 2004 7:44 am
Contact:

Post by iandunn »

The $IDLevel == $IDLevel is just for debugging. The output of echo $IDLevel is "Administrator" (without the quotes). The capitalization matches. echo $IDLevel is done earlier in the script (line 16) just to test it, which you can see @ the top of the image.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

litterally cut and paste that echo in the place i said to.
do not retype it, and do not put it somewhere else in the script. put it directly above the if statement.

either the output from the echo will be different, of the if statement will work. theres no other possibility.
iandunn
Forum Newbie
Posts: 16
Joined: Fri Dec 03, 2004 7:44 am
Contact:

Post by iandunn »

Ugh, the problem was just the I declared the variable as $IDlevel and was trying to use it as $IDLevel :oops:

Thanks for your help :)
Post Reply