简体   繁体   中英

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 error...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'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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM