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.

PHP Forum


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



Freelance Jobs

Reply
php function to connect to mysql database
Old 05-06-2005, 04:11 AM php function to connect to mysql database
Skilled Talker

Posts: 79
Trades: 0
Hello

I'm trying to get a function written to make connecting to the database quicker. Currently, I currect using this sequence of commands, which I beliece is very standard:

PHP Code:
$link mysql_connect ('localhost''username''password') or die (mysql_error());
$query "SELECT * FROM payments";
$result mysql_db_query ('myaccount'$query$link) or die (mysql_error()); 
I've just started using functions to make my PHPing quicker, and have been successful so far. However, I would like to make the $link and the $result into functions, because I use them most frequently, and they are the same every time.

I'm running PHP 4.3.9 and mySQL 4.0.21.

If I define the $link function as :

PHP Code:
<?php
function fn_db_connect() 

    
mysql_connect ('localhost''username''password') or die (mysql_error());

?>
and then set:
$link = fn_db_connect();

it doesn't work, and says that $link does not exists when it tries to call it in $result.

I have seen thinks like:

PHP Code:
$conn fn_db_connect();

$result $conn->query("SELECT * FROM payments"); 
however, that didn't work. I wondered if that was only something one could do in PHP5.

My question is, does the above command work (and I just screwed it up when I tried)? How can I make $link be a variable of the function fn_db_connect() ?

Many thanks, and any suggestions to get me back on track are very welcome.

Have a nice day
hamish
__________________

Please login or register to view this content. Registration is FREE
secure and fully automated computer data backup for small and medium sized companies.


Please login or register to view this content. Registration is FREE
hamish is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-06-2005, 06:03 AM
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Hi,

If you want to use this function in such a way, then you should at least return a link identifier from the fn_db_connect(). Your function doesn't return a value, that's why $link is undefined.

Also, what's a $conn? Is it an object? Where is its declaration? I suppose that you need to read mysql_* secion of PHP manual more carefully.

As for me, I do mysql connection in a way like following:
1. Make common.php which has mysql_connect() and mysql_select_db() inside along with some other code common to all pages (e.g. definig constants and stuff)
2. require "common.php" in every script.
__________________

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 05-06-2005, 07:27 AM
Skilled Talker

Posts: 79
Trades: 0
Hey

should I just make the db connect function return "mysql_connect ('localhost', 'username', 'password')" ?

is that correct?

I haev all my functions in a seperate file, yes.

kind regards
hamish
__________________

Please login or register to view this content. Registration is FREE
secure and fully automated computer data backup for small and medium sized companies.


Please login or register to view this content. Registration is FREE
hamish is offline
Reply With Quote
View Public Profile
 
Old 05-06-2005, 08:20 AM
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Yes, absolutely. But if your script doesn't use more than one database then its unnecessary to use links. You can just mysql_connect() and mysql_use_db(), then mysql_query().

Take a look at my DB class, http://www.shareware-download.org/files/DB.class.zip I wrote it for myself and it seems very useful.
__________________

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!
 
Reply     « Reply to php function to connect to mysql database
 

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