Monday, October 25, 2010

Some notes about MySQL and PHP

Handy MySQL command is   here . and   hotscript
1)example1: type: mysql
mysql> CREATE DATABASE  test ;
mysql> show databases;
mysql> use test;
mysql> CREATE TABLE  useful  (firstname VARCHAR(20), lastname VARCHAR(20));
mysql> show tables;
mysql> INSERT INTO  useful values('Jiansen' 'lu');
mysql>  select * from  useful;
mysql> commit;    (or rollback)
2) setup user and password


kill -9 all sql, then start sql
 /usr/bin/safe_mysqld --user=mysql --skip-grant-tables &
chown -R mysql /var/lib/mysql/*
 chgrp daemon /var/lib/mysql/*
(/etc/my.cnf)
 mysql --user=root mysql
mysql> INSERT INTO user
    -> VALUES('localhost','jiansen',PASSWORD('test'),
    ->  'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');
mysql>  update user set Password=PASSWORD('test') where host='localhost' and User='root';
 mysql> select * from user;
mysql> show tables;
mysql> commit;
mysql> FLUSH PRIVILEGES;
(don't need to restart server, using flush. Note here not case sensitive)
mysql> quit
To login sql, type
 mysql --user=root mysql -p
 mysql --user=jiansen  mysql -p
  The file is in /var/lib/
3) Use PHP and mysql
use following test2.php to check  if mysql connect successfully.
<?php
   $link = mysql_connect("localhost", "root", "rootpassword")
       or die("Could not connect: " . mysql_error());
   print ("Connected successfully");
   mysql_close($link);
?>
creat a db: mydb
mysqladmin -u root create mydb -p
creat a file  mydb.dump
CREATE TABLE employees (  id tinyint(4) DEFAULT '0' NOT NULL AUTO_INCREMENT,  first varchar(20),  last varchar(20),  address varchar(255),  position varchar(50),  PRIMARY KEY (id),  UNIQUE id (id));INSERT INTO employees VALUES (1,'Bob','Smith','128 Here St, Cityname','Marketing Manager');
INSERT INTO employees VALUES (2,'John','Roberts','45 There St , Townville','Telephonist');
INSERT INTO employees VALUES (3,'Brad','Johnson','1/34 Nowhere Blvd, Snowston','Doorman');
creat a file test.php, which can access to mysql database by web
 <html>
<body>
<?php
$db = mysql_connect("localhost", "root","mypassword");
print("$db");
mysql_select_db("mydb",$db);
$result = mysql_query("SELECT * FROM employees",$db);
printf("First Name: %s<br>\n", mysql_result($result,0,"first"));
printf("Last Name: %s<br>\n", mysql_result($result,0,"last"));
printf("Address: %s<br>\n", mysql_result($result,0,"address"));
printf("Position: %s<br>\n", mysql_result($result,0,"position"));
?>
</body>
</html>
 
 
 
4) Establish music and image database, here
create database files;
use files;
CREATE TABLE files (
  id int(11) NOT NULL auto_increment,
  shortName varchar(50) default NULL,
  mimeType varchar(30) default NULL,
  mimeName varchar(50) default NULL,
  fileContents blob,
  PRIMARY KEY  (id)
) TYPE=MyISAM;
Appendix) One simple example of   PHP .
3.1)The first PHP script you will be writing is very basic. All it will do is print out all the information about PHP on your server. Type the following code into your text editor:
<?
phpinfo();
?>

 
As you can see this actually just one line of code. It is a standard PHP function called phpinfo which will tell the server to print out a standard table of information giving you information on the setup of the server.
One other thing you should notice in this example is that the line ends in a semicolon. This is very important. As with many other scripting and programming languages nearly all lines are ended with a semicolon and if you miss it out you will get an error.
3.2) print("Hello world!");
variables:  $welcome_text = "Hello and welcome to my website.";   $user_id = 987;
example:
<?
$welcome_text = "Hello and welcome to my website.";
print($welcome_text);
?>
Unfortunately, the output from your PHP programs is quite boring. Everything is just output in the browser's default font. It is very easy, though, to format your text using HTML. This is because, as PHP is a server side language, the code is executed before the page is sent to the browser. This means that only the resulting information from the script is sent, so in the example above the browser would just be sent the text:
<?
print("Hello world! <p>");
$welcome_text = "Hello and welcome to my website.";
print($welcome_text);
?>
3.3 IF statement
if ($username == "webmaster") {
echo "Please enter your password below";
} else {
echo "We are sorry but you are not a recognised user";
}
Other Comparisons
There are other ways you can use your IF statement to compare values. Firstly, you can compare two different variables to see if their values match e.g.
if ($enteredpass == $password)
You can also use the standard comparision symbols to check to see if one variable is greater than or less than another:
if ($age < "13")
Or :
if ($date > $finished)
You can also check for multiple tests in one IF statement. For instance, if you have a form and you want to check if any of the fields were left blank you could use:
if ($name == "" || $email == "" || $password == "") {
echo "Please fill in all the fields";
}
3.4 send an email,
$to = "php@gowansnet.com";
$subject = "PHP Is Great";
$body = "PHP is one of the best scripting languages around";
$headers = "From: webmaster@gowansnet.com\n";
mail($to,$subject,$body,$headers);
echo "Mail sent to $to";
Something you may have noticed from the example is that the From line ended with \n. This is acutally a very important character when sending e-mail. It is the new line character and tells PHP to take a new line in an e-mail. It is very important that this is put in after each header you add so that your e-mail will follow the international standards and will be delivered.
The \n code can also be used in the body section of the e-mail to put line breaks in but should not be used in the subject or the To field.
Setting Up Your Form
Setting up a form for use with a PHP script is exactly the same as normal in HTML. As this is a PHP tutorial I will not go into depth in how to write your form but I will show you three of the main pieces of code you must know:
<input type="text" name="thebox" value="Your Name">
Will display a text input box with Your Name written in it as default. The value section of this code is optional. The information defined by name will be the name of this text box and should be unique.
<textarea name="message">
Please write your message here.
</textarea>
Will display a large scrolling text box with the text 'Please write your message here.' as default. Again, the name is defined and should be unique.
<input type="submit" value="Submit">
This will create a submit button for your form. You can change what it says on the button by changing the button's value.
All the elements for your form must be enclosed in the <form> tags. They are used as follows:
<form action="process.php" method="post">
Form elements and formatting etc.
</form>
The form's action tells it what script to send its data to (in this case its process.php). This can also be a full URL (e.g. http://www.mysite.com/scripts/private/processors/process.php). The method tells the form how to submit its data. POST will send the data in a data stream to the script when it is requested. GET is the other option. GET will send the form data in the form of the url so it would appear after a question mark e.g. http://www.mysite.com/process.php?name=david
It really makes no difference which system you use but it is normally better to use POST if you are using passwords or sensitive information as they should not be shown in the browser's address bar.
yourpage.php?user=david
could show David's page and:
yourpage.php?user=tom
could show Tom's page, using the same script.
It is also possible to pass more than one piece of information to the script using this system by separating them with the & symbol:
yourpage.php?user=david&referrer=gowansnet&area=6
These could all be accessed separately using the GET variables user, referrer and area.
To get a variable which has been sent to a script using the POST method you use the following code:
$variablename=$_POST['variable'];
which basically takes the variable from the POST (the name of a form field) and assigns it to the variable $variablename.
Similarly, if you are using the GET method you should use the form:
$variablename=$_GET['variable'];
This should be done for each variable you wish to use from your form (or URL).
<form action="mail.php" method="post">
Your Name: <input type="text" name="name"><br>:
E-mail: <input type="text" name = "email"><br><br>
Comments<br>
<textarea name="comments"></textarea><br><br>
<input type="submit" value="Submit">
</form>
This will make a simple form where the user can enter their e-mail address, their name and their comments. You can, of course, add extra parts to this form but remember to update the script too. Now create the PHP script:
<?
$name=$_POST['name'];
$email=$_POST['email'];
$comments=$_POST['comments'];
$to="php@gowansnet.com";
$message="$name just filled in your comments form. They said:\n$comments\n\nTheir e-mail address was: $email";
if(mail($to,"Comments From Your Site",$message,"From: $email\n")) {
echo "Thanks for your comments.";
} else {
echo "There was a problem sending the mail. Please check that you filled in the form correctly.";
}
?>
Example using php to upload a file
test1.html:
 
<form action="http://hermespc4.triumf.ca/jiansen/myupload.php" method=post enctype="multipart/
form-data">
submit this file: <input type=file name="userfile" size="20">
<input type=submit>
</form>
myupload.php
<?php
$uploadDir = '/home/jiansen/html/';
$uploadDir = '/var/www/html/jiansen/';
$uploadDir = '/home/jiansen/upload/';
$uploadFile = $uploadDir . $_FILES['userfile']['name'];
print($uploadFile);
print($_FILES['userfile']['tmp_name']);
print "<pre>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadFile))
{
    print "File is valid, and was successfully uploaded. ";
    print "Here's some more debugging info:\n";
    print_r($_FILES);
}
else
{
    print "Possible file upload attack!  Here's some debugging info:\n";
    print_r($_FILES);
}
print "</pre>";
?>
In /etc/php.ini, turn global variable on.

No comments:

Post a Comment