473,382 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Is this a good idea?

Hi!

I've been programming ASP for 5 years and am now learning PHP.
In ASP, you can use GetRows function which returns 2 by 2 array of
Recordset.
Actually, it's a recommended way in ASP when you access DB as it
disconnects the DB earlier.
Also, it's handy as you can directly access any data in the array
without looping.

As far as I know, there's no such function in PHP and I can make one.
My question is whether it's good in PHP.

pseudo-code:

$data = get_data("select * from table1");
$var = $data[3][2]; //value at 4th row, 3rd column

This way, I can wrap db connection, data retrieval, and error handling
with one function (or maybe a class).
Is the idea workable?

TIA.
Sam

Jan 17 '06
54 3573
I found this in the MySQL manual under row-level locking:
If you want to perform many INSERT and SELECT operations on a table when
concurrent inserts are not possible, you can insert rows in a temporary
table and update the real table with the records from the temporary table
once in a while. This can be done with the following code:

mysql> LOCK TABLES real_table WRITE, insert_table WRITE;
mysql> INSERT INTO real_table SELECT * FROM insert_table;
mysql> TRUNCATE TABLE insert_table;
mysql> UNLOCK TABLES;
"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:Yo********************@comcast.com...
Geoff Berrow wrote:
Message-ID: <cu********************@comcast.com> from Jerry Stuckle
contained the following:

So the system recomputes the balance. If there are sufficient funds in
the account, it processes the withdrawal.

Isn't a lock required for the very short period of time in between
recomputing the balance and processing the withdrawal?


Yes, but that's a very short time - on a mainframe you're talking
microseconds; milliseconds at the worst.

You can never eliminate all locks. The secret is to minimize the number
and length of the locks.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jan 25 '06 #51
Jim Michaels wrote:
I found this in the MySQL manual under row-level locking:
If you want to perform many INSERT and SELECT operations on a table when
concurrent inserts are not possible, you can insert rows in a temporary
table and update the real table with the records from the temporary table
once in a while. This can be done with the following code:

mysql> LOCK TABLES real_table WRITE, insert_table WRITE;
mysql> INSERT INTO real_table SELECT * FROM insert_table;
mysql> TRUNCATE TABLE insert_table;
mysql> UNLOCK TABLES;


Jim,

Yes, I've seen this and disagree to some extent with this statement.

First of all, it just moves the problem from the real table to the
insert table. Holding a lock on the insert table will still lock out
other people.

Secondly, you still need to be able to lock the real table. If you
can't do inserts on the fly, when will you be able to lock the entire
table? Are you going to stop all usage of the database every 5 minutes
(or whatever) so you can do the inserts?

And if you need current information, you need to now search two tables,
in case something has been added to the temporary table.

But inserts have never been a major cause of the problem - usually any
locks which affect an insert are short-lived (if you have appropriate
indicies, etc.).

The REAL problem comes from SELECT/UPDATE statement combination,
especially if you're waiting for user input between the SELECT and the
UPDATE (and are still holding the locks).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jan 25 '06 #52

"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:ff********************@comcast.com...
Jim Michaels wrote:
I found this in the MySQL manual under row-level locking:
If you want to perform many INSERT and SELECT operations on a table when
concurrent inserts are not possible, you can insert rows in a temporary
table and update the real table with the records from the temporary table
once in a while. This can be done with the following code:

mysql> LOCK TABLES real_table WRITE, insert_table WRITE;
mysql> INSERT INTO real_table SELECT * FROM insert_table;
mysql> TRUNCATE TABLE insert_table;
mysql> UNLOCK TABLES;

Jim,

Yes, I've seen this and disagree to some extent with this statement.

First of all, it just moves the problem from the real table to the insert
table. Holding a lock on the insert table will still lock out other
people.


I guess if you have the need to then lock them out, but if you really don't
have to avoid it then. Odds are not.

Secondly, you still need to be able to lock the real table. If you can't
do inserts on the fly, when will you be able to lock the entire table?
Are you going to stop all usage of the database every 5 minutes (or
whatever) so you can do the inserts?

And if you need current information, you need to now search two tables, in
case something has been added to the temporary table.

But inserts have never been a major cause of the problem - usually any
locks which affect an insert are short-lived (if you have appropriate
indicies, etc.).

