Jump to content

Very simple question on connection time.


TomBrown

Recommended Posts

Hello All

 

I'm a bit of an oldie trying to teach myself PHP and MySQL. “Use it or lose it the nurse says.”

 

I've created a testing environment using PHP, Apache, MySQL and phpmyadmin. I've created a database and an html form to post the data to the database.

I've retrieved data displaying it to a browser using a “While” loop. My next task is to fetch one row at a time to an html form so the data can be edited before posting it back to the database.

 

My question (and pardon the naivety) is an understanding of the connection time between the database and user.

 

From my html form the user can take as much time as is necessary to enter data. When the data is posted the PHP script makes a connection to the database, updates the database and closes the connection.

 

When I retrieve data, my script again makes a connection, retrieves all the data to a variable, displaying it to a browser and closes the connection.

 

My understanding then so far is that the actual time spent connected to the database is minimal. So, when multiple users are using a database, they are only actually connected in the moment the software is reading or writing data.

 

Am I right here or not?

 

When I try to write my script to retrieve one row at a time, should that script include making a new connection and closing it after each row has been received by the html form. Bearing in mind that the time editing the data by the user is always an unknown (and may even include a lunch break). Or should I retrieve all the database to a variable, scrolling through the html form using the variable as the source of the data.

 

Is this how databases work. Or is my understanding wrong.

 

I hope I've explained my query clearly.

 

Regard to you all from a chilly UK.

 

Tom. 

 

 

Link to comment
Share on other sites

A connection will be made only when the connection function is called.

mysql_connect()

The connection will close when the script has finished processing unless the mysql_close() function is used prior.

 

Looping through rows does not open and close connections. The same handle is used:

$handle = mysql_connect();
$results = mysql_query("SQL", $handle);
// loop rows
while($row = mysql_fetch_array($results)) {

}

Link to comment
Share on other sites

Thank you Neil for the very prompt response.

 

So would you suggest that when trying to write a script that will display each row into an html form I should,

 

1.Connect to the database

2.Retrieve all the data to a variable

3.Close the connection

4.Scroll through the data held in the variable

5.Edit the data

6.Connect to the database

7.Write to the database

8.Close the connection.

 

Is it good practice to keep the connection time to a minimum? Is this the normal procedure? When I try to imagine a database with many users, it's hard to imagine a large amount of open connections as efficient. The data manipulation is handled in the memory on the clients machine, so an open connection is not necessary.

 

Am I right and is this what I should consider when writing script? Or is my logic wrong?

 

Regards

 

Tom

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.