· 9 years ago · Nov 08, 2016, 12:14 AM
1
2echo "<table style='border: solid 1px black;'>";
3echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>";
4
5class TableRows extends RecursiveIteratorIterator {
6 function __construct($it) {
7 parent::__construct($it, self::LEAVES_ONLY);
8 }
9
10 function current() {
11 return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
12 }
13
14 function beginChildren() {
15 echo "<tr>";
16 }
17
18 function endChildren() {
19 echo "</tr>" . "\n";
20 }
21}
22
23$host="localhost";
24
25$root="root";
26$root_password="";
27
28try {
29 $conn = new PDO("mysql:host=$host", $root, $root_password);
30 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
31 $sql = "CREATE DATABASE IF NOT EXISTS TEST";
32 $conn->exec($sql);
33 echo "uspesno kreiranje na baza test <br/>";
34
35}
36catch (PDOException $er){
37 echo "neuspeshno kreiranje na baza test <br/> ".$er->getMessage();
38}
39
40
41$conn = null;
42try{
43 $conn=new PDO("mysql:host=$host;dbname=TEST",$root,$root_password);
44 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
45 $sql="CREATE TABLE IF NOT EXISTS Test2Tabela(
46 id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
47 ime VARCHAR(30) NOT NULL,
48 prezime VARCHAR(30) NOT NULL)";
49 $conn->exec($sql);
50 echo "uspesno kreiranje na Test2Tabela <br/>";
51
52}
53catch (PDOException $e){
54 echo "Neuspesno kreiranje na Test2Tabela <br/> ".$e->getMessage();
55}
56
57$conn = null;
58
59try{
60 $conn=new PDO("mysql:host=$host;dbname=TEST",$root,$root_password);
61 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
62 $sql = "INSERT INTO Test2Tabela (ime, prezime)
63 VALUES ('Filip', 'Gjorgievski')";
64 $conn->exec($sql);
65 echo "Uspesno kreiranje nov zapis<br/>";
66
67}
68catch (PDOException $e){
69 echo "Neuspesno kreiranje na nov zapis <br/> ".$e->getMessage();
70}
71
72$conn = null;
73try{
74 $conn=new PDO("mysql:host=$host;dbname=TEST",$root,$root_password);
75 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
76 $stmt = $conn->prepare("SELECT id, ime, prezime FROM Test2Tabela");
77 $stmt->execute();
78 $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
79 foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
80 echo $v;
81 }
82}
83catch (PDOException $e){
84 echo "Neuspesna selekcija from testTable <br/> ".$e->getMessage();
85}
86$conn=null;
87echo "</table>";
88?>