Lihtsaid php ja mysql näiteid
Allpool on näited kasutajana st2014 ja andmebaasiga st2014. Lisainfot tasub vaadata w3schoolsi php tutorialist, aga siintoodud näidetest peaks neljanda praksi jaoks juba piisama.
Hello world php-s: http://dijkstra.cs.ttu.ee/~tammet/k1.php
Algus.<p> <?php echo '<p>Hello World</p>'; ?> <p>Lopp.
Nii saab uurida PHP settungeid, praksis see otseselt oluline ei ole: http://dijkstra.cs.ttu.ee/~tammet/k2.php
Algus.<p> <?php echo '<p>calling phpinfo():</p>'; phpinfo(); ?> <p>Lopp.
Esimene andmebaasipäring: http://dijkstra.cs.ttu.ee/~tammet/k3.php
Algus.<p> <?php $con = mysqli_connect("localhost","st2014","progress","st2014"); if (!$con) { die('Could not connect: ' . mysqli_connect_error()); } $result = mysqli_query($con,"SELECT * FROM tt_users"); while($row = mysqli_fetch_array($result)) { echo $row['username'] . " " . $row['name'] ; echo "<br />"; } mysqli_close($con) ?> <p>Lopp.
Kui $_REQUEST väljadel ei ole väärtust, siis võib mõni PHP versioon anda rämeda warningu (ja mõni teine jälle ei pruugi): http://dijkstra.cs.ttu.ee/~tammet/k4.php
<body> Algus. <p> <?php echo "Hi, I'm a <b>PHP</b> script with input a: "; echo $_REQUEST['a']; echo " and input b: "; echo $_REQUEST['b']; ?> <form method="post"> a <input type="text" name="a" autocomplete="off"><br> b <input type="text" name="b" autocomplete="off"><br> <input type="submit"> </form> <p> Lopp. </body>
Warningu vältimiseks on mõistlik kasutada isset kontrollifunktsiooni: http://dijkstra.cs.ttu.ee/~tammet/k5.php
<body> Algus. <p> <?php if (isset($_REQUEST['a'])) { echo "<br>input a: "; echo $_REQUEST['a']; } if (isset($_REQUEST['b'])) { echo "<br>input b: "; echo $_REQUEST['b']; } ?> <form method="post"> a <input type="text" name="a" autocomplete="off"><br> b <input type="text" name="b" autocomplete="off"><br> <input type="submit"> </form> <p> Lopp. </body>
Andmebaasipäring, kus me ise paneme kokku sql where tingimuse vastavalt kasutaja antud väärtustele (siin näites ei ole see turvaliselt tehtud, vaata paari järgmist). http://dijkstra.cs.ttu.ee/~tammet/k6.php
<body> Algus. <p> <?php $con = mysqli_connect("localhost","st2014","progress","st2014"); if (!$con) { die('Could not connect: ' . mysqli_connect_error()); } $username = "tanel"; if (isset($_REQUEST['username'])) { $username = $_REQUEST['username']; } $q="SELECT * from users where username='$username'"; echo "query: $q <br>"; $result = mysqli_query($con,$q); echo "leitud:<p>"; while($row = mysqli_fetch_array($result)) { echo $row['username'] . " " . $row['name']; echo "<br />"; } mysqli_close($con); ?> <form method="post"> username <input type="text" name="username" autocomplete="off"><br> <input type="submit"> </form> <p> Lopp. </body>
Siin paneme juurde esmaselt vajaliku turva (et kasutaja ei saaks sql süntaksit sisestada), selleks on funktsioon mysqli_real_escape_string: http://dijkstra.cs.ttu.ee/~tammet/k8.php
<body> Algus. <p> <?php $q=""; $c=""; function addcond($con,$fld) { global $q, $c; if (isset($_REQUEST[$fld]) && $_REQUEST[$fld]!="") { if ($c!="") $c=$c . " and "; $c=$c . " $fld='" . mysqli_real_escape_string($con,$_REQUEST[$fld]) ."' "; } } function main() { global $q, $c; $con = mysqli_connect("localhost","st2014","progress","st2014"); if (!$con) { die('Could not connect: ' . mysqli_error()); } $q="SELECT * from users where "; $c=""; addcond($con,"username"); addcond($con,"name"); addcond($con,"sex"); if ($c=="") { echo "<p>palun sisesta midagi otsinguvaljadele!<p>"; } else { $q=$q . $c; echo "query: " . $q . "<br>"; $result = mysqli_query($con,$q); echo "leitud:<p>"; while($row = mysqli_fetch_array($result)) { echo $row['username'] . " " . $row['name']; echo "<br />"; } } mysqli_close($con); } main(); ?> <form method="post"> username <input type="text" name="username" autocomplete="off"><br> name <input type="text" name="name" autocomplete="off"><br> sex <input type="text" name="sex" autocomplete="off"><br> <input type="submit"> </form> <p> Lopp. </body>