Jump to content

php mysql query from multiple clients


ksas025

Recommended Posts

I am attempting to write a php script to query some data, do some calculations, and display them to the user via an html table.  It works great most of the time but every now and then it seem that data becomes corrupt, or the calculations are not right.  Is it possible that when two users access the PHP script via different browsers the script steps on itself and data becomes corrupt?  Is there some way to ensure that when two (or more) people access the script at the same time the querys wont collide?  Is this ever an issue with PHP?

 

Just wondering if there is a php best practice to allow multiple users to use the same script to query and present data?

 

 

Thanks.

 

 

Link to comment
Share on other sites

Each request to a web server is serviced by a separate instance/child process of the web server. Each request also receives its own connection to a database server and the queries are completely separate. There is no interaction possible unless you are running up against a bug or a memory resource/corruption problem. If you were writing data to a disk file and you were only using one file name, then you could see data being overwritten.

 

You would need to provide specific information as to what the data corruption looks like v.s. what it should be.

 

It is more likely that your code contains a bug or you are not validating data or checking for errors that could occur and then continue accessing non-existent data. You would need to post your code for any one in a Forum to be able to tell you what it might be doing that could cause the symptoms you have seen.

Link to comment
Share on other sites

I am attempting to write a php script to query some data

 

Does that data come from / is written to a text file?  If so, then it is possible for two requests to attempt to access the file at the same time, which would corrupt the text file.  In database terms, this is almost exactly like a deadlock, except rather than denying access to both, it grants access to both and they corrupt the data.

Link to comment
Share on other sites

Thank you for the replies.

 

PFMaBiSmAd:

 

Is the same true for IIS?  I am more familiar with apache and how it does have individual instances for each connection (you can see them in the process list) but I guess I have never looked for this on a WIndows machine.

 

Thank you for you input.  When I get back to the office, the first thing I will do is set error checking blocks at the query and the while(odbc_fetch_object($results) loop.  You are probably right that on some queries it errors or does not retrieve all rows, in which case it might cause my problem.  And while I am on the subject let me explain what I am trying to do a little more:

 

I have a near real-time database that has Server metrics; cpu utilization, disk space, network utilization, etc.  I am attempting to write a php script to query 200 rows (200 servers) with each column have different info: server name, IP, location, description, etc.  My query gets all the current values and I attempt to do the following on the data:

 

  1. Seperate each column into a differnt array with the server name being the key in each array.  Now I have all the columns retrievable by servername.  Please let me know if there is a better way to do this! :)

 

  2. I then attempt to sort a metric for the given table, cpu utilization for example using the arsort($cpu_array) command.

 

  3. I then loop through the sorted $cpu_array and list the data in rows in an html table, calling the remainder of the columns for the server by using the $key of the current array. (all arrays use the same key).  So echo "<tr><td>."$server_name[$key]."....etc."

 

  4. The result is a table which lists the most heavily loaded servers on top, near-realtime using auto-refresh.

 

I guess what I have been seeing is the retrieval of the different columns using my "same key" technique is not working and I get CPU utilizations matched to the data.

 

I took the time to say all this because there probably a much better way to do this but I am not a very experienced programmer and sometimes wounder if there are widely known practices to do what I am trying to do.

 

In any case, thank you very much and like I said, I will try some more error checking and verifying data when I get back to the office.

 

 

Link to comment
Share on other sites

Why are you storing the data in an array? Retrieving data from the database is already in the form of an array, with keys. Therefore you could make the request, loop through the results and return the output directly. Unless you're trying to manipulate hierarchical data in which case multi-dimensional arrays would make sense.

 

To elaborate, if I had 2 database fields with 2 records in each:

 

ServerName     ServerDescription

myserver1        server1 description
myserver2        server2 descrption

 

 

 

The results would already be in the form of an array i.e.

$row = array("myserver1" => "server1 description", "myserver2" => "server2 description");

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.