Results 1 to 12 of 12

 

Thread: PHP / mysql help

  1. #1
    Stuart

    Status
    Offline
    Join Date
    Nov 2003
    Location
    Ely
    Posts
    1,346
    Thanks
    5
    Thanked 15 Times in 15 Posts


    Hi,

    Please could anyone offer any help. I keep getting this error message on my site pricefilterdotcodotuk
    ----------------------------------
    Warning: mysql_connect() [function.mysql-connect]: Too many connections in /home/reedsd/public_html/index.php on line 109
    Unable to Connect to Database
    ----------------------------------
    There are conversations happening about the above issue, is it coding or a hosting issue?

    Seperatly: Prior to the above error the pricing results coming back in the XML feed witin the shopping section, sometimes displayed and sometimes did not, if we did a forced refresh when they were not displayed then after a few attempts the results appeared.

    The feed provider said knowother partners are having problems, again we cant figure out if the 2nd issue is hosting related, cosding or wheer the results are pullled from the feed provider (Pricerunner)

    Anyone help please - thanks

    Stuart

  2. #2
    Registered User

    Status
    Offline
    Join Date
    Aug 2005
    Location
    Bristol - UK
    Posts
    633
    Thanks
    31
    Thanked 5 Times in 4 Posts
    I'm not sure if this will help but you could try using the mysql_pconnect() function instead of mysql_connect(), it will setup a persistent connection so if another user tries to use the script, the script will look for an existing already open connection.

    Quote Originally Posted by php.net
    when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.
    More details can be found here: PHP: mysql_pconnect - Manual

  3. #3
    Registered User

    Status
    Offline
    Join Date
    May 2008
    Posts
    797
    Thanks
    16
    Thanked 19 Times in 16 Posts
    here is a quick connection script

    <?php
    $dbu = 'USERNAME';
    $dbp = 'PASSWORD';
    $dbh = 'HOSTNAME NORMALLY LOCALHOST';
    $dbn = 'DATABASE NAME';
    $dbc = mysql_pconnect($dbh, $dbu, $dbp);
    mysql_select_db($dbn, $dbc);
    $query = "SELECT * FROM TABLENAME";
    $doquery = mysql_query($query, $dbc);
    $result = mysql_fetch_assoc($doquery);
    ?>

    hope that helps

    thats just a connection scriopt and a basic query
    High Quality Bed Frames From BedFrames.co.uk
    bed frames

  4. #4
    Stuart

    Status
    Offline
    Join Date
    Nov 2003
    Location
    Ely
    Posts
    1,346
    Thanks
    5
    Thanked 15 Times in 15 Posts
    Thanks affiliatemarketingltd and shyguy0507. I'll let you know if this solves the problem

    Br
    Stuart

  5. #5
    tbp
    Registered User

    Status
    Offline
    Join Date
    Dec 2006
    Posts
    1,998
    Thanks
    0
    Thanked 22 Times in 22 Posts
    The error is exactly as it says, too many open MySQL connections.

    There are 2 reasons why this happens:

    1) You have some rogue code somewhere thats opening MySQL connections and not closing them again afterwards, meaning they build up.

    2) You're site has become so busy that its opening more connections than have been allowed in the my.cnf (MySQL configuration file).

    The solution to 1 is to do a global search looking for code that opens MySQL connections, and then make sure that each one is closed again later in the script.

    The solution for 2 is to get the host to edit their my.cnf file and allow more connections to be opened.

  6. #6
    Stuart

    Status
    Offline
    Join Date
    Nov 2003
    Location
    Ely
    Posts
    1,346
    Thanks
    5
    Thanked 15 Times in 15 Posts
    tbp

    I have asked my developer to take a look at the options

    Thanks a million

    Br
    Stuart

  7. #7
    Stuart

    Status
    Offline
    Join Date
    Nov 2003
    Location
    Ely
    Posts
    1,346
    Thanks
    5
    Thanked 15 Times in 15 Posts
    Hi,

    I have spoken with my host company and they say this issue

    "Warning: mysql_connect() [function.mysql-connect]: Too many connections in /home/reedsd/public_html/scripts/functions.php on line 40
    Unable to Connect to Database!"


    Is related to using "SELECT *" is bad and should not be used, select on the data needed.
    also RAND() is very bad and should be avoided at ALL costs.
    You have a totally unindexed database table.

    This causes errors as follows

    | 3541 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 1024 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    '1002827' AND approved > '' ORDER BY RAND() LIMIT 1 |
    | 3542 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 1024 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    '991664' AND approved > '' |
    | 3602 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 975 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    '1131680' AND approved > '' ORDER BY RAND() LIMIT 1 |
    | 3609 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 969 | Opening tables | SELECT * FROM tblshopping_details WHERE
    pr_category_id = '351' |
    | 3636 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 956 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    '387478' AND approved > '' |
    | 3639 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 954 | Opening tables | SELECT * FROM tblshopping_details WHERE
    category = 'web-cameras/8' |
    | 3662 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 946 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    '1022763' AND approved > '' |
    | 3712 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 919 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    '994267' AND approved > '' |
    | 3740 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 909 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    '1003060' AND approved > '' |
    | 3770 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 889 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    '220893' AND approved > '' ORDER BY RAND() LIMIT 1 |
    | 3838 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 845 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    '921481' AND approved > '' |
    | 3857 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 831 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    '537969' AND approved > '' |
    | 3878 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 823 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    '740019' AND approved > '' |
    | 3912 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 805 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    '647661' AND approved > '' |
    | 3914 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 804 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    '764665' AND approved > '' |
    | 3925 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 795 | Opening tables | SELECT * FROM tblshopping_details WHERE
    category = 'womens-clothes' |
    | 3939 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 783 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    'F242315434' AND approved > '' |
    | 4046 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 766 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    'F488316259' AND approved > '' |
    | 4241 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 718 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    '757082' AND approved > '' |
    | 4250 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 715 | Opening tables | SELECT * FROM tblshopping_details WHERE
    category = 'hand-blenders/10' |
    | 4357 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 689 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    '537689' AND approved > '' |
    | 4365 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 682 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    'F488336829' AND approved > '' |
    | 4398 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 670 | Opening tables | SELECT * FROM tblshopping_details WHERE
    category = 'fax-machines/7' |
    | 4399 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 670 | Opening tables | SELECT * FROM tblarticles WHERE category =
    'barbeques' AND title = 'Chicken and Mango Skewer' |
    | 4404 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 666 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    '522376' AND approved > '' |
    | 4457 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 649 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    '703064' AND approved > '' |
    | 4558 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 617 | Opening tables | SELECT * FROM tblshopping_details WHERE
    category = 'baby-transport' |
    | 4720 | reedsd_pfilter | localhost | reedsd_pfilter |
    Query | 570 | Opening tables | SELECT * FROM user_reviews WHERE product_id =
    '55624' AND approved > '' |


    Please can anyone offer any advice, they are going to suspend my account if we cant sort it

    Br
    Stuart

  8. #8
    D-Mac's Avatar
    Registered User

    Status
    Offline
    Join Date
    Mar 2004
    Location
    Surrey
    Posts
    1,353
    Thanks
    29
    Thanked 49 Times in 44 Posts
    SELECT * FROM user_reviews WHERE product_id =
    '1002827' AND approved > '' ORDER BY RAND() LIMIT 1
    Do you have multiple products with the same product_id ? E.g. multiple retailers selling the same product.If so, presumably you're using RAND() to select a random single occurrence of this product?
    Take a look here: MySQL Select Random Row Fast
    There are also other ways that you could select a random row, but before going into detail it's best to clarify the actual requirement of the scripts.
    David Macfarlane
    Cost effective web development. Codewise

  9. #9
    tbp
    Registered User

    Status
    Offline
    Join Date
    Dec 2006
    Posts
    1,998
    Thanks
    0
    Thanked 22 Times in 22 Posts
    Basically, what they are saying is that it's the speed of the SQL queries that's too slow, and consequently it leads to a large number of connections open at once.

    I do get the feeling that its not the whole problem, and its excuse to pass the buck back onto you, but you should do everything you can to make the queries as quick as possible to give your site the best performance.

    Ordering using RAND() is ok for a small number of records, say up to 500, but with a larger dataset its very slow ,as a random number has to be generated for each record, and then these random numbers placed in ascending order.

    It's better to use your PHP code to pick a random record id, based on the number of records in the table, and then to call up that record. It does use more SQL queries, but even so it's still faster than the single one containing RAND().

    They are correct in saying that you should avoid using SELECT * where possible, and just select the fields that you actually want to use.

    The indexes are also very important, and can speed things up by a huge amount. I`ve seen an SQL query that previously took 5 - 10 minutes brought down to a few seconds, simply by adding an index. If you use phpMyAdmin it makes it very easy to add indexes, basically you need to make sure that there is an index on each of the major fields you search on. So if you search a dataset by record id, name and email address, then you would make sure these fields are indexed (although record id should be the primary key and as such is already indexed). Don't add an index to everything, as then things start slowing down again, just add them to fields which are regularly searched on.

    How much this is going to affect your site I don't know, as it depends on the size of your data. If you have 1000 records its not going to make a noticeable difference, if you have 100,000 records it will.

    The above should be implemented anyway, as its the best practice for working with databases, so get your developers to do the above and then see what happens from there. It may be that your host should still raise their connection limit, but when you've done the above you will be in a better bargaining position.

    If the developers haven't ventured into this area before, there are huge numbers of tutorials available on the net on indexes and MySQL optimisation which will guide them.

  10. #10
    Stuart

    Status
    Offline
    Join Date
    Nov 2003
    Location
    Ely
    Posts
    1,346
    Thanks
    5
    Thanked 15 Times in 15 Posts
    Superb, thanks tbp and D-mac...

    We take XML feeds from pricerunner so its 1000's or products across multiple categories (pricefilter dot co dot uk) don't know the exact number.

    I'll pass this post to my developer.

    I was shocked receiving the email from my host, as the strange thing is the sites been liver for nearly 2 years and this only just started happening, although traffic is up slightly (well was before these errors).

    The host said they won't raise their connection limit

    Hopefully we can get it sorted ASAP

    Br
    Stuart

  11. #11
    Registered User

    Status
    Offline
    Join Date
    May 2008
    Posts
    797
    Thanks
    16
    Thanked 19 Times in 16 Posts
    Quote Originally Posted by Stuart View Post
    The host said they won't raise their connection limit
    To be hojnest stuart your host sounds like an arsehole who is running a slow ass server and wants to try and blame the clients for any problems
    agree about teh select * comment .. although i had no idea aboutthe size etc when i posted

    hope you get this sorted as my host threw a hissy fit once .. although my script did lock up their server lol
    High Quality Bed Frames From BedFrames.co.uk
    bed frames

  12. #12
    Stuart

    Status
    Offline
    Join Date
    Nov 2003
    Location
    Ely
    Posts
    1,346
    Thanks
    5
    Thanked 15 Times in 15 Posts
    A public thank you to everyone that replied and gave advice as to how we could solve our problem.

    Without wanting to offend anyone - speacial thanks to "tbp" for the detailed response, crossing fingers we have solved the issue, only time will tell...

    Br
    Stuart



Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Dependent Drop Down in PHP Using MYSQL data
    By mike_01 in forum Programming
    Replies: 1
    Last Post: 22-05-08, 09:00 AM
  2. php populate drop down list from MySQL
    By accelerator in forum Programming
    Replies: 1
    Last Post: 07-04-08, 04:47 PM
  3. Best PHP & MySQL
    By Stellar in forum Programming
    Replies: 13
    Last Post: 28-07-06, 04:27 PM
  4. Anyone anygood with xml, php, mysql and Affiliate Window
    By AnnonnyMouse in forum Affiliate Marketing Lounge
    Replies: 1
    Last Post: 23-09-04, 11:00 AM
  5. PHP & MYSQL
    By Affiliates4u in forum Programming
    Replies: 8
    Last Post: 13-01-03, 08:12 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
To Top

Content Relevant URLs by vBSEO 3.5.0 RC2