April 5, 2012

mysql_fetch_assoc inside if then statement

Question by BBuchanan

So, I’m still new with the php coding, still learning, etc. So, if this has been asked before I’m sorry. I’m writing this off the top of my head, so any mistakes, again, sorry.

I want to pull info from the database and if it’s a number then display text instead. So, I thought something like this would work (I have to check 1 – 12 with a specific word):

 if(mysql_fetch_assoc($result) == 1){
 echo "student";
 }else
  "generic error";

or since I have to do 1 – 12

if($row_members['ulevel'] == 1){
echo "Student";
}else
"generic error";

When users register, they are assigned a number as part of the insert, so there shouldn’t be any error.
So I guess it’s a multi-part question. How do I create a statement to check the ulvel and print it as text. 1 being a observer and 12 being the admin.

Answer by Starx

mysql_fetch_assoc() does not return integer value, it returns associative array of strings.

To check the status, you can simply put expression inside the if expression.

$row = mysql_fetch_assoc($result);
if($row['ulevel'] == 1){
 echo "student";
} else
  "generic error";

If you are checking among multiple rows

while($row = mysql_fetch_assoc($result)) {
    if($row['ulevel'] == 1){
     echo "student";
    } else
      "generic error";
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!