The REAL problem comes from SELECT/UPDATE statement combination,
especially if you're waiting for user input between the SELECT and the
UPDATE (and are still holding the locks).
great. I see an example of how to lock an INSERT table, but not how to lock
rows I wanted to SELECT/UPDATE (is that possible?). How do I? (why lock the
whole table out if I don't have to?)
I mean, if I do a LOCK TABLES, how do I know MySQL is going to do a row lock
and not a table lock? It looks like from the manual, that row-locking is a
hidden thing and not a command.
from what I've seen, I have to lock tables like this:
mysql> LOCK TABLES real_table WRITE;
mysql> SELECT * FROM real_table WHERE id=5;
do stuff?
mysql> UPDATE real_table SET columnname=value WHERE id=5;
mysql> UNLOCK TABLES;

and from the manual, "When you use LOCK TABLES, you must lock all tables
that you are going to use in your queries." not nice...

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Feb 10 '06 #53
Jim Michaels wrote:
"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:ff********************@comcast.com...
Jim Michaels wrote:
I found this in the MySQL manual under row-level locking:
If you want to perform many INSERT and SELECT operations on a table when
concurrent inserts are not possible, you can insert rows in a temporary
table and update the real table with the records from the temporary table
once in a while. This can be done with the following code:

mysql> LOCK TABLES real_table WRITE, insert_table WRITE;
mysql> INSERT INTO real_table SELECT * FROM insert_table;
mysql> TRUNCATE TABLE insert_table;
mysql> UNLOCK TABLES;


Jim,

Yes, I've seen this and disagree to some extent with this statement.

First of all, it just moves the problem from the real table to the insert
table. Holding a lock on the insert table will still lock out other
people.

I guess if you have the need to then lock them out, but if you really don't
have to avoid it then. Odds are not.

Secondly, you still need to be able to lock the real table. If you can't
do inserts on the fly, when will you be able to lock the entire table?
Are you going to stop all usage of the database every 5 minutes (or
whatever) so you can do the inserts?

And if you need current information, you need to now search two tables, in
case something has been added to the temporary table.

But inserts have never been a major cause of the problem - usually any
locks which affect an insert are short-lived (if you have appropriate
indicies, etc.).

The REAL problem comes from SELECT/UPDATE statement combination,
especially if you're waiting for user input between the SELECT and the
UPDATE (and are still holding the locks).

great. I see an example of how to lock an INSERT table, but not how to lock
rows I wanted to SELECT/UPDATE (is that possible?). How do I? (why lock the
whole table out if I don't have to?)
I mean, if I do a LOCK TABLES, how do I know MySQL is going to do a row lock
and not a table lock? It looks like from the manual, that row-locking is a
hidden thing and not a command.
from what I've seen, I have to lock tables like this:
mysql> LOCK TABLES real_table WRITE;
mysql> SELECT * FROM real_table WHERE id=5;
do stuff?
mysql> UPDATE real_table SET columnname=value WHERE id=5;
mysql> UNLOCK TABLES;

and from the manual, "When you use LOCK TABLES, you must lock all tables
that you are going to use in your queries." not nice...

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================



Jim,

LOCK TABLES will lock the entire table. That's what it's supposed to
do. You don't have any direct control over row locking; the database
manager handles that for you.

The point I was making, however, is - don't hold locks for any length of
time (i.e. > 0.1 sec). It messes up concurrent access to the database.

And never, ever, hold a lock on the database where you're waiting for a
user response. What happens if that user just went to lunch?

LOCK TABLES is often used when you need to do something like back up one
or more tables while the database is active. You don't want data
changing in the middle of the backup. It's not something you generally
use in an application.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 10 '06 #54
Jim Michaels wrote:
great. I see an example of how to lock an INSERT table, but not how to lock
rows I wanted to SELECT/UPDATE (is that possible?). How do I? (why lock the
whole table out if I don't have to?)
I mean, if I do a LOCK TABLES, how do I know MySQL is going to do a row lock
and not a table lock? It looks like from the manual, that row-locking is a
hidden thing and not a command.
from what I've seen, I have to lock tables like this:
mysql> LOCK TABLES real_table WRITE;
mysql> SELECT * FROM real_table WHERE id=5;
do stuff?
mysql> UPDATE real_table SET columnname=value WHERE id=5;
mysql> UNLOCK TABLES;

and from the manual, "When you use LOCK TABLES, you must lock all tables
that you are going to use in your queries." not nice...


If you need row level locking, you need to use a database that supports
row level locking. MySQL supports this with the InnoDB tables and (from
the manual) it appears that the table locks are mutated into row-level
locks in InnoDB tables.

see http://dev.mysql.com/doc/refman/5.0/...l-locking.html

Other databases (postgres, Oracle, etc.) have more explicit row level
locking support (e.g. select for update in Oracle)

-david-

Feb 10 '06 #55

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: matty | last post by:
Go away for a few days and you miss it all... A few opinions... Programming is a craft more than an art (software engineering, not black magic) and as such, is about writing code that works,...
12
by: Generic Usenet Account | last post by:
I am going through some legacy code that has an "isNull()" method defined on certain classes. I can see that this can be a good way to eliminate certain types of crashes, by making this the first...
14
by: dreamcatcher | last post by:
I always have this idea that typedef a data type especially a structure is very convenient in coding, but my teacher insisted that I should use the full struct declaration and no further...
43
by: Sensei | last post by:
Hi! I'm thinking about a good programming style, pros and cons of some topics. Of course, this has nothing to do with indentation... Students are now java-dependent (too bad) and I need some...
150
by: tony | last post by:
If you have any PHP scripts which will not work in the current releases due to breaks in backwards compatibility then take a look at http://www.tonymarston.net/php-mysql/bc-is-everything.html and...
0
by: Charles D Hixson | last post by:
I was reading through old messages in the list and came up against an idea that I thought might be of some value: "Wouldn't it be a good idea if one could "rewind" an iterator?" Not stated in...
2
by: pigeonrandle | last post by:
Hi, My application creates a project that is structured like a tree, so i want to use a treeview to display it to the user. Would it be a good idea to create the various parts of project as...
7
by: elgiei | last post by:
Good morning at all, i have to implement a server,that every n-seconds (eg. 10sec) sends to other clients,which files and directory has been deleted or modified. i build a n-tree, for each...
5
by: mike3 | last post by:
Hi. Is this a good idea?: <begin code> /* Addition operator: += */ const BigFix &BigFix::operator+=(const BigFix &rhs) { ErrorType err; int lhs_sign = sign, rhs_sign = rhs.sign;
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.