Getting down and dirty with PHP/MySQL - 08/13/05 03:21 PM
Okay this is a easy MySQL lesson.
=================================================
Reference for this tutorial.
http://us2.php.net/manual/en/ref.mysql.php
http://us2.php.net/manual/en/function.mysql-query.php
http://us2.php.net/manual/en/function.mysql-fetch-array.php
http://us2.php.net/manual/en/function.mysql-close.php
http://us2.php.net/manual/en/function.mysql-connect.php
-----------------------------------------------
This is a very basic MySQL command in PHP
lets break that apart shall we?
----------------------------------------------
You will see first "$dg" This is a variable obviously. But why do we need it? Well you could very easily do
But "$dg" will tell us if out query worked. $dg is a boolean variable. That is it is either true or false. It is set once the query is run. You could, and I do the following.
The above code will run the script just fine unless the query fails. Think of it like this...
This is just like saying...
This is why we have $dg in the code. Lets look again at what we have now.
The next thing you see is "= mysql_query();"
This tells PHP we are about to do a mysql_query. It is simply a function in PHP. just like date(U); or php_info(); the "(" and ")" encase the actual MySQL command to execute in MySQL. If you were dialed into the server all you would need to do is what is inside the "(" and ")" So if you follow me so far...
the above is all PHP. It all the code you see in the code block above is simply to let PHP know it is doing a MySQL query. Now lets look at the MySQL part of the code.
What you see above, basically in human terms says this:
Select everything that is in table one. Think of the * as a wildcard. Same as in MS DOS. You could very easily do
or
which if you only need a few columns then you should only select those as it will be faster to do so, plus use less server resources than selecting everything.
So let�s look at it all again.
This is lesson one is MySQL, I will post more in the next 24 hrs It is 04:29 here and I am tired. I will probably edit this as I forgot to explain and show how to login to a database.
Oh I forgot. The $dg with the mysql_error(); is most handy in debugging and testing your code. Other things you should know.
Lets say you want to "delete" a row in your database.
You start with something like this
hey that looks kind of like...
Lets say you want to "update" a bit of data. You would start like this
Hmmm I see a pattern here...
If you want to "insert" data you would begin with this code
Did you notice they all start the same? That is because these things are all queries. All you really have to learn once you get the PHP syntax down is the MySQL code. And it is easy.
Delete http://dev.mysql.com/doc/mysql/en/delete.html
Update http://dev.mysql.com/doc/mysql/en/update.html
Insert http://dev.mysql.com/doc/mysql/en/insert.html
Select http://dev.mysql.com/doc/mysql/en/select.html
With the 4 commands above you can do al hell of a lot. I mean a [censored] load of dynamic content is only limited by your imagination. Next logging in with php. Login out with PHP, how the syntax in the links I just listed for "delete, update, insert, select" work in PHP. Keep reading and do not post comments till I am done the tutorial. I will just delete them when I come back.
=================================================
Reference for this tutorial.
http:/
http:/
http:/
http:/
http:/
-----------------------------------------------
This is a very basic MySQL command in PHP
Code
$dg = mysql_query("SELECT * FROM table1");
----------------------------------------------
You will see first "$dg" This is a variable obviously. But why do we need it? Well you could very easily do
Code
mysql_query("SELECT * FROM table1");
Code
$dg = mysql_query("SELECT * FROM table1"); if($dg){ // execute code }else{ echo "echo out why it failed ".mysql_error(); exit(); }
Code
if($dg){ }
Code
if($dg == "true"){ do this code }else{ do this }
Code
$dg = mysql_query("SELECT * FROM table1");
This tells PHP we are about to do a mysql_query. It is simply a function in PHP. just like date(U); or php_info(); the "(" and ")" encase the actual MySQL command to execute in MySQL. If you were dialed into the server all you would need to do is what is inside the "(" and ")" So if you follow me so far...
Code
$dg = mysql_query("");
Code
SELECT * FROM table1
Select everything that is in table one. Think of the * as a wildcard. Same as in MS DOS. You could very easily do
Code
SELECT colum1 FROM table1
Code
SELECT colum1, colum2, colum3 FROM table1
So let�s look at it all again.
Code
$dg = mysql_query("SELECT * FROM table1"); //select everything from table1 if($dg){ // if the query worked do everything up to the else section // execute code }else{ //if the query failed, tell me why. echo "echo out why it failed ".mysql_error(); //mysql_error(); is a PHP function that will tell you what the exact error is in your query. exit(); //stop executing code here, just stop every [censored] thing right now!!!! }
Oh I forgot. The $dg with the mysql_error(); is most handy in debugging and testing your code. Other things you should know.
Lets say you want to "delete" a row in your database.
You start with something like this
Code
$dg = mysql_query("");
Lets say you want to "update" a bit of data. You would start like this
Code
$dg = mysql_query("");
If you want to "insert" data you would begin with this code
Code
$dg = mysql_query("");
Delete http:/
Update http:/
Insert http:/
Select http:/
With the 4 commands above you can do al hell of a lot. I mean a [censored] load of dynamic content is only limited by your imagination. Next logging in with php. Login out with PHP, how the syntax in the links I just listed for "delete, update, insert, select" work in PHP. Keep reading and do not post comments till I am done the tutorial. I will just delete them when I come back.