PHP调用fetch_assoc()报错:Call to a member function fetch_assoc() on boolean
Hey there! That error pops up when your database query fails and returns false instead of a valid result set object—so trying to call fetch_assoc() on that boolean value throws a fatal error. Let’s walk through how to diagnose and fix this step by step:
1. First, validate your SQL syntax
The most common culprit is a broken SQL query. Grab the exact SQL string your code is trying to run, paste it directly into a database tool like phpMyAdmin or MySQL Workbench, and execute it manually. Look for:
- Typos in table/column names (e.g.,
userssinstead ofusers) - Mismatched quotes (e.g., forgetting to close a single quote around a string value)
- Missing keywords or invalid syntax (e.g.,
SELECinstead ofSELECT)
For example, if your code has:
$sql = "SELECT * FROM userss WHERE id = 1"; // Typo: "userss" is wrong $result = $conn->query($sql); $row = $result->fetch_assoc(); // This will throw the error
Fixing the table name here would resolve the issue immediately.
2. Verify your database connection is working
If your script can’t connect to the database at all, every query will return false. Always add a check right after creating your connection to catch this:
$conn = new mysqli('localhost', 'your_username', 'your_password', 'your_database'); // Check for connection errors if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
Double-check that your host, username, password, and database name are all correct—even a tiny typo here will break everything.
3. Add explicit error handling for your query
Never assume your query will succeed. Add a check to catch failures and get detailed error feedback from MySQL:
$sql = "SELECT * FROM users WHERE id = 1"; $result = $conn->query($sql); if ($result === false) { // Print the exact MySQL error to debug echo "Query failed: " . $conn->error; } else { // Safe to fetch results now $row = $result->fetch_assoc(); // Do something with $row... }
The $conn->error message will tell you exactly why MySQL rejected your query—this is your most valuable tool for debugging.
4. Avoid SQL injection (and accidental syntax errors) with prepared statements
If your query uses user input (like values from $_GET or $_POST), directly concatenating it into SQL can cause syntax errors (e.g., if the input contains quotes) or serious security risks. Use prepared statements instead:
$userId = $_GET['id']; // Prepare the query with a placeholder $stmt = $conn->prepare("SELECT * FROM users WHERE id = ?"); // Bind the input ( "i" = integer, "s" = string, "d" = double ) $stmt->bind_param("i", $userId); // Execute the query $stmt->execute(); // Get the result set $result = $stmt->get_result(); // Now fetch safely $row = $result->fetch_assoc();
Prepared statements eliminate syntax issues from user input and protect against SQL injection—win-win.
If you’re still stuck, share your full code (with sensitive info like passwords removed!) and we can dig deeper.
内容的提问来源于stack exchange,提问作者DMM Official




