0

I have this form in my 'index.php'

<form id="form1" name="form1" method="post" target="foo" action="include/database.php">
    <input type="text" name="betamount">
    <input type='text' name="multipler">
    <input type="checkbox" name="hilo" value="High" checked>
  <input type="submit" name="submit" value="Bet">

In my database.php:

  1. I connect to my mysql database

  2. Fetch the post values

  3. Upload them to a database and then close the connection

THe thing is, this isn't efficient, someone can try running thousand requests per second and im sure there can be some problems

Is there any way to make it connect once and then only run the queries?

  • I've marked this as a duplicate of the general problem. It isn't really possible to address your specific issue because any mechanisms to share database connections between script invocations are going to depend on the database API you are using and you haven't told us what that is. – Quentin Jun 13 '14 at 14:50
  • MySQL?? Or are you talking of MyISAM – user3733878 Jun 13 '14 at 14:55
  • 1
    You need some way to communicate between PHP and MySQL. Typically that would be either the PDO API or the mysqli_ API. If you're several years out of date, you might still be trying to use the mysql_ API. – Quentin Jun 13 '14 at 14:58
  • mysqli , its tagged in the question :/ – user3733878 Jun 13 '14 at 15:01
  • Can you clarify, are you after sharing a connection among your applications, or restricting the number of connections that can be opened? – scragar Jun 13 '14 at 15:05
  • mysql's connection protocol is very lightweight, and doesn't have much overhead. The bigger question is why would you want to send "thousands" of separate updates instead of one large monolithic update? You'd probably find that the HTTP overhead is VASTLY higher than what tiny little bit mysql's con protocol adds. – Marc B Jun 13 '14 at 15:05
  • @MarcB Thanks for clarifying that. I was just taking an example. I used to think mysql is a bit resource hungy, I guess it isnt then – user3733878 Jun 13 '14 at 15:06
  • mysql can be VERY hungry. but it depends on what you're doing with it. a simple insert doesn't take much, but a few thousand inserts will add up quickly. – Marc B Jun 13 '14 at 15:08
  • @MarcB Hm.. But wont the multiple connect-disconnects be resource hungry? because database.php is called on every submit. – user3733878 Jun 13 '14 at 15:10
  • no. because php's connection protocol is lightweight. it has very little overhead compared to other DBs, e.g. oracle or mssql. Persistent connections can help reduce that further, but they bring in literal TON of other problems, including potential deadlocks, "trashed" connection states, etc... – Marc B Jun 13 '14 at 15:14
  • So you say it is fine that I run the code I am running right now, and like the site gets a lot of traffic + lots of form submits.. it will stay all good? – user3733878 Jun 13 '14 at 15:16
  • yes, exactly. Also, you'll do yourself huge favor if put such questions aside for a while. – Your Common Sense Jun 13 '14 at 15:19
  • Thanks for the motivation lol. @MarcB Post this as an answer maybe, I will accept it – user3733878 Jun 13 '14 at 15:24

0 Answers0