Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Using a function for database connection?
Old 06-17-2005, 05:42 PM Using a function for database connection?
feraira's Avatar
BeTheBand!

Posts: 339
Trades: 0
Hmm, I have a small problem on my plate! I am trying to code some scripts, and came up with a genius (or not so) idea for using an include with functions like a database connection instead of having to keep typing the database connection stuff, using a function instead.

Heres what I have in the functions.php (with information filled in correctly of course!)
PHP Code:
<?

// Define the database settings
$host "localhost";
$user "";
$pass "";
$db "";

function 
db_connect() {

mysql_connect("$host""$user""$pass") or die("Could not connect to $db because: ".mysql_error());
mysql_select_db("$db") or die("Unable to select DB") or die("Could not select table $db because: ".mysql_error());

};

?>
then to call the function you do "db_connect();" right? I always get this error:
Code:
Warning: mysql_connect(): Access denied for user 'xingeh'@'localhost' (using password: NO) in /home/xingeh/public_html/scripts/mass_email/functions.php on line 11
Could not connect to because: Access denied for user 'xingeh'@'localhost' (using password: NO)
Anyone got any ideas?
feraira is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-17-2005, 05:49 PM
feraira's Avatar
BeTheBand!

Posts: 339
Trades: 0
Small change, I put an @ sign infront of the "mysql_connect" and put the variables inside the function ... it got rid of the error. Now I get an error:
Code:
Could not do query because: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #3' at line 1

Last edited by feraira; 06-17-2005 at 05:51 PM..
feraira is offline
Reply With Quote
View Public Profile
 
Old 06-17-2005, 05:59 PM
feraira's Avatar
BeTheBand!

Posts: 339
Trades: 0
Ok, forget all that before, I have changed the code and sorted a few things out with it..
Note: To fix the problem I had, i had to put the variables inside the function rather than outside!

Now onto the "mail()" function... In another script I have, I use this to inform people of there account details at the site (www.xing-designs.com). I used this same code (changed to suit this script of course) and get the following message:
Code:
Email couldnt be sent because:
That's all it says... Heres the code:
PHP Code:
<?

function db_connect() {

// Define the database settings
$host "localhost";
$user "";
$pass "";
$db "";

@
mysql_connect("$host""$user""$pass") or die("Could not connect to $db because: ".mysql_error());
mysql_select_db("$db") or die("Unable to select DB") or die("Could not select table $db because: ".mysql_error());

};

function 
error_check() {

// Get the information from the form
$subj $_POST['subj'];
$author $_POST['author'];
$message $_POST['message'];

// Check it
if(strlen($subj) < 5) {
exit(
"The subject is too short!");
} else if(
strlen($author) < 2) {
exit(
"Your name is too short!");
} else if(
strlen($message) < 20) {
exit(
"That message is too short to send!");
}

};

function 
email_query() {

// Selecting the "email" row from the table
$query mysql_query("SELECT email FROM test") or die("Could not do query because: ".mysql_error());

};

function 
mail_it() {

// These are the vaiables that we defined from the form
mail($email$subj$message) or die("Email couldnt be sent because: ".mysql_error());

};

?>
Can anyone help me solve this problem?
Cheers!

Last edited by feraira; 06-17-2005 at 06:18 PM..
feraira is offline
Reply With Quote
View Public Profile
 
Old 06-18-2005, 02:42 AM
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
How does mysql error relate to sending mail? If your mail() cannot send a message it dies, but if mail_it() was called at all then there were no mysql erros, so mysql_error() returns empty string, so you see this message. The behavior of script is absolutely correct, but your logic suffers from the lack of understanding.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 06-18-2005, 08:09 AM
feraira's Avatar
BeTheBand!

Posts: 339
Trades: 0
Yeah i figured that out last night after i posted it ... didnt wana post again lol
its sorted now
feraira is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Using a function for database connection?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 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.21488 seconds with 12 queries