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/MySQL) Is is mandatory to store mysql connection information in cookies?
Old 04-11-2010, 06:24 AM (PHP/MySQL) Is is mandatory to store mysql connection information in cookies?
Novice Talker

Posts: 5
Trades: 0
Hello there. I am learning php/mysql so i am new to this.

Let's say i have s pages:
a.php
b.php

So starting with a.php, it has the information to connect to mysql database and does the actual connection with mysql. Then from a.php i am able to go to b.php using a hyperlink. In b.php i have a mysql query without the connection information but when i do the query it gives me an error.

If i do the same query on a.php i have no problem because i opened the connection in there. But when i go to b.php the connection does not retain even if i did not close the mysql connection in a.php.

-So does it mean that i have to re-open the connection for every php page i have?
-Isn't there any way around other than using cookies to store the information and retrieving them back?

Here are simple sample codes i wrote:

PHP Code:
//a.php
<?php
    
if (mysql_connect("localhost""root"""))
        echo 
"good<br/>";
    if (
mysql_select_db("test"))
        echo 
"good again<br/>"
?>
<html>
    <body>
        <a href="b.php">b.php</a>
    </body>
</html>
PHP Code:
//b.php
<?php
    
if (mysql_query("SELECT * FROM INFORMATION.SCHEMA.TABLES"))
        echo 
"good again once more";
?>
HTML Code:
//error i get when opening b.php
Warning:  mysql_query() [function.mysql-query]:  Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampplite\htdocs\template\b.php  on line 2

Warning:  mysql_query() [function.mysql-query]:  A link to the server could not be established in C:\xampplite\htdocs\template\b.php  on line 2
              c.php

Last edited by kyon9394; 04-11-2010 at 06:29 AM..
kyon9394 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-11-2010, 07:27 AM Re: (PHP/MySQL) Is is mandatory to store mysql connection information in cookies?
Novice Talker

Posts: 11
Trades: 0
Hi Kyon9394.

It looks like this is what you need: http://www.php.net/manual/en/feature...onnections.php
__________________

Please login or register to view this content. Registration is FREE
.:. Professional Hosting since 2003
2wdh.com is offline
Reply With Quote
View Public Profile Visit 2wdh.com's homepage!
 
Old 04-11-2010, 10:17 AM Re: (PHP/MySQL) Is is mandatory to store mysql connection information in cookies?
Novice Talker

Posts: 5
Trades: 0
I appreciate your help but you game me an article instead of a direct answer. The article is about persistent connections which in my code would be mysql_pconnect(). However, it also seem that persistent connections doesn't work with apache servers like mine so it is no use. Any other ideas?
kyon9394 is offline
Reply With Quote
View Public Profile
 
Old 04-11-2010, 10:43 AM Re: (PHP/MySQL) Is is mandatory to store mysql connection information in cookies?
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Thats odd that its trying the user ODBC ..

have you got the php_mysql php plugin?
__________________

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

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


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 04-11-2010, 10:51 AM Re: (PHP/MySQL) Is is mandatory to store mysql connection information in cookies?
Novice Talker

Posts: 5
Trades: 0
I assuming i do but i can't tell for sure. I am assuming that i do because the mysql php codes i have used so far work. But i can't tell for sure because i don't know if the plugin is included in XAMPP Lite.
kyon9394 is offline
Reply With Quote
View Public Profile
 
Old 04-11-2010, 12:06 PM Re: (PHP/MySQL) Is is mandatory to store mysql connection information in cookies?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
So does it mean that i have to re-open the connection for every php page i have?
Yes, it does.
PHP is a scripting language, that is run in a "fire and forget" model, like the web.

A request to apage is done, the server fires the PHP engine up, process that page, and shut the PHP processor down.
You request another page, it fires the PHP engine again, process the page, and shut it down.
Well, not really today when you use an apache module, but the principle stays the same.

Each page are distinct, and no logic is shared between them, if you don't take care of it.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 04-11-2010, 04:14 PM Re: (PHP/MySQL) Is is mandatory to store mysql connection information in cookies?
Novice Talker

Posts: 11
Trades: 0
Kyon9394,

Quote:
-So does it mean that i have to re-open the connection for every php page i have?
The short answer is: yes.
You can open the new connection with mysql_connect or use persistent connection with mysql_pconnect, but you need to use the connection function in every script.
You can put the connection code into separate php file and include it in every script for better organization (so in case of connection settings change you will need to modify just 1 file and not every single script).
__________________

Please login or register to view this content. Registration is FREE
.:. Professional Hosting since 2003
2wdh.com is offline
Reply With Quote
View Public Profile Visit 2wdh.com's homepage!
 
Old 04-11-2010, 07:00 PM Re: (PHP/MySQL) Is is mandatory to store mysql connection information in cookies?
Novice Talker

Posts: 5
Trades: 0
Thanks for your answers.

@2wdh.com
That was a nice suggestion but it looks like it won't work on the project i am dealing with. The project i am dealing with first ask the user for the MySQL host and login information to connect to any MySQL database and then process data. So i would need the login information that was entered in the first page to be used in the rest of the pages. So, my guess is that i would have to store the information in cookies.
kyon9394 is offline
Reply With Quote
View Public Profile
 
Old 04-11-2010, 07:16 PM Re: (PHP/MySQL) Is is mandatory to store mysql connection information in cookies?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
So, my guess is that i would have to store the information in cookies.
No, bad.
Store it in the session.

Cookies are browser side, session are server side.
If there is no absolute reason to do so, do not store them in a cookie.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 04-11-2010, 07:49 PM Re: (PHP/MySQL) Is is mandatory to store mysql connection information in cookies?
Novice Talker

Posts: 5
Trades: 0
Quote:
Originally Posted by tripy View Post
No, bad.
Store it in the session.

Cookies are browser side, session are server side.
If there is no absolute reason to do so, do not store them in a cookie.
That would be great. Do you know of any simple sample codes or tutorials i can take a look at?

P.S.> Googling it right away.
kyon9394 is offline
Reply With Quote
View Public Profile
 
Old 04-12-2010, 01:26 AM Re: (PHP/MySQL) Is is mandatory to store mysql connection information in cookies?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
http://www.php.net/manual/en/session.examples.basic.php
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to (PHP/MySQL) Is is mandatory to store mysql connection information in cookies?
 

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