Question:
Can not connect to MYSQL database through PHP.?
ROCK
2010-02-01 07:09:34 UTC
The following php program is not connecting with mysql.It display the word welcome and the variable in echo but the connection is not getting established.Solve this.

$u=$_POST[username];
$p=$_POST[password];
echo "Welcome ,$u";

$conn = mysql_connect ("localhost","root","password");

$db = mysql_select_db("empdb", $conn) or die("Couldn't select database.");

$sql = "insert into employee (username) values("$u")";

$sql_result = mysql_query($sql,$conn) or die("Couldn't execute query.");

?>
Five answers:
Jason W-S
2010-02-01 07:26:28 UTC
change $sql = "insert into employee (username) values("$u")";



to $sql = "insert into employee (username) values("'.$u.')";





Or better yet



$sql = sprintf("insert into employee (username) values(%s)",

mysql_real_escape_string($u));
rbjolly
2010-02-01 12:46:38 UTC
Jason and Pete offer some good suggestion. But it would help if we had a better understanding of your problem. You state that the code displays the output of your echo statement, but you do not tell us if any errors are getting displayed (this would be helpful to know if there are any). Then you state a connection is not getting made.



If it is true that the connection is not made, then the point where your code fails first is on the mysql_connect statement. In order to verify this, you should modify your connect statement to handle the connection error. So the statement should be written like so:



$conn = mysql_connect ("localhost","root","password") or die("Could not connect to db.");



If you see the "Could not connect to db" message after modifying the connect statement and executing your code, then you definitely are not connecting to MySQL. Assuming the user credentials are correct for a localhost connection, the first thing I would check is to see if PHP is configured to talk to MySQL. You can do this by creating a page that contains the statement:




phpinfo();

?>



Executing the above statement will display your PHP configuration information. Search for a section titled mysql. If you can't find this section, then PHP does not have the necessary extension to talk to MySQL. So you will have to locate your php.ini file (usually found in the php directory) and add the following to the end of the file:



[PHP_MYSQL]

extension=php_mysql.dll



This assumes PHP is installed on a Windows machine, that the extension_dir is set, and php_mysql.dll resides in the extension directory. Once you have made the changes you must restart your web server for the changes to take affect (just restart your computer if you don't know how to restart the web server).



The missing extension is a common mistake for new users who have installed PHP and MySQL for the first time. If none of the suggestions work, contact me and I will try to help you get your code working.
Pete S
2010-02-01 10:24:32 UTC
Okay, my simple suggestion is to connect to mysql using the command line, with that username and password with that database



mysql -D empdb -u root -p



If you can't do that, then clearly you have a problem with mysql itself.

You'll need to install mysql of course, have a schema called empdb,

and a user called root (almost always there, but bad practice to use this account daily)

with the correct password



also, as pointed out prior, the $sql query itself looks like a syntax error.
jhaban
2016-10-19 07:40:43 UTC
you will no longer have the skill to aim this. you are able to desire to create one extra desirable table that links to the 1st that has the columns which you truly pick. the technique to stipulate how this works is talked approximately as normalling. you are able to desire to have a database with tables that are 2nd time-honored or maybe third time-honored. it ought to take an entire e book to stipulate all of this, yet you are able to desire to be able to do slightly study online that supplies you the basics as nicely.
kimcord_99
2010-02-01 07:59:24 UTC

$u=$_POST[username];

$p=$_POST[password];

echo "Welcome ,$u";



$conn = mysql_connect ("localhost","root","password");



mysql_select_db("empdb", $conn) or die("Couldn't select database.");



$sql = "insert into employee (username) values("'".$u."'")";



mysql_query($sql) or die("Couldn't execute query.");



?>


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...