php - Determine if two MySQL records exist before detemining their relationship -


imagine have table of persons, busses, , mapping table in between. want determine, given set of people, on given bus. create php page return simple list of persons. want page robust, want return error reflected db or other failures. also, pdo used make site flexible.

to reduce database queries (assuming querying db expensive part, if db big and/or lots of visitors), want reduce queries db absolute minimum.

a simple script would, after validating data types , other unintended values, such on db side:

//see if bus exists select id busses id={$bus_id};  //see if given persons exist select id persons id in ({$comma-seperated-person-ids});  //finally check mappings table , discover on bus. select person_id busses_persons bus_id={$bus_id} person_id in ({$comma-seperated-ids}); 

i think order important, because suppose bus did not exist, page return success , show empty list if first query did not execute determine existing busses. return successful page no persons, if none of persons existed , hadn't performed second query determine of persons set exists, , don't. strictly, should return error page if any provided data invalid or non-existant.

how can list of persons on bus, given set of persons, , given bus, and find out if given person or bus or not exist, , in single query?

i suppose can hack (even though maintain db abstraction pdo, can't go far either), or becomes question of db execution time. wondering how other developers made scripts faster , more efficient when stumbling upon problem here?


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -