我正在尝试创建一个php表单,用于注册用户并将他们的信息存储在MySQL表中。下面是PHP代码和MySQL表后面的错误。
/hsphere/local/home/khalidalomar/khalidalomar.com/talent/added.php警告: mysql_query():无法通过第22行/hsphere/local/home/khalidalomar/khalidalomar.com/talent/added.php中的socket '/var/lib/ MySQL /mysql.sock‘(2)连接到本地MySQL服务器,警告: mysql_query():无法在第22行的/hsphere/local/home/khalidalomar/khalidalomar.com/talent/added.php中建立到服务器的链接,无效查询:无法通过socket’/var/lib/mysql/mysql.sock连接到本地mysql服务器(2)
这是我的PHP代码
include("connect.php");
// Talent table
$t_username = strip_tags(substr($_POST['t_username'],0,32));
$t_password = strip_tags(substr($_POST['t_password'],0,32));
$t_fname = strip_tags(substr($_POST['t_fname'],0,32));
$t_lname = strip_tags(substr($_POST['t_lname'],0,32));
$t_cname = strip_tags(substr($_POST['t_cname'],0,32));
$t_phone = strip_tags(substr($_POST['t_phone'],0,32));
$t_description = strip_tags(substr($_POST['t_description'],0,32));
$t_tags = strip_tags(substr($_POST['t_tags'],0,32));
$results = mysql_query("INSERT INTO talents (id, t_username, t_password, t_fname, t_lname, t_cname, t_phone, t_description, t_tags, t_approved, t_dateofreg)
VALUES ('', '$t_username', '$t_password', '$t_fname', '$t_lname', '$t_cname', '$t_phone', '$t_description', '$t_tags', 'N', NOW() )"); // store date by NOW() // date int(8)
if($results) { echo "Successfully Added"; } else { die('Invalid query: '.mysql_error()); }
?>如果您需要SQL查询,我可以提供它。
发布于 2012-02-27 03:04:41
您的mysql.conf设置可能是错误的,您的php被编译成在错误的路径中搜索mysql.sock,或者您的mysql服务器没有运行。根据我在您发布的代码中所看到的,您根本没有连接到数据库?您在mysql_connect(); mysql_select_db();文件中有connect.php吗?
发布于 2012-02-27 03:10:50
以前也发生过。只需在php验证之前定义连接,或者使用绝对路径。此外,您可以在会话文件中创建一个DB类(我假设您有一个类,否则将它包含在连接中,并在需要连接时简单地实例化)
<?php $root = realpath($_SERVER["DOCUMENT_ROOT"]);
inlcude($root./connect.php);//edit the path as needed.
?>除此之外,你应该知道永远不要使用相对路径,因为这是一个非常坏的习惯。而且,我也不会为此使用mysql。转到mysqli参数化查询。这是一个升级的努力,但它是值得的麻烦。
https://stackoverflow.com/questions/9459375
复制相似问题