0

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.

3
  • 1
    you should provide the code in connect.php too since this is a connect to the database problem, not a failed query... Commented Feb 27, 2012 at 2:38
  • @MichalPlško it has my host name, username of database, password, and database name. You think it is necessary to post the codes of it as well? Commented Feb 27, 2012 at 2:45
  • do not expose the passwords of course :) xxx them out before posting.... there is no connect call to the database in the code you posted here, so either it is in connect.php or you are missing something... Commented Feb 27, 2012 at 2:52

2 Answers 2

2

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?

Sign up to request clarification or add additional context in comments.

3 Comments

Perfect! I didn't know that I need to add the mysql_connect(); to my added.php page and to the connect.php Thanks so much =)
that's not correct @Khalid - either add it to connect.php or to added.php, do not add it to both... connect.php is called from the script, if you add it to both files you end up with duplicit connect calls...
@MichalPlško Thanks again. I will added to the connect.php only =) appreciated.
1

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.

1 Comment

Thanks for the tip @Alex appreciate it =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.