PHP从MySQL输出表格异常:本地正常远程服务器报错求助
Hey there, let's get your certificate lookup tool working smoothly on AwardSpace. The errors you're seeing all trace back to a core database connection issue plus a tiny syntax mistake. Let's break this down step by step:
1. The Root Issue: Database Access Denied
The first warning spells out the main problem loud and clear:
Warning: mysqli_connect(): (42000/1044): Access denied for user '2950881_choice'@'%' to database 'Certificates'
This means your user account 2950881_choice doesn't have permission to access the Certificates database on AwardSpace's servers. Here's how to fix it:
- Double-check your database credentials: AwardSpace usually prefixes database names with your account ID. So instead of
Certificates, your database name is likely something like2950881_Certificates(matching your username's prefix). Log into your AwardSpace control panel to confirm the exact database name, server address, username, and password. - Verify user permissions: Use AwardSpace's phpMyAdmin tool to make sure
2950881_choicehasSELECT(and any other required) permissions for your target database. If not, grant those permissions directly through the control panel.
2. Fix the Syntax Mistake in Your Connection Code
Take a close look at your mysqli_connect line:
$conn=mysqli_connect("server", "user", "password" "database");
You're missing a comma between "password" and "database"! This tiny oversight breaks the entire connection. Correct it to:
$conn=mysqli_connect("server", "user", "password", "database");
3. Add Error Handling to Debug Faster
To avoid vague follow-up errors, add a check right after your connection code to catch issues early:
$conn = mysqli_connect("your_server", "your_username", "your_password", "your_database"); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
This will show you a clear, direct error message if the connection fails, instead of the chain of confusing warnings you're seeing now.
4. Secure Your Code Against SQL Injection
Right now, you're directly inserting user input into your SQL query—this is a major security risk. Swap your current query code for a prepared statement to fix this:
if(isset($_POST['search'])) { $set = $_POST['search']; // Use a prepared statement to avoid injection $stmt = mysqli_prepare($conn, "SELECT * FROM Certificates WHERE CertificateNum = ?"); mysqli_stmt_bind_param($stmt, "s", $set); // "s" indicates we're passing a string mysqli_stmt_execute($stmt); $result = mysqli_stmt_get_result($stmt); while($rows = mysqli_fetch_array($result)) { // Your existing table row output code goes here } } else { echo "Nothing Found"; }
Prepared statements block attackers from injecting malicious SQL into your query, which is critical for any public-facing site.
Once you fix the connection credentials, the syntax error, and add proper error handling, your tool should work exactly like it did on your local server!
内容的提问来源于stack exchange,提问作者xdiiabizi

