php 3dbuzz

Intro to tables

 

use buzzdb;

create database buzzDb;

show tables;

create table buzz_test (Name varchar(15), Age int);

describe buzz_test;

drop table buzz_test;

create table guests(autoid int unsigned auto_increment primary key,first_name varchar(15), last_name varchar(15), age int, comments text, date_entered date);

describe guests

 

10 Working with data

describe guests;
insert intu guests values (NULL, “Adam”,”Mcguirrey”,46,”Love 3D Buzz!”,”1970-03-05″);
select * from guests
select first_name from guests;
select firt_name, last_name from guests;
select * from guest where first_name= “Adam”;

UPDATE guests SET last_name=”McGuire”; // fara a se specifica clauza where se va opera asupra ultimei inregistrari
UPDATE guest SET last_name=”McGuire” where autoID=1

delete from guests; // se sterg toate recordurile
delete from guests where autoID = 2; //se va sterge record-ul 2

 

 

11 User accounts

 

use mysql;
show tables;
select * from user G
insert into user (Host, User, Password, Select_priv, Insert_priv, Update_priv, Delete_priv) values (“localhost”,
“php”,Password(“FOO”),”Y”,”Y”,”Y”,”Y” ); //crearea userului odata cu setarea privilegiilor pentru userul creat
flush privileges;//aplicareea privilegiilor

exit

mysql -u php -p
enter password: foo

 

 

12 Integrating PHP with mysql

//fisierul db_config.php situat in folderul config
<?php

$db_host = “localhost”;
$db_user = “php”;
$db_password = “foo”;
$db_name = “buzzDB”;

?>

<?php

require($SERVER[DOCUMENT_ROOT].”/config/db_config.php”);
$connection = mysql_connect($db_host,$db_user, $db_password) or die(“error_connecting”);
echo “connection made”;

//daca se foloseste @mysql_connect nu mai sunt afisate toate erorile in caz ca nu se conecteaza
?>

 

13 writing a simple counter script

IN CONSOLA
use buzzDB
create table counter(num_views int unsigned);
show tables;
describe table counter;

insert into counter values (0);
select * from counter;

IN EDITOR – PHP – fisierul counter.php

<?php

require($_SERVER[“DOCUMENT_ROOT”].”/config/db_config.php”);
$connection = @mysql_connect($db_host, $db_user, $db_password) or die(“error connection”);
mysql_select_db ($db_name, $connection);

$query = “select * from counter”;
$result = mysql_query($query,$connection) or die(mysql_error());

$views = mysql_result($result,0,”num_views”);
$views++;

$query = “UPDATE counter SET num_views = $views”;
mysql_query($query, $connection) or die(mysql_error());

echo “This page has been viewed “. $views. ” times”;
?>

 

14 For loops and Array

$temp = Array (“one”,”two”, “cow”, “toe”);
for($i = 0; $i < count($temp); $i++)
{
echo “Loop #$i = $temp[$i] <br>”;
}

 

15 IF statements

<?php

$var = 0;
$var2 = False;

if($var === $var2)
{
echo “var equals var2″;

if($var2 == “8”)
{
echo ” and var2 was 8″ ;
}
else
{
echo ” and var2 was not 8 “;
}
}
elseif($var == 20)
{
echo “var was 20″;
}
else
{
echo “var was false”;
}

echo “<br> end of script”;

?>

 

 

16 POST – GET – FORMS

 
in bara de adrese a browser-ului:

http://127.0.0.1/iftest.php?test=cat

in fisierul vartest.php
<?php
$var = $_GET[“test”];

$len = strlen($var);
echo $len; //se afiseaza 3

?>

//verificare pentru no input
<?php

$var = $_GET[“test”];

$len = strlen($var);

if($len>0)
{
echo $len;
}
else
{
echo “error: no input”;
}
?>

<form action=”vartest.php” method=”GET”>
<input type=”text” name=”test”>
<input type=”submit” value=”submit”>
</form>

pentru a nu afisa in bara de adrese se foloseste ca method “POST”
iar in loc de $_GET se foloseste $_POST[“test”]

 

Urmeaza crearea unui GUEST BOOK