954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

MySQL efficiency

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!

dmanw100
Posting Whiz in Training
242 posts since Apr 2008
Reputation Points: 104
Solved Threads: 27
 

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.

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

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:

$result = mysql_query("SELECT primaryKey FROM table");
dmanw100
Posting Whiz in Training
242 posts since Apr 2008
Reputation Points: 104
Solved Threads: 27
 
$result = mysql_query("SELECT * FROM table where id=%s, $id");
almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

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.

theighost
Junior Poster
103 posts since Jan 2009
Reputation Points: 8
Solved Threads: 15
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You