If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Trying to connect to MySQL using PHP

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-03-06, 00:40
johnnybregar johnnybregar is offline
Registered User
 
Join Date: Dec 2006
Posts: 3
Trying to connect to MySQL using PHP

Hi,

First post.

I'm a total PHP newbie. I'm trying to get a simple db up and running so I can submit a form and take people's email addresses to be on my mailing list.

Here's the deal:

I have two files, one db_login.php with that looks like this:
<?php
$db_host="mysql213.secureserver.net";
$db_database="mydbname";
$db_username="myusername";
$db_password="mypassword";
?>

That php file lives in the same folder (root/html) as the following file (called phptest.php:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>
<body>
<p>&nbsp;</p>
<p>
<?php
include(db_login.php);
$connection=mysql_connect($db_host,$db_database,$d b_password);
if (!$connection) {
die ("Could not connect to the db");
}
?>
</body>
</html>

When I browse to phptest.php I get the following error:
Warning: main(db_loginphp): failed to open stream: No such file or directory in /home/content/j/o/h/johnnybregar/html/html/phptest.php on line 13

Warning: main(db_loginphp): failed to open stream: No such file or directory in /home/content/j/o/h/johnnybregar/html/html/phptest.php on line 13

Warning: main(): Failed opening 'db_loginphp' for inclusion (include_path='.:/usr/local/lib/php') in /home/content/j/o/h/johnnybregar/html/html/phptest.php on line 13

Warning: mysql_connect(): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /home/content/j/o/h/johnnybregar/html/html/phptest.php on line 14
Could not connect to the db

Is my PHP totally messed up? Or are these directories insane? I'm lost and could use a helping hand to get up and running. Nowhere do I have a directory structure like what it is looking for....

Thanks,

Johnny B.
Reply With Quote
  #2 (permalink)  
Old 12-03-06, 02:58
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,259
where is your file db_login?... that is one cause for problems, or alternatively where are the other 3 lines, as PHP is reporting as the inlcude is on iline 13 yet PHP reports the fault on 10


if you are concerned that you installation isnt working then Id suggest you run a simple testscript calling the phpinfo() function

eg
<?php
phpinfo(-1)
?>

the phpinfo should provide a lot of bumpf, not least of which is the database server the webserver is connected to

Im not sure you webserver is correclty specified
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 12-03-06, 09:45
dbmab dbmab is offline
Registered User
 
Join Date: Apr 2006
Location: Denver, Co. USA
Posts: 240
The include(...) is expecting a string as a parameter for the filepath/filename. Try - include('db_login.php');
Reply With Quote
  #4 (permalink)  
Old 12-03-06, 15:22
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
try hardcoding the inofrmation you have in that file directly in your php file. see if everything connects properly. if it does then the problem is not with mysql and php connecting but with your php code including the file.
Reply With Quote
  #5 (permalink)  
Old 12-04-06, 00:03
johnnybregar johnnybregar is offline
Registered User
 
Join Date: Dec 2006
Posts: 3
OK. Thanks for the suggestions. By hardcoding the db connection, I got rid of the errors. There may be something up with my php.ini file, pointing to a different include dir. Not sure. Don't care.

Now I simply need help with my form submittal - I'm not seeing any rows get added by my db with the following code:

<?php
$hostname="mysql213.secureserver.net";
$username="username";
$password="password";
$dbname="dbname";
$usertable="mailinglist";
mysql_connect($hostname,$username,$password);
mysql_select_db($dbname);
$FirstName = $_GET[‘FirstName’];
$LastName = $_GET[‘LastName’];
$EmailAddress = $_GET[‘EmailAddress’];
?>
<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method=GET />
<p>First Name: <input type="text" name="FirstName" size="27" value="<?php echo $FirstName; ?>"><br>
Last Name: <input type="text" name="LastName" size="27" value="<?php echo $LastName; ?>"><br>
Email: <input type="text" name="EmailAddress" size="49" value="<?php echo $EmailAddress; ?>"></p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
<?php
mysql_query("INSERT INTO 'mailinglist' (FirstName,LastName,EmailAddress) VALUES ($FirstName,$LastName,$EmailAddress)");
?>

Any thoughts on why my data is not showing up? Have I screwed something up?

Thank you all so much for the help here...
Reply With Quote
  #6 (permalink)  
Old 12-04-06, 01:14
johnnybregar johnnybregar is offline
Registered User
 
Join Date: Dec 2006
Posts: 3
Figured it out.

Among other things, it didn't like the single quotes around my field names in my $_GET['fieldname']. Seems like there are a lot of examples out there that use these single quotes. Not sure why I couldn't use them.

Oh well, I got data now. That's good.

jb
Reply With Quote
  #7 (permalink)  
Old 12-04-06, 07:29
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
jb, just to tidy your code up, can you use tags around it please (they'll work on other sites you visit as well most of the time).

remove the spaces in the first of the two tags.

For wrapping php use [ php] and [/php]
for wrapping sql code alone use [ code] and [/code]

One last thing and this is important, take the time to read the php manual on using mysql_real_escape_string on your variables that are coming in from a user (with either GET or POST variables). Doing it the way you currently are is leaving your database vulnerable to injection attacks.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On