MySQL and PHP display problem?
I can't get the following code to display items from the database where the parent_id is equal to the id.
Here is the code below.
// Query member data from the database and ready it for display
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT * FROM categories WHERE id=parent_id");
if (!$dbc) {
// There was an e开发者_如何学Gorror...do something about it here...
print mysqli_error();
}
while ($row = mysqli_fetch_assoc($dbc)) {
echo '<li><a href="' , $row['url'] , '" title="' , $row['description'] , '">' , $row['category_name'] , '</a>';
}
I think I know what I'm doing wrong how can I have this query check a previous query?
I'd put the SQL query in a var so you can output it, then try this direct in the database to see if there are any matching rows:
$mysqli = new mysqli("localhost", "root", "", "sitename");
$query = "SELECT * FROM categories WHERE id=parent_id";
echo $query;
i think this line should be rethought:
if ($row['parent_id'] == $row['id']) {
cause are u sure its the correct logic?
Try do do something like:
if ($row['parent_id'] == $row['id']) {
echo '<li><a href="' , $row['url'] , '" title="' , $row['description'] , '">' , $row['category_name'] , '</a>';
}
else {
echo "there is no match!";
}
And see if the problem is, in fact, not in the SQL query but in you app logic (which I think is more likelly).
That said try to debug SQL queries with phpMyAdmin. Just post the query you want there and check for the output. If the output is ok, look into your logic, if not look into your query.
Do try to split your problem into smaller chunks, makes your debugging much easier.
Hope it helps.
精彩评论