Jaynesh Posted July 10, 2011 Share Posted July 10, 2011 Hello How do I setup an if statement with a query. I want a column to match another piece of data. $post = "SELECT * FROM dbKarma"; $post_query = mysql_query($post); if ( [color=red]???[/color] ) { $col1 = "#CCC"; } else { $col1 = "#fff"; } Quote Link to comment https://forums.phpfreaks.com/topic/241615-if-statements-with-query/ Share on other sites More sharing options...
.josh Posted July 10, 2011 Share Posted July 10, 2011 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"; } } Quote Link to comment https://forums.phpfreaks.com/topic/241615-if-statements-with-query/#findComment-1241006 Share on other sites More sharing options...
Jaynesh Posted July 10, 2011 Author Share Posted July 10, 2011 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; } "); Quote Link to comment https://forums.phpfreaks.com/topic/241615-if-statements-with-query/#findComment-1241008 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.