I have a fan of this thread now don't I hitty :P
thought I might make a few mysql tutorials
---
1.connect to mysql server
here we go..
| PHP Code: |
<?php mysql_connect("mysql_server", "mysql_username", "mysql_password") or die("<p>mysql_error: ".mysql_error()."</p>"); //replace mysql_server with your server, mysql_username with your mysql username, and mysql_password with your mysql password...if it fails, it will say error: then state the mysql error. //select a database mysql_select_db("mysql_database"); //just replace mysql_database with your database name. ?> |
wow, you just connected to a database :P
2.select data from a database and display it
| PHP Code: |
<?php //connect to server mysql_connect("mysql_server", "mysql_username", "mysql_password") or die ("<p>Mysql_Errro: ".mysql_error()."</p>"); //select database mysql_select_db("mysql_database") or die("<p>Failed to select database, error: ".mysql_error()."</p>"); //set sql query, replacing `table` with your `table`(must have the ``) $sql="SELECT * FROM `table`"; //run sql query $s=mysql_query($sql) or die("<p>Error selecting data from db: ".mysql_error()."</p>"); //put data into an array $row=mysql_fetch_assoc($s); //display data, data 1 and data 2 must be in your table, and must have something inserted(showing you how to insert later...) echo "<p>Data1: $row[data1]</p><p>data2: $row[data2]"; //thats about it for that :P ?> |
wow, you can connect and display data from a database now :P
3.insert data into a database
| PHP Code: |
<?php //connect to DB mysql_connect("server", "user", "pass") or die("<p>".mysql_error()."</p>"); //select a DB mysql_select_db("database") or die("<p>".mysql_error()."</p>"); //set sql query $sql="INSERT INTO `table` (`data1`, `data2`) VALUES (`text1`, `text2`)"; //run query mysql_query($sql) or die("<p>".mysql_error()."</p>"); echo "Insert text1, and text2 into data1, and data2 successfully."; ?> |
and, you can insert data aswell now :P
plus my neck herts aswell, so I hope someone enjoys this...
4.display certin data
| PHP Code: |
<?php //connect to server mysql_connect("server", "user", "pass") or die("<p>".mysql_error()."</p>"); //select db mysql_select_db("database") or die("<p>".mysql_error()."</p>"); //select sql $sql="SELECT * FROM `table` WHERE `type`='data'"; //run query $s=mysql_query($sql) or die("<p>".mysql_error()."</p>"); //display every thing that has a field called `type` that is set to 'data' while($row=mysql_fetch_assoc($s)) { echo "<p>$row[data1]</p><p>$row[data2]</p>"; } ?> |
wow, my shoulders are starting to hert now, so you better like it

1 more..
5.checking if data exists
| PHP Code: |
<?php //connect mysql_connect("server", "user", "pass") or die("<p>".mysql_error()."</p>"); //db selection mysql_select_db("database") or die("<p>".mysql_error()."</p>"); //sql query data $sql="SELECT * FROM `table` WHERE `data1`='text1'"; //sql query $s=mysql_query($sql) or die("<p>".mysql_error()."</p>"); //see if any exist $count=mysql_num_rows($s); if($count !=0) { echo "<p>$row[data1]</p>"; } else { echo "<p>Sorry, the data doesnt exist.</p>"; } ?> |
ok, done, now, i wasn't sitting strait...cuz I am an idiot(lol) so I have sore shoulders and a sore neck..and when i started I had a headache, and I still do...please, oh, PLEAE enjoy it!
p.s I sat here, and wrote this all, I did not copy and paste, and took no brakes. you just wait till I write a php user system tutorial...you will like :P(includes profile editing, comments, signitures for comments, admins being able to edit and delete users.....big big big...lol, may be a while before thats ready though)
enjoy anyways, for now, cya later!