The perfect pair: SQL & PHP…
In order for us to even think about using PHP and SQL we need to know the four main MySQL functions.
mysql_connect/strong>
1 2 3 4 | $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if(!$link){ die('Could not connect: ' . mysql_error()); } |
All you need to do here is replace ‘mysql_user’ with a username, ‘mysql_password’ with a password and assuming that it will be running on the same server then it should be enough to get started with.
Then there’s:
mysql_error
As you can see above mysql_error can be used to debug code and should be used thoroughly throughout the code.
And there’s also:
mysql_select_db
1 2 3 4 5 6 7 8 9 10 11 | $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Not connected : ' . mysql_error()); } $db_selected = mysql_select_db('people', $link); if (!$db_selected) { die ('Can\'t select the database people : ' . mysql_error()); } ?> |
Where you would replace ‘people’ with any database that that username and password combination has access to.
And finally, the golden one…
mysql_query
This is where all the actions are performed:
1 2 3 4 | $result = mysql_query('SELECT * FROM `Builders` WHERE 1'); if (!$result) { die('Invalid query: ' . mysql_error()); } |
As you can see, it is all performed by writing SQL into the query function and we will discuss this more in the near future.
