在XAMPP环境下无法创建MySQL数据表的问题求助
Hey there! Let's work through why your table isn't creating properly. I’ve noticed a few key issues in your code that are likely causing the problem—let’s fix them step by step:
1. 停止使用废弃的mysql_*函数
The mysql_connect() and mysql_select_db() functions you’re using are completely deprecated and removed in modern PHP versions. They’re unsafe and no longer supported. Switch to either mysqli (MySQL Improved) or PDO instead—both are secure, maintained, and offer better error handling.
2. 补全并修正你的SQL语句
Your CREATE TABLE query is cut off at latin2_hungarian_...—you need to finish the collation (like latin2_hungarian_ci) and ensure all field definitions are complete. Also, the backticks around field names aren’t strictly necessary here (unless you’re using reserved keywords), but they won’t break things if you keep them.
3. 添加错误处理(关键!)
Right now, you have no way to see what’s actually going wrong. Adding error checks will tell you exactly why the table isn’t creating—whether it’s a connection issue, missing database, or invalid SQL.
修正后的完整代码示例
Here’s a revised version using mysqli (process-oriented style, easy to adapt from your original code):
<?php // 建立数据库连接 $kapcsolat = mysqli_connect("127.0.0.1", "root", "jelszo"); // 检查连接是否成功 if (!$kapcsolat) { die("连接失败: " . mysqli_connect_error()); } // 选择目标数据库 if (!mysqli_select_db($kapcsolat, "gyakorlas")) { die("无法选中数据库 'gyakorlas': " . mysqli_error($kapcsolat)); } // 完整的CREATE TABLE语句(补全了排序规则,可添加更多字段) $parancs = "CREATE TABLE uj_tabla ( id BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY, nev VARCHAR(100) CHARACTER SET latin2 COLLATE latin2_hungarian_ci NOT NULL -- 如果需要其他字段,在这里继续添加,确保格式正确 ) ENGINE=InnoDB DEFAULT CHARSET=latin2"; // 执行SQL并检查结果 if (mysqli_query($kapcsolat, $parancs)) { echo "数据表创建成功!"; } else { echo "创建失败,错误信息: " . mysqli_error($kapcsolat); } // 关闭数据库连接 mysqli_close($kapcsolat); ?> <html> <head> <title>MySQL alapok</title> </head> <body> </body> </html>
额外排查步骤
- Double-check that the
gyakorlasdatabase actually exists in phpMyAdmin—if not, create it first. - Verify your root password: By default, XAMPP’s MySQL root user has an empty password. If you set
jelszoas the password, make sure it matches what’s configured in XAMPP. - Run the code and look at the error message it outputs—this will give you precise details if something still isn’t working.
内容的提问来源于stack exchange,提问作者Wexutar

