Solved

Technical Question About mysql_connect

Posted on 2002-04-11
7
164 Views
Last Modified: 2006-11-17
Hi There Everybody,

i have a simple question, i hope you will understand my needs to accomplish this the better way possible .

i have all my pages calling a page called functions.inc, where i have two functions like this..

StartTheDatabase();  // Opens The Database
CloseTheDatabase;    // Closes It

Well, what is the better way to have everything done, having the least ports to the mysql server open ??

i mean, shall i StartTheDatabase() and CloseTheDatabase() Everytime I Need It, Or Shall i Start it once in the start of the page and close it at the end of it ??

i am using mysql_connect in the StartTheDatabase function, maybe changing to pconnect is better ?


the matter is that index.php includes 5 different pages, footer header and some more, and each of them accesses the database on its own way, when i load index.php in the browser, i get about 28 queries to the database to generate the page, but i need everything to be optimized to get 1 or 2 connection at maximum opened for each page load

i hope you understood my point ..

regards,
<Ruslan>
0
Comment
Question by:neostudio
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 32

Expert Comment

by:Batalf
ID: 6934142
You shouldn't have to open more than one connection if the user you use to connect to the database have the desired access.

Why not use a include-file at the top of the file for the connection:

include("connect.inc");

And put the mysql_connect() in the connect.inc file. Then this db-connection would be available to you during the entire script, no matter how many queries you have.

You don't have to close the connection either. It's closes it self automatically after the scripts ends.

Batalf
0
 
LVL 3

Author Comment

by:neostudio
ID: 6934234
mmmm....

lets say i have
include("connect.inc");
in all my pages, the user enters from index.php,the file is included and the database is open, that is ok !

then he navigates to another page, include("connect.inc"); is included again, will this open a new connection ?
won't this kill the server ?
can you please explain m,ore ?



BTW)
the main server is a Windows 2000 ADV. Server, when i do a
netstat -n
i get some 100 open ports to the MySQL server .

on the MySQL server machine, A RH7 Linux, i do some netstat and i get 2 or three open ports, what am i missing ??
0
 
LVL 3

Author Comment

by:neostudio
ID: 6934244
Also..

here is the MySQL> status output


Threads: 1  Questions: 10149  Slow queries: 1  Opens: 311  Flush tables: 1  Open tables: 25 Queries per second avg: 12.256

what are these   Open tables: 25  !!

0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 32

Accepted Solution

by:
Batalf earned 50 total points
ID: 6934371
If you use mysql_connect() and not mysql_pconnect() you won't kill the server.

From the mysql-manual:
"The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling mysql_close(). "

= When the user navigates to a new page, the old connection would be closed and a new one opened.

Batalf
0
 
LVL 3

Author Comment

by:neostudio
ID: 6934489
Ok Batalf ..
consider the thread yours, great information you provided here..

but what about these open ports to the server whn i do a netstat -n ??

aren't these open connections to the server ??

0
 
LVL 32

Expert Comment

by:Batalf
ID: 6934510
I'm Sorry, but I'm not that familiar with Windows 2000 server.

What I could tell you is that I have used mysql_connect() in each script for several years without any problem.

Is the number of open ports increasing for each time you load a web-page?

Batalf
0
 
LVL 3

Author Comment

by:neostudio
ID: 6934636
Great Answer, Thanx !!!

BTW ))
on the unix machine, the post keeps open for a second or less, but on the windows machine it is open for at least 10 minutes, strange !!
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question