开发者

Running an If/Else PHP function with SQL

Thank you for reading my question.

I am trying to make a site where information from a database is displayed onto a webpage. The end result will look like this, but for a different game.

Here is a plain HTML page o开发者_JAVA技巧f what I want it to look like.

So far I know that my connection to the database works. When I run:

mysql_select_db("DATABASE", $con);
$result = mysql_query("SELECT * FROM DATABASE");
while($row = mysql_fetch_array($result)) {
    echo $row['Title'] . " " . $row['Type'];
    echo "<br />";
}

It returns the Title and Type.

What I want to do is run an If/Else statement that runs a different that block of code depending on the card type.

while($row = mysql_fetch_array($result)) {
    if ($row['Title'] == 'Hero') { 
        echo "<div>";
    }
} 

I tried this based on the tutorials at w3schools.com but it doesn't work.

Do any of you have any ideas for what I should do?

EDIT:

Here is what I tried running:

while($row = mysql_fetch_assoc($result)) {
    if ($row['Title'] == 'Hero') {
        echo $row['Title'] . " Hero.<br>";
    } else {
        echo $row['Title'] . " Who cares.<br>";
    }
}

Here is the output (Gimli should show up as a Hero):

For Gondor! Who cares.<br>
Bilbo Baggins Who cares.<br>
Ungoliant's Spwan Who cares.<br>
Gimli Who cares.

EDIT 2: Thank you Phil for spotting the error, I now get the result I wanted using Mikushi's method. Thank you all so much.


The fetching of your mysql result seems wrong, should be like this:

while($row = mysql_fetch_assoc($result)) {
  if ($row['Title'] == 'Hero') { 
    echo ""; }
  } 

mysql_fetch_array fetch the result as an indexed array (1=> data, 2=> thing) , which explains why $row['Title'] doesn't work. The difference:

http://ca2.php.net/mysql_fetch_array

http://ca2.php.net/mysql_fetch_assoc

Please, always refer to the documentation, it's very well done and a better source than w3cschools.


Maybe it's one of those all too obvious things but...

Shouldn't it be

if ($row['Type'] == 'Hero') // "Type", not "Title"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