Welcome Guest!

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.

 
Reply to this topicStart new topic
> Connecting To Db Multiple Times & Another Question, about adding values that already exist to a unique column
djeyewater
post 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:
  1. If a form has been submitted connect to the database
  2. Post data
  3. Close connection
  4. PHP stuff
  5. Connect to the database
  6. Get nav data
  7. Close connection
  8. PHP stuff
  9. Connect to the database
  10. Get content data
  11. Close the connection
  12. PHP stuff

would it be better to do:
  1. Connect to the database
  2. If a form has been submitted Post data
  3. PHP stuff
  4. Get nav data
  5. PHP stuff
  6. Get content data
  7. Close the connection
  8. PHP stuff

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
Go to the top of the page
 
+Quote Post
japh
post Nov 4 2008, 07:15 AM
Post #2


Squeeze Machine
Group Icon

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/
Go to the top of the page
 
+Quote Post
Rakuli
post Nov 4 2008, 07:19 AM
Post #3


Squeeze Machine
Group Icon

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.


--------------------
Luke Dingle . com

Turn Over a Playful Leaf on Web Design -- read about the javascript cat
Go to the top of the page
 
+Quote Post
japh
post Nov 4 2008, 07:33 AM
Post #4


Squeeze Machine
Group Icon

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/
Go to the top of the page
 
+Quote Post
djeyewater
post 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
Go to the top of the page
 
+Quote Post
Rakuli
post Nov 5 2008, 08:18 AM
Post #6


Squeeze Machine
Group Icon

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.


--------------------
Luke Dingle . com

Turn Over a Playful Leaf on Web Design -- read about the javascript cat
Go to the top of the page
 
+Quote Post
djeyewater
post Nov 8 2008, 06:46 AM
Post #7


Squeezing
***

Posts: 81
Joined: 18-February 08


Thanks

Dave
Go to the top of the page
 
+Quote Post
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!
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   5 shammy2007 408 17th February 2008 - 12:57 AM
Last post by: Jacob
No New Posts   11 shammy2007 656 22nd February 2008 - 11:09 AM
Last post by: Stuart
No new   34 Linda 1,458 30th March 2008 - 09:50 PM
Last post by: JustinStudios
No New Posts 2 Itsumishi 157 20th April 2008 - 08:02 PM
Last post by: Itsumishi
No New Posts   4 unitedcraig 131 18th April 2008 - 04:23 PM
Last post by: unitedcraig