943,696 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 568
  • PHP RSS
Mar 31st, 2009
0

MySQL efficiency

Expand Post »
Hello,
I'm pretty new to PHP and MySQL. I was wondering what the most efficient way to access a large database would be. All the tutorials I've seen show something along the lines of:

$database = mysql_query("SELECT * FROM table");
But it seems to me that if 'table' was a large database then the time that it would take to copy 'table' into $database would grow fast. Wouldn't it be faster to only load a few rows if we knew their primary key?

And how do you look up a row by its primary key? What I found on Google didn't have much information with it explaining what was going on. Thanks!
Last edited by peter_budo; Mar 31st, 2009 at 3:39 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Similar Threads
Reputation Points: 104
Solved Threads: 27
Posting Whiz in Training
dmanw100 is offline Offline
239 posts
since Apr 2008
Mar 31st, 2009
0

Re: MySQL efficiency

table is the name of the table in the database. $database in your example will contain a result set of rows from the database according to the query passed into the mysql_query function. It should probably be named $result instead of $database to give you a clearer understanding of what is happening.

Try to think of a database being like a filing cabinet. The tables in the database are the files in the filing cabinet and store the actual data.

EDIT: To actually connect to the database in php, check out the mysql_connect function. Once connected you run SQL queries using the mysql_query function and interrogate the result with the mysql_fetch_* functions.
Last edited by darkagn; Mar 31st, 2009 at 1:04 am.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Mar 31st, 2009
0

Re: MySQL efficiency

Thanks darkagn. I guess a better way to phrase my question would be is it possible to only select one row at a time based on something like the primary key? Something along the lines of:

PHP Syntax (Toggle Plain Text)
  1. $result = mysql_query("SELECT primaryKey FROM table");
Reputation Points: 104
Solved Threads: 27
Posting Whiz in Training
dmanw100 is offline Offline
239 posts
since Apr 2008
Mar 31st, 2009
0

Re: MySQL efficiency

php Syntax (Toggle Plain Text)
  1. $result = mysql_query("SELECT * FROM table where id=%s, $id");
Last edited by almostbob; Mar 31st, 2009 at 7:04 pm.
Reputation Points: 562
Solved Threads: 368
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009
Mar 31st, 2009
0

Re: MySQL efficiency

your question is rather vague, if you're asking how to limit the results by a condition then almostbob asked your question.

If you meant columns by 'rows' then u asked the question yourself by:
$result = mysql_query("SELECT column FROM table");
but If you want efficiency, meaning finding something fast in a database, well, I guess very few people had trouble with this, and I think you don't have to worry about efficiency in this case.
Last edited by peter_budo; Apr 1st, 2009 at 4:39 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 8
Solved Threads: 15
Junior Poster
theighost is offline Offline
103 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Cookies and session are not working on my server for the index page
Next Thread in PHP Forum Timeline: Search Multiple Fields in Query - help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC