0

I have following while loop.

$readNews_SQLselect = "SELECT  ";
$readNews_SQLselect .= "id, content ";  // rows names
$readNews_SQLselect .= "FROM ";
$readNews_SQLselect .= "news ";         // table name


$readNews_SQLselect_Query = mysql_query($readNews_SQLselect);   

//$indx = 1;    
while ($row = mysql_fetch_array($readNews_SQLselect_Query, MYSQL_ASSOC)) {
    $ID = $row['id'];
    $CONTENT = $row['content'];

    echo '<li>' . $ID .' ' . $CONTENT . '</li>';

    //$indx++;

}

mysql_free_result($readNews_SQLselect_Query);

How would it look if I added '$LIVE = $row['live'];' with condition that only rows with live value string of '0' should be displayed? Live will be either 0 / 1 (VARCHART).

Any suggestion much appreciated.

5
  • if( $LIVE == 0 ) echo ... ? Commented Aug 5, 2011 at 9:36
  • 2
    If you ask a question on SO for every new line in your program it will take a looong time to develop it. get a book or tutorial Commented Aug 5, 2011 at 9:37
  • @yi_H - exercises only :) Is my third day using SQL / PHP. Commented Aug 5, 2011 at 9:39
  • @yi_H - can you suggest any worth reading tutorials? I went through the whole safaribooks video library. Commented Aug 5, 2011 at 9:45
  • No idea.. stackoverflow.com/questions/772349/… stackoverflow.com/questions/90924/… Commented Aug 5, 2011 at 9:50

3 Answers 3

5

Just add this to your query:

$readNews_SQLslect .= " WHERE live = '0'";
Sign up to request clarification or add additional context in comments.

5 Comments

As last? Which one will be better, in the while loop or in SQL query?
The SQL Query of course : faster, less memory used to fetch data, less CPU used because of less whille loop iterations.
If you do it in your SQL query the database wont even have to look at those rows, if you do it in your loop your database would have to fetch the rows, and you have to have more logic skipping them.
Yes. It does make sense. Sorry for my silly questions - I'm new to the PHP / SQL stuff.
Silly questions dont exist ;) Just make sure you provide enough information, and show some effort, then people will answer you.
3

Alter $readNews_SQLselect

$readNews_SQLselect = "SELECT  ";
$readNews_SQLselect .= "id, content ";   // rows names
$readNews_SQLselect .= "FROM ";
$readNews_SQLselect .= "news ";          // table name
$readNews_SQLselect .= "WHERE live='0'"; // condition

Comments

1
  1. alter your sql:

    $readNews_SQLselect = "SELECT  ";
    $readNews_SQLselect .= "id, content ";  // rows names
    $readNews_SQLselect .= "FROM ";
    $readNews_SQLselect .= "news ";         // table name
    $readNews_SQLselect .= "where live = '0' "; 
  2. if condition:

    if ($row['live'] == '0') { echo '<li>' . $ID .' ' . $CONTENT . '</li>'; }

2 Comments

is there any other option as WHILE expr?
@RiaD, tahks! something went bad as i was typing an answer. @Pete, just add ... and ($row['live'] == '0') to while... but it's not very good

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.