PHP新手求助:mysqli_query()参数类型错误问题排查
Hey there! I totally get how frustrating this error can be when you're just starting out with PHP. Let's walk through exactly what's going wrong and how to fix it step by step.
What's Causing the Error?
The warning tells us that when you called mysqli_query(), the first parameter (which should be a valid MySQLi database connection object) is null. Right now, your code is only passing the SQL query to mysqli_query()—but this function requires two arguments: the active database connection first, then your SQL string. Without that connection object, PHP doesn't know which database to run your query against.
Step-by-Step Fix
Let's adjust your code to include the missing connection object and get it working:
First, set up a valid database connection
Before youradd_signaturefunction (probably in your theme'sfunctions.phpor a dedicated config file), add code to connect to your database. Replace the placeholder values with your actual database credentials:// Database connection setup $db_host = 'localhost'; // Usually localhost for local setups $db_user = 'your_database_username'; $db_pass = 'your_database_password'; $db_name = 'your_database_name'; // Create the connection $conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name); // Check if the connection succeeded if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }Update your
add_signaturefunction
You need to access that connection object inside your function. We'll use aglobaldeclaration to pull it in, then pass it as the first argument tomysqli_query():add_filter('the_content','add_signature', 1); function add_signature($text) { global $post, $conn; // Add $conn to access the database connection if(($post->post_type == 'post') || ($post->post_type == 'page')){ $sql_site_d = "select * from orders_discounts where url = 'homeworkmaid.com' and status =1"; // Pass the connection as the first parameter to mysqli_query $rs_results_site_d = mysqli_query($conn, $sql_site_d) or die(mysqli_error($conn)); $total_site_d = mysqli_num_rows($rs_results_site_d); if ($total_site_d > 0){ $row_site_d = mysqli_fetch_array($rs_results_site_d); // Add your signature HTML/output here using the data from $row_site_d $text .= '<div class="post-signature">'; // Example: $text .= 'Special Discount: ' . $row_site_d['discount_percent'] . '%'; $text .= '</div>'; } } // Critical! Return the modified content so it displays on your site return $text; }
Quick Additional Tips
- Don't skip returning
$text: If you forget this line, your posts and pages will show up empty—since thethe_contentfilter relies on returning the processed content to display. - Avoid global variables long-term: While
global $connworks for a quick fix, better practice later is to pass the connection object directly to the function (dependency injection) to keep your code more organized and maintainable. - Guard against SQL injection: Right now your query uses a hardcoded URL, but if you ever use dynamic values (like user input), use prepared statements with
mysqli_prepare()to keep your database safe from attacks.
内容的提问来源于stack exchange,提问作者Anthony Muchangi

