Khalid
Khalid

Reputation: 59

MySQL error connecting when creating a user using PHP

I'm trying to create a php form that for registering the user and store their information in MySQL table. Here is the error following that a PHP code and the MySQL table.

Warning: mysql_query(): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /hsphere/local/home/khalidalomar/khalidalomar.com/talent/added.php on line 22 Warning: mysql_query(): A link to the server could not be established in /hsphere/local/home/khalidalomar/khalidalomar.com/talent/added.php on line 22 Invalid query: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

Here is my PHP code

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()); }

?>

if you needed the SQL query I could provide that.

Upvotes: 0

Views: 258

Answers (2)

user1189402
user1189402

Reputation:

happened before. simply define the connection right before the php validation or use an absolute path. furthermore, you could create a DB con class in your session file(i assume you have one, otherwise include it in the connection and simply instantiate whenever you need a connection)

<?php $root = realpath($_SERVER["DOCUMENT_ROOT"]);
inlcude($root./connect.php);//edit the path as needed.
?>

other than that, you should know never to use relative paths, as this is a very bad habit. Also, i wouldn't use mysql for that. Go for mysqli parametized queries. It's an effort to upgrade but it's well worth the trouble.

Upvotes: 1

tftd
tftd

Reputation: 17032

Either your mysql.conf settings are wrong, your php is compiled to search for mysql.sock in the wrong path or your mysql server is just not running. From what I see in the code you have posted you're not connecting to the database at all? Do you have mysql_connect(); mysql_select_db(); in the connect.php file?

Upvotes: 2

Related Questions