Log in

View Full Version : Database with Column "Approved"



onestopplay
06-30-2009, 11:30 PM
So I have a database,
id | title | approved
1 | title1 | yes
2 | title2 | yes
3 | title3 | no

Then I have this PHP script:


<?php
//opens connection to mysql server
$dbc = mysql_connect("localhost", "username", "password");
if (!$dbc)
{
die('Not connected :' . mysql_error());
}
//select database
$db_selected = mysql_select_db("mydatabase", $dbc);
if (!$db_selected)
{
die("cant connect :" . mysql_error());
}

$result = mysql_query("SELECT * FROM mytable ORDER BY title");

while($row = mysql_fetch_array($result))
{
echo "" . $row['title'] . "";
echo "<BR>";
}
mysql_close($dbc);
?>

That displays each Title. How do I make it so that only approved=yes titles show? Thanks in advanced for any help.

SChaput
06-30-2009, 11:36 PM
Try this...


$result = mysql_query("SELECT * FROM mytable ORDER BY title WHERE approved='yes'");

onestopplay
06-30-2009, 11:43 PM
One more thing...
what if I want to say
WHERE approved='yes'

AND somethingelse='something'

Can I say AND?

SChaput
07-01-2009, 12:45 AM
Yes you can.



$result = mysql_query("SELECT * FROM mytable ORDER BY title WHERE approved='yes' AND another='no'");

heavensgate15
07-02-2009, 10:56 AM
ei.. w8 a minute... I think it's not possible that in your query, the order by will be put first then the where clause... Because there is a sequence which clause will be put first.... I think the complete (not a complete one, complete only in the clause part) syntax of the select statement is:

Select * from Table_name
[where clause]
[group by]
[having]
[order by]
[limit];

You must follow the sequence else it will generate an error.. though you can omit some of the clauses, like I will only use the [order by] clause, But if you use the [order by] clause, make sure that the [where],[group by],and [having] clause is not put in the same select statement afterward.

onestopplay
07-02-2009, 11:14 PM
So I was working on my site today and I kept getting this error message that was really annoying me on most of my scripts. Finally, I fixed it by doing WHERE first, then ORDER BY. Thanks for your help.