0

I try to print out all user email from table, but i want odd number print in red even number print in blue. how to do this in while loop?

$connect=mysql_connect("localhost", "root", "");
$database=mysql_select_db("phplogin", $connect);

$SQL=mysql_query("SELECT * FROM users");
$num=mysql_num_rows($SQL);
$start=0;

while($start<$num){
    $result_id=mysql_result($SQL, $start, "id");
    $result_email=mysql_result($SQL, $start, "email");

    echo "<b style='color:red'>" . $result_id . $result_email . "</b><br/>";
    echo "<b style='color:blue'>" . $result_id . $result_email . "</b><br/>";

    $start++;
    }
3
  • I'm so sorry I have to put this here, but could not resist - this is pretty much the Fizz Buzz question Commented Dec 15, 2012 at 3:27
  • *next time please use PDO instead mysql_query Commented Dec 15, 2012 at 3:27
  • You can do this with just css plus inline css is bad practice, as well as using <b>. Commented Dec 15, 2012 at 3:37

1 Answer 1

4
$connect=mysql_connect("localhost", "root", "");
$database=mysql_select_db("phplogin", $connect);

$SQL=mysql_query("SELECT * FROM users");
$num=mysql_num_rows($SQL);
$start=0;

while($start<$num){
    $result_id=mysql_result($SQL, $start, "id");
    $result_email=mysql_result($SQL, $start, "email");
    if ($start&1) {
      echo "<b style='color:red'>" . $result_id . $result_email . "</b><br/>";
    }
    else {
      echo "<b style='color:blue'>" . $result_id . $result_email . "</b><br/>";
    }
    $start++;
    }
Sign up to request clarification or add additional context in comments.

4 Comments

How $start&1 works here? Whether it should take all odd ones?
@Justin no clue. It just does. Couldn't find any documentation anywhere
Yeah but I can't find in the PHP.net documentation. I found this: php.net/manual/en/language.operators.bitwise.php, but it doesn't seem to describe this behavior.

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.