Reply
PHP-MySQL Connection Problem
Old 03-23-2006, 02:21 PM PHP-MySQL Connection Problem
Average Talker

Posts: 16
Hello!

I have created a MySQL database on my server, and I want to use it with PHP. I know how to use it, and I have already written some code. The first part is a simple form which sends lets the use enter his email address and submit to the newsletter database. But, I can't connect to the database I get the 'or die(...)' error. I put the mysql_connect() in a variable to see if it connected, but it didn't. I have tried all kinds of solutions without success.

Any advice on what I could be doing wrong, or am I on my own here?

Code:
<?php

     $username = "****";
     $password = "****";
     $database = "****";

     $email = $_POST[ 'email' ];

     $query = "INSERT INTO nyhetsbrev VALUES( '', '$email' );";

     mysql_connect( localhost, $username, $password );
     @mysql_select_db( $database ) or die( "Couldn't select MySQL database" );

     mysql_query( $query );

     mysql_close( );

?>
Also, is the @ before mysql_select_db() necessary? What's it for?
Cerasti is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 03-23-2006, 06:45 PM Re: PHP-MySQL Connection Problem
Christopher's Avatar
Iced Cap

Latest Blog Post:
PHP and Unicode with UTF-8
Posts: 3,111
Location: Toronto, Ontario
The @ symbol suppresses any errors PHP might spit out. So in that case, you are suppressing the default mysql_select_db() error and outputting your own. You need more information to debug your script, try changing your die message to contain the information from mysql_error():

PHP Code:
or die('Error: ' mysql_error()); 
That'll tell you exactly what the problem is.
Christopher is offline
Reply With Quote
View Public Profile Visit Christopher's homepage!
 
Old 03-24-2006, 12:11 AM Re: PHP-MySQL Connection Problem
Super Talker

Posts: 144
looks to me like it's dying on localhost. you need to provide the function with a string containing the address to the server, if it's localhost, then "localhost".

$link_id = mysql_connect("localhost", $username, $password) or die(mysql_error());

also, your INSERT query is wrong. you are missing your column list...

INSERT INTO table (column, list) VALUES ('column', 'values')
__________________
create.vibe
web development portfolio
createvibe.com is offline
Reply With Quote
View Public Profile Visit createvibe.com's homepage!
 
Old 03-24-2006, 07:54 AM Re: PHP-MySQL Connection Problem
Average Talker

Posts: 16
Big thanks! It works now!
Cerasti is offline
Reply With Quote
View Public Profile
 
Old 03-26-2006, 10:20 AM Re: PHP-MySQL Connection Problem
floridian's Avatar
Novice Talker

Posts: 9
Name: Sergey
Location: Florida
Quote:
Originally Posted by Cerasti
Hello!

Code:
<?php
 
     $username = "****";
     $password = "****";
     $database = "****";
 
     $email = $_POST[ 'email' ];
 
     $query = "INSERT INTO nyhetsbrev VALUES( '', '$email' );";
 
     mysql_connect( localhost, $username, $password );
     @mysql_select_db( $database ) or die( "Couldn't select MySQL database" );
 
     mysql_query( $query );
 
     mysql_close( );
 
?>
Also, is the @ before mysql_select_db() necessary? What's it for?
Hi Cerasti,

@before mysql_select_db or any other function ptohibit displaying error
messages. Some times it is good because error message gives hint to hacker
how to break you system You need error message because it help you to see what is wrong


$sql="insert into nyhetsbrev (field1, email) VALUES( '', ' ".$email." ' )";

if(!mysql_query($sql))
{
echo mysql_errno() . ": ";
echo mysql_error() . "<br>";
}

and I usually use auto_increment number for id field and therefore

i would write

$sql="insert into nyhetsbrev (field1, email) VALUES( 0, ' ".$email." ' )";

echo $sql."<br>" // this line to see what your sql is

Please visit http://www.configure-all.com and select PHP from main menu

there you will find code examples that help you

Good luck
floridian is offline
Reply With Quote
View Public Profile Visit floridian's homepage!
 
Reply     « Reply to PHP-MySQL Connection Problem
 

Thread Tools

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

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




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13367 seconds with 12 queries