|
 |
|
Codewalkers Forums
> PHP Related
> PHP Coding
|
Cant connect php to database
Discuss Cant connect php to database in the PHP Coding forum on Codewalkers. Cant connect php to database Having problems with a PHP script you are coding? This is the place to get help!
|
|
|
|
 |
|
|
|
|

Codewalkers Forums Sponsor:
|
|
|

January 30th, 2013, 09:16 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 15 m 50 sec
Reputation Power: 0
|
|
|
Cant connect php to database
Hi all! I would like to ask for help. i have a code that i got from youtube. my problem is i cant connect to my database and i dont know why. i dont think that i have a problem in my code. i have 2 php file. reg.php for the connection and demo-form.php for adding the record.
demo-form.php
<form action="reg.php" method="post" />
<p>First Name: <input type="text" name="fname" /> </p>
<p>Middle Name: <input type="text" name="mname" /> </p>
<p>Last Name: <input type="text" name="lname" /> </p>
<p>Home address: <input type="text" name="homeadd" /> </p>
<p>Email address: <input type="text" name="emailadd" /> </p>
<p>Office/project number: <input type="text" name="ofcnum" /> </p>
<p>Mobile number: <input type="text" name="mobilenum" /> </p>
<p>Home number: <input type="text" name="homenum" /> </p>
<p>Position:
<select name="position">
<option value ="">Select</option>
<option value ="Project Manager">Project Manager</option>
<option value ="Practice Lead">Project Manager</option>
<option value ="Team Leader">Team Leader</option>
<option value ="Team Member">Team Member</option>
</select>
<p>Practice:
<select name="position">
<option value ="">Select</option>
<option value ="Solutions">Solutions</option>
<option value ="Outsourcing">Outsourcing</option>
</select>
<p>Project Name: <input type="text" name="projname" /> </p>
<p>Project Manager: <input type="text" name="projmgr" /> </p>
<p>Team Leader: <input type="text" name="teamlead" /> </p>
<input type="submit" value="Submit" />
<body bgcolor="cccccc">
</form>
reg.php
<?php
define ('DB_NAME', 'emp');
define ('DB_USER', 'root');
define ('DB_PASSWORD', '');
define ('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$value1 = $_POST["fname"];
$value2 = $_POST["mname"];
$value3 = $_POST["lname"];
$value4 = $_POST["homeadd"];
$value5 = $_POST["emailadd"];
$value6 = $_POST["ofcnum"];
$value7 = $_POST["mobilenum"];
$value8 = $_POST["homenum"];
$value9 = $_POST["position"];
$value10 = $_POST["practice"];
$value11 = $_POST["projname"];
$value12 = $_POST["projmgr"];
$value13 = $_POST["teamlead"];
$sql = "INSERT INTO employee (fname, mname, lname, homeadd, emailadd, ofcnum, mobilenum, homenum, position, projname, projmgr, teamlead)
VALUES ('$value1', '$value2', '$value3', '$value4', '$value5', '$value6', '$value7', '$value8', '$value9', '$value10', '$value11', '$value12')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
mysql_close();
?>
|

January 30th, 2013, 10:22 PM
|
|
Contributing User
|
|
Join Date: Apr 2007
Posts: 136
Time spent in forums: 2 Days 10 h 5 m 23 sec
Reputation Power: 7
|
|
|
What is|are the error|s you are getting when you run this?
Plus you should be using mysqli or PDO instead of mysql.
__________________
Bob
Last edited by rbrown : January 30th, 2013 at 10:25 PM.
|

January 30th, 2013, 10:25 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 15 m 50 sec
Reputation Power: 0
|
|
hi rbrown!
i dont get any errors. i just cant connect it to my database

|

January 30th, 2013, 11:23 PM
|
|
Contributing User
|
|
Join Date: Apr 2007
Posts: 136
Time spent in forums: 2 Days 10 h 5 m 23 sec
Reputation Power: 7
|
|
you should be with this line in the code.
PHP Code:
if (!$link) {
die('Could not connect: ' . mysql_error());
}
so if you aren't getting anything,
1) it's not getting to that section of the code
OR
2) you do not have errors turned on in the server
add this to the top of the code and run it.
PHP Code:
error_reporting(E_ALL);
ini_set('display_errors', '1');
|

January 31st, 2013, 12:03 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 15 m 50 sec
Reputation Power: 0
|
|
it's still the same 
|

January 31st, 2013, 12:23 AM
|
|
Contributing User
|
|
Join Date: Apr 2007
Posts: 136
Time spent in forums: 2 Days 10 h 5 m 23 sec
Reputation Power: 7
|
|
Try this below in a separate file, replace "your_whatever" with your settings and tell me what you have.
And what are you testing on and with?
local server, hosted server
windows, unix
Wamp, uniform server, etc...
PHP Code:
<?php
mysql_connect("localhost", "your_username", "your_password") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("your_db") or die(mysql_error());
echo "Connected to Database"; ?>
Last edited by rbrown : January 31st, 2013 at 12:31 AM.
|

January 31st, 2013, 12:26 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 15 m 50 sec
Reputation Power: 0
|
|
|
i replaced you code with this
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
echo “Connected to MySQL<br />”;
mysql_select_db("emp") or die(mysql_error());
echo “Connected to Database”;
?>
and what i got was
"”; mysql_select_db("emp") or die(mysql_error()); echo “Connected to Database”; ?> "
im using wamp server
|

January 31st, 2013, 12:35 AM
|
|
Contributing User
|
|
Join Date: Apr 2007
Posts: 136
Time spent in forums: 2 Days 10 h 5 m 23 sec
Reputation Power: 7
|
|
Sorry... I forgot to fix the double quotes...
try it again with this...
PHP Code:
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("emp") or die(mysql_error());
echo "Connected to Database";
?>
|

January 31st, 2013, 12:40 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 15 m 50 sec
Reputation Power: 0
|
|
i tried it and got this
"; mysql_select_db("emp") or die(mysql_error()); echo "Connected to Database"; ?>

|

January 31st, 2013, 12:42 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 15 m 50 sec
Reputation Power: 0
|
|
|
Hi rbrown!
i assessed the codes. there was a br tag. i removed it then i got a blank page
|

January 31st, 2013, 12:47 AM
|
|
Contributing User
|
|
Join Date: Apr 2007
Posts: 136
Time spent in forums: 2 Days 10 h 5 m 23 sec
Reputation Power: 7
|
|
|
your files has to have a .php not a .html
Unless...
you have it setup in your .htaccess file to process .html files as .php files
Or
if you are using Apache force type directive
|

January 31st, 2013, 01:04 AM
|
|
Contributing User
|
|
Join Date: Apr 2007
Posts: 136
Time spent in forums: 2 Days 10 h 5 m 23 sec
Reputation Power: 7
|
|
|
the test code should work without removing the <br/> tag the reason why you are seeing:
"; mysql_select_db("emp") or die(mysql_error()); echo "Connected to Database"; ?>
is because your test page does not have a php extension.
save it as db_test.php
and try it again.
you should see:
Connected to MySQL
Connected to Database
I'm trying to make sure you can connect to the db first them we can address the rest of your code. There are a few issues.
I have to leave so we can pick this back up in about 4 hours if you still haven't got it working....
Last edited by rbrown : January 31st, 2013 at 01:07 AM.
|

January 31st, 2013, 12:50 PM
|
|
Contributing User
|
|
Join Date: Apr 2007
Posts: 136
Time spent in forums: 2 Days 10 h 5 m 23 sec
Reputation Power: 7
|
|
|
Well where are we at now?
Did you get the db test working?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|