Jump to content

[SOLVED] value and field help in mySQL


berry05

Recommended Posts

 
<?php 

include("dbconfig.php"); //replace with your db connection file 

$query = mysql_query("SELECT * FROM tbl_name");   //replace tbl_name with your tbl 

while($getinfo = mysql_fetch_array($query)){ 

   if($getinfo['rowhere'] == 0){ 

       echo "row here equals 0" ; 
    } 

   if($getinfo['rowhere'] == 1){ 

     echo "row here equals 1"; 

} 
} 

?> 

 

I think this is what you want, my php isn't "great" but im learning and like to try and help people out. If there is any errors i apologize =)

 

my version....

<?php


<?php 

include("dbconfig.php"); //replace with your db connection file 

$query = mysql_query("SELECT * FROM tbl_name");   //replace tbl_name with your tbl 

while($getinfo = mysql_fetch_assoc($query)){ 

switch($getinfo['rowhere']) {

   case '0':

  $x="Some Text";
  
  break;

  case '1': 

    $x='Do something else!';
    
    break;
}
  }
  
  echo $x;
?>

Why is everyone connecting to a database?  He only wanted an if elseif statement...  Use premiso's, or rather use my updated version of premiso's.

 

The sould purpose of everyone doing that is because is said the value of a field, not a var. Also for the post count

Why is everyone connecting to a database?  He only wanted an if elseif statement...  Use premiso's, or rather use my updated version of premiso's.

 

MySQL is in the title....but here is my version!

<?php 
$result = mysql_query("SELECT field FROM tbl_name WHERE field = 1");   //replace tbl_name with your tbl 

if (mysql_num_rows($result) == 0) {
    echo 'Do this here';
}else {
    echo 'Do this other thing here!';
}
?>

 

=)

Hey phpsenei,

 

I was wondering about this line that you put in your code

 

 
($row['field'] == 0) ? 0 : $row['field'];

 

could you explain the " ? 0 : " part or give me a link to read up on it?

 

Its a shortened if/else statement. If the field is equal to 0 set it to 0 else set it to $row['field'];

Hey phpsenei,

 

I was wondering about this line that you put in your code

 

 
($row['field'] == 0) ? 0 : $row['field'];

 

could you explain the " ? 0 : " part or give me a link to read up on it?

 

Hey friend, glad to help.

 

It is another conditional operator called ternary, prevents the whole curly bracelet hassle

 

http://ca.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

It's called a ternary operator.  TRUE('?') ELSE(':').

 

$field = ($row['field'] == 0) ? 0 : $row['field'];

 

Is the same exact thing as:

 

if($row['field'] == 0) {
  $field = 0;
} 
else {
   $field = $row['field'];
}

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.