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!

8 Years
Discussion Span
Last Post by theighost

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.


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");

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.