I have a block of PHP code which is called through AJAX to insert a value into a table, then return a list of matching values from that table. The insert query runs just fine, but the select query returns nothing. If I go into phpmyadmin and manually run the select query, I get the expected result set, but in the PHP code, fetch_assoc() returns null. I've confirmed that all variables are being passed correctly, but the code never enters the while loop because there are no results in the set. Below is the set of relevant code:
if (isset($_POST['action']) && $_POST['action'] == "link" && !empty($_POST['link_id']) && !empty($_POST['cat_id'])) {
require_once(realpath(dirname(__FILE__)) . "/../config/config.php");
$link_id = (int)$_POST['link_id'];
$category_id = (int)$_POST['cat_id'];
$query = $conn->query("INSERT INTO links_to_categories (link_id, category_id) VALUES ({$link_id}, {$category_id})");
if ($query) {
$cats = $conn->query("SELECT * FROM links_to_categories lc LEFT JOIN categories c ON lc.category_id = c.category_id WHERE lc.link_id = {$link_id}");
while ($cat = $cats->fetch_assoc()) {
echo "{$cat['category_name']} (<a href=\"#\" onclick=\"unlinkCategory({$link_id}, {$category_id});return false;\">x</a>)<br>";
}
}
die();
}
This and one other function (which is nearly identical but does a DELETE instead of INSERT) are the only ones in my code that have this problem. I have a dozen other queries that are working properly, including the same SELECT query in a different part of the code which runs fine, so this is very puzzling.