PDA

View Full Version : MySQL Tutorial


688206002
10-21-2004, 04:55 AM
I'm playing in a squash tourny this weekend and work load for school is increasing so I'm going to be dividing this up into 3 mini-sections and post them over the next few days. If someone feels like getting this started, please feel free to do so... I'm going to add my own stuff within a couple days.

MikeyBe
10-24-2004, 03:18 PM
I am anxiously waiting...This would be good info to have. Not quite sure what I'm doing so any help you post would be appreciated.

Mike

mck9235
10-24-2004, 05:16 PM
Yes Im sure this will help anyone who want to start using MYsql, Im going to read it becuase I have no clude how to use it, but I do no it is on my Cpanel! :lol:

688206002
10-24-2004, 06:20 PM
Okay, I'm terribly sorry I didn't get this up yet... I had a huge squash tourny this weekend and I've been putting everything off.

For now, please visit:
http://www.sqlcourse.com/

for basic things you can do with any SQL DB.

I promise I'll post some stuff by the latest tomorrow to show how you can access SQL through PHP.

Once again, sorry for the delay!

azzracing
10-30-2004, 10:31 PM
that is a very informative link. cant wait to see what you have to post for us.

asgsoft
11-02-2004, 10:00 PM
it is ver good i found it useful and informative but the teaching style was a bit chuncky

TjeerdJan
11-05-2004, 03:29 PM
Yeah thx this link u gave was really great info!

TjeerdJan
11-09-2004, 10:40 AM
mm not much of a tutorial this is.

I will make a mysql in php tutorial later this week.

Really it is easy.

TjeerdJan
11-11-2004, 08:27 AM
Ok, i assume you all know the query format, if not visit: http://www.sql.org (http://www.sql.org/)
Make sure you set a username and a password for the mysql database in ur cpanel.
Setup up a database with a table.
Now make a new .php file or open an existing one.
INSERT:


<?php
$connect = mysql_connect("localhost", "<hostmatrixinlogname>_<mysqlusername>", "<mysqlpassword>")or die("Can't connect: " . mysql_error());
mysql_select_db("<hostmatrixinloginname>_<subdomai nname>", $connect);

$query = "SELECT <coloms> FROM <tablename> WHERE <condition>";
$result = mysql_query ($query)or die("ERROR IN REQUEST: " . mysql_error());
$row = mysql_fetch_object($result);
<userdeclaredvariable> = $row-><colomname>;
echo <userdeclaredvariable>;
?>

Ok, here's the deal for every name within "<>"
They are variables that must be changed,
first a little help on those variables:
(Rember these are the variables you need to set for hostmatrix.org
on other hosting sites this will be different!)

<hostmatrixinlogname>=for example the loginname from your cpanel and ftp location
<mysqlusername>= the loginname you created for mysql in your cpanel
<mysqlpassword>=the password you set for the loginname you created in your cpanel
<subdomainname>=insert the name in: yourname.hostmatrix.org
<coloms>=the coloms you want to read in your mysql table eg. "id, value, test"
<tablename>=the name of the table you want to fetch data from, remember this table must be created first!
<conditions>= here conditions can be place eg. "id=1 AND value=3", if conditions == True, then the row that is checked will be return in the result
<userdeclaredvariable>= this is a variable where the output will be stored
<colomname>=one of the coloms you want to return (it has to be one of the coloms you put in <coloms>

Now we going to run through the code line by line:
1: <?php
The php opening tag required in the beginning of every .php file (<? can also be used)
2: $connect = mysql_connect("localhost", "<hostmatrixinlogname>_<mysqlusername>", "<mysqlpassword>")or die("Can't connect: " . mysql_error());
Connects to the database or returns an error if one of the variables is set wrong
3: mysql_select_db("<hostmatrixinloginname>_<subdomai nname>", $connect);
Selects the database you want to fetch data from
4: $query = "SELECT <coloms> FROM <tablename> WHERE <condition>"; This is the mysqlquery, here you can set what you want the server to return from the database
5: $result = mysql_query ($query)or die("ERROR IN REQUEST: " . mysql_error());
Here the mysqlquery is processed and stored in the $result variable, or it gives an error message, which ussually means your mysql query is incorrect
6: $row = mysql_fetch_object($result);
Stores the data from $result into array $row
7: <userdeclaredvariable> = $row-><colomname>;
stores a particullar field from the $row array, this line can be repeated for every colom you've requested
8: echo <userdeclaredvariable>;
Prints the data to ur output screen/browser

I know this tutorial is one big mess and ill clean it up soon and update/extend it.
I hope it helps the starters a bit to set up their mysql with php.
Feel free to pm me with questions or post them in a thread in the php help section
of this forum


NOTE: Moderators please split this thread, thx