Log in

View Full Version : PHP/MySLQ Tutorials



matthewbluewars
04-04-2008, 08:22 PM
Anyone know a good tutorial? The one at http://www.w3schools.com/php/php_mysql_intro.asp is a little too complex. :):confused:

thetestingsite
04-04-2008, 08:26 PM
http://www.php-mysql-tutorial.com is a good one.
Hope this helps.

Rockonmetal
04-04-2008, 08:35 PM
I thought that one was good...
I can give you a good tutorial on MySQL
First getting stuff from the database you need a couple main parts...


<?php
$con = mysql_connect("host","username_of_database_admin","password_for_the_corresponding_username_of_the_administrator");
if (!$con)
{
die('Could not connect: ' . mysql_error());
//A basic line that says if the variable $con isn't correct it can't connect so it gives an error and kills the functions for the rest of the page...
}

mysql_select_db("database", $con);

$result = mysql_query("SELECT * FROM table WHERE row='$user'");
/*When getting stuff using WHERE remeber these key rules:
for numbers always do
WHERE row=1
for variables always do
WHERE row='$variable'
for normal text ALWAYS do
WHERE row='text'
And it is case-sensitive
--
You also don't have to use where
*/
while($row = mysql_fetch_array($result))
{
// To get stuff from the row you will use this: $row['columnname']
//Below is an example
echo $row['time'];
echo $row['username'];
//You can put if statements in here as well...
if($row['username']=1){
echo $row['time'];
}else{
echo "Rockonmetal Rules";
}
}//That second bracket is for the while function which gets the data from the database...
?>
To put stuff in a database you should have MySQL... I had no luck with WAMP5 but you can try... It apparently works right out of the box... I'm the one exception...
General Question:
How much experience do you have with PHP and or MySQL?
if so... another tutorial I sometimes use is:
http://www.php-mysql-tutorial.com/
W3 schools is helpful and resourceful... Though the best is www.php.net (official site of php) it has all the functions ever made for php... (I wonder why? :p but its a very good site)

matthewbluewars
04-04-2008, 09:04 PM
I have MySQL on my web host. Is there a tutorial for that?

Rockonmetal
04-04-2008, 09:48 PM
I'll just explain everything since its easier to do that cuz I learned myself.
First you have to find mysql manager... You need add a database (call it what ever you want) to make sure phpmyadmin is on your hosting service...
Once you make a database head to phpmyadmin. Then click on your new database on the left frame. You should get a page in the main (right) showing you have no tables in the database. Below that message you should see a form which says make a new table. Give it a name (avoid typing spaces at all costs) and add a rough number of how many "fields" you'll have in the database. It should bring you to a form with a bunch of name things:
Heres the best way to do a simple user database:
Field 1 name "id" type "int" extras "auto-increment" set it as a primary key
Field 2 name "user" type "text" extras "none"
Field 3 name "password" type "text" extras "none"
Field 4 name "email" type "text" extras "none"
leave everything else blank or how it was if I don't specify what to do with it...
To insert stuff into the database there should be a tab on the browse, structure, export, opperations, drop (delete), and insert. (Might not be the exact order).
Hope this helps

matthewbluewars
04-05-2008, 02:28 PM
I finished doing the code. The form that submits it has four fields: username, password, title and post. Its method is POST. Please give feedback:


<html>

<head>

<title></title>

</head>

<body>
<a href="http://lightbulbproductions.net">Back</a>
<?php
$username = $_POST["username"];
$pass = $_POST["pass"];
$usernames = array("user1", "user2");
$passwords = array("pass1", "pass2");
$title = $_POST["title"];
$post = $_POST["post"];
for ($login = 0; $login < 2; $login++)
{
if ($username == $usernames[$login] && $pass == $passwords[$login])
{
sql();
break;
}

}
function sql() {
$connect = @mysql_connect("http://www.lightbulbproductions.net/php/", "admin", "password");
if (!$connect) {
die( "<P>Unable to connect to the " .
"database server at this time.</P>" );
exit();
} else {
mysql_query("INSERT INTO forum (user, title, post)
VALUES ($username, $title, $post)");

}
mysql_close($connect);
}
?>
</body>

</html>

PS. If I wanted to find the length of an array named arrayExample in JavaScript, you could find it with arrayExample.length. Is there a PHP counterpart?

Rockonmetal
04-05-2008, 02:50 PM
Looks good...
You can find the length of a string by doing this line of code in php:

<?php
echo strlen("Hello World");
//Or with variables:
$variable = "Don't forget to thank!";
echo strlen($variable);

?>
This one will tell you how many items there are in an array...

<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
$result = sizeof($people);

echo $result;
?>
I don't know if the strlen function will work with arrays... You should try it...
Looks like you picked up php really quickly.
For references to all the php functions you can go to w3 (http://www.w3schools.com/php/default.asp) || php.net download manual (http://us.php.net/download-docs.php) || here (http://us.php.net/manual/en/)

Jfis
04-07-2008, 05:41 PM
I found this PHP tutorial download to be a good and starting place if you are using dreamweaver.

http://www.sawmac.com/missing/dw8/