3

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 of 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.

5
  • 1
    first line in the loop do a var_dump($row), paste the output here. Commented Feb 21, 2011 at 2:25
  • array(5) { ["Title"]=> string(11) "For Gondor!" ["Set"]=> string(4) "Core" ["Chapter Pack"]=> string(6) " " ["Number"]=> string(2) "22" ["Type"]=> string(5) "Event" } array(5) { ["Title"]=> string(13) "Bilbo Baggins" ["Set"]=> string(19) "Shadows of Mirkwood" ["Chapter Pack"]=> string(0) "" ["Number"]=> string(1) "1" ["Type"]=> string(4) "Hero" } array(5) { ["Title"]=> string(17) "Ungoliant's Spwan" ["Set"]=> string(4) "Core" ["Chapter Pack"]=> string(6) " " ["Number"]=> string(2) "76" ["Type"]=> string(5) "Enemy" } Commented Feb 21, 2011 at 2:34
  • array(5) { ["Title"]=> string(12) "• Gimli" ["Set"]=> string(4) "Core" ["Chapter Pack"]=> string(6) " " ["Number"]=> string(2) "11" ["Type"]=> string(4) "Hero" } Who cares. Commented Feb 21, 2011 at 2:35
  • @Mason240 Code doesn't work very well in comments. You should edit your question to add the new information Commented Feb 21, 2011 at 3:13
  • @Mason240 You'd get the same results using mysql_fetch_assoc() as you would using mysql_fetch_array() Commented Feb 21, 2011 at 3:46

2 Answers 2

3

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:

https://www.php.net/mysql_fetch_array

https://www.php.net/mysql_fetch_assoc

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

Sign up to request clarification or add additional context in comments.

4 Comments

mysql_fetch_array defaults to MYSQL_BOTH which includes both numeric and associative indexes
@Phil Brown : Absolutely right, didn't think of that, always make use of mysql_fetch_assoc. So yeah, i'll approve your question: "Define 'doesn't work'".
I tried mysql_fetch_assoc,but got the same result. I posted the test function, along with the result in the question text above.
@Mason240 Not anything against Mikushi but if this doesn't answer your question, you should not have accepted the answer
2

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

Shouldn't it be

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

3 Comments

No, because need execute a different block of code for based on the Type of card - this is just a test. Here is a plain HTML mock up of what I am trying to create.
@Mason240 Sorry, not following you there. The only time "Hero" shows up in your data dump is for record "Type" fields. None of your "Title" fields contain "Hero"
Yes, I see what you mean, I fixed it and it does work now. I triple checking everything to make sure I wasn't wasting your time with simple errors, but I have been staring at this all day. I think next time I will take a looong break first. Thank you all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.