Welcome Guest!
Please login
If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:
- Receive Fast Advice
- Learn Programming Languages
- Get Professional Website Reviews
- Quick Troubleshooting Assistance
|
|
Connecting To Db Multiple Times & Another Question
This is a discussion on Connecting To Db Multiple Times & Another Question, within the MySQL section. This forum and the thread "Connecting To Db Multiple Times & Another Question" are both part of the Programming Your Website category.
![]() ![]() |
Nov 4 2008, 06:36 AM
Post
#1
|
|
|
Squeezing ![]() ![]() ![]() Posts: 81 Joined: 18-February 08 |
Question 1 - Connecting to db multiple times
Currently I am connecting (and disconnecting) to the database multiple times to build my page, once each for building the nav, page content, and processing a form if it's been submitted. Would I be better off just connecting to the database once at the top of the page, and then closing the connection at the bottom of the page? e.g. My page currently looks like this:
would it be better to do:
Question 2 - adding values that already exist to a unique column On my site users can add/remove tags for pages. The tags are just listed in a textbox seperated by a comma or semicolon. What I'm doing at the moment is to explode (in PHP) the tags textbox data, then add all the seperate tags posted to the tags table. Because the tags column is unique tags that are already in the table won't be added again. Is it okay to work like this or I would I be better checking whether a tag exists and then only adding it to the table if it's not already in there? Thanks Dave |
|
|
Nov 4 2008, 07:15 AM
Post
#2
|
|
![]() Squeeze Machine ![]() Posts: 508 Joined: 7-October 08 From: Australia |
In my opinion, for your first question I think your single connection / disconnection is the better option. And for your second question, I think that's the correct way to go, otherwise you're increasing your PHP running time for no real reason if the database can already handle it quite quickly.
-------------------- The more you visit, the more I'll post: http://japheththomson.com/
|
|
|
Nov 4 2008, 07:19 AM
Post
#3
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
Hey Dave,
Answering your first question: It would be better to open a database connection once at the beginning and close it at the end of your php executions. Opening and closing repeatedly slows things down and adds load to your server. If you aren't working on a monster site, the easiest way to achieve this is to store your database details in an include file, have that file create a connection and store the resource in a variable. You can then use this variable in future SQL statements. It's then just a matter of closing the connection at the end of your PHP dealings. Your second question: If you have a column that is marked as unique then you would definitely want to check to ensure you're not doubling up on values when adding tags to the database. Something like: CODE $tags = explode(',', $string); // Use the INSERT IGNORE statement which will instert but ignore any duplicate values $query = "INSERT IGNORE INTO tags (tag) VALUES ('" // Join the tags into a nice string $query .= preg_replace("/\'),$/", '', join("'),", $tags)) . ')'; mysql_query($query); Using INSERT IGNORE is a handy shortcut for not adding duplicate rows. -------------------- |
|
|
Nov 4 2008, 07:33 AM
Post
#4
|
|
![]() Squeeze Machine ![]() Posts: 508 Joined: 7-October 08 From: Australia |
Ah, that's much better explaination of question 2's answer... thanks, Rakuli!
-------------------- The more you visit, the more I'll post: http://japheththomson.com/
|
|
|
Nov 5 2008, 08:04 AM
Post
#5
|
|
|
Squeezing ![]() ![]() ![]() Posts: 81 Joined: 18-February 08 |
Thanks for your help Rakuli, with regards to the duplicate values question, the reason I exploded the tags text input was I was inserting the tags one by one, like this:
CODE $stmt = $conn->stmt_init(); $sql = 'INSERT INTO tags (Tag) VALUES(?)'; $stmt = $conn->prepare($sql); $tmp = ''; $stmt->bind_param('s', $tmp); //$tagsPosted is the tags input exploded into an array of indivdual tags foreach($tagsPosted as $tmp) { $tmp = trim($tmp); $stmt->execute(); } $stmt->close(); For some reason I didn't even think of inserting multiple values in the one statement. Anyway, you can see from the above code that since it inserts each tag one by one if a tag already exists it will just abort that insertion and carry on with the next one. Since I didn't put any error handling in this code I wasn't even aware that an error was being generated when trying to insert a tag that already exists. Anyway, I presume it's much more efficient to add the tags all in one go using your method? It would also mean I don't have to explode the tags input, just preg_replace semi-colons with commas. Dave This post has been edited by djeyewater: Nov 5 2008, 08:08 AM |
|
|
Nov 5 2008, 08:18 AM
Post
#6
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
You will probably still need to explode the tags because they are string input they need to be wrapped in quotes. You could use preg_replace to do that as well but it's probably better to explode so you can strip whitespace and not end up with empty values.
It is better to do more at once with MySQL rather than run multiple queries with PHP. -------------------- |
|
|
Nov 8 2008, 06:46 AM
Post
#7
|
|
|
Squeezing ![]() ![]() ![]() Posts: 81 Joined: 18-February 08 |
Thanks
Dave |
|
|
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
5 | shammy2007 | 408 | 17th February 2008 - 12:57 AM Last post by: Jacob |
|||
![]() |
11 | shammy2007 | 656 | 22nd February 2008 - 11:09 AM Last post by: Stuart |
|||
![]() |
34 | Linda | 1,458 | 30th March 2008 - 09:50 PM Last post by: JustinStudios |
|||
![]() |
2 | Itsumishi | 157 | 20th April 2008 - 08:02 PM Last post by: Itsumishi |
|||
![]() |
4 | unitedcraig | 131 | 18th April 2008 - 04:23 PM Last post by: unitedcraig |
|||






Nov 4 2008, 06:36 AM










