Jump to content

if statements with query


Jaynesh

Recommended Posts

when you use mysql_query(), that returns a result source.  You must then get the data out of the result source using mysql_result, mysql_fetch_assoc, mysql_fetch_row, or similar (follow the links).  For example, if one of your database columns is called "foo" and you want $col1 to be assigned "#CCC" if the value of the "foo" column is "bar", and "#fff" if not, you would do something like this:

 

$post = "SELECT * FROM dbKarma";
$post_query = mysql_query($post);

// loop through each row of the returned query results.. 
while ($row = mysql_fetch_assoc($post_query)) {
  // if column 'foo' equals 'bar'...
  if ($row['foo'] == 'bar') {
    $col1 = "#CCC";
  } else {
    $col1 = "#fff";
  }
}

Hello

 

Another question. Slightly unrelated.

 

is this possible? I am basically trying to add if statements to css.

 

 

include ("dbConfig.php");
header('Content-type: text/css');
$user = $_SESSION["valid_id"];

$post = "SELECT * FROM dbPosts";
$post_query = mysql_query($post);

while ($row = mysql_fetch_assoc($post_query)) {

if ( $row['username_id'] == $user )
{
$col1 = "#CCC";
}
else
{
$col1 = "#fff";
}
}



echo ("
  #postbox {

  width: 700px ;
  margin-left: auto ;
  margin-right: auto ;
height: 0px auto;
height-top: 10px;
border-width:1px;
border-style:dashed;
border-color:#353535;
background-color:$col1;
font-size:24px;
}
");

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.