Here’s a couple of tricks I learned early on (though not early enough perhaps) regarding databases at least:
Find a consistent naming convention for your table and column names and then stick to it.
A single table containing multiple “person” data (so make it plural).
=====
persons
=====
id
first_name
last_name
tel
.. etc
When you have a result set from mysql, lets say you picked all people with a tel no which starts with “0033”:
// use a PLURAL to describe what the set of rows actually contains
// steer clear of this result_set_12 style of anon naming...
$persons = mysql_fetch_object($sql);
// in your loop you then change the PLURAL to singular
foreach($persons as $person){
echo $person->first_name . ' ' . $person->last_name . ' Tel: ' . $person->tel;
}
Notice I also prefer to use the OBJECT notation to access the db data as it looks cleaner and is therefore easier to read, and will lead to less errors, and how you try to name your variables so that it looks a bit more like you actually saying what you are doing.
As pseudocode
for (each of the persons){
echo the persons first name, last name and tel
}
The trick is to have a clear idea of how you are going to be accessing your code. You wont get this right from the beginning. Dont be afraid to go back and change your database.
Whereas far too often we read code on here which looks like this, where we have to scan up and down the code to find out what result set this is, and if an error has been made in choosing the field name and so on.
foreach($result_set as $row){
echo $row['field1'] . ' ' . $row['field2'] . ' Tel: ' . $row['field7'] ;
}
When you have a html form which allows an admin person to say, add a new person, use the exact same field names as your database.
<input type=text name ="first_name" />
<input type=text name ="last_name" />
<input type=text name ="tel" />
Then you will have teed yourself up to treat your code like variables, so then read loops are good, and save even more typing… top marks to you for asking such an intelligent question though, I wish I’d thought of it back when.