Php Veritabanı Bağlantısı (Php Mysql Bağlantısı)

Php Veritabanı Bağlantısı (Php Mysql Bağlantısı)


Burada yazıla php kodlarıyla bir anket çalışması yapılmış veriler mysql veritabanı üzerinde bulunan tablolara yazılmıştır. Dört sayfadan oluşan projede birinci aşamada php ile mysql veritabanının bağlantısı sağlanmıştır. İkinci aşamadan yapılacak anketi seçeceğiniz arayüz olan index.php bulunmaktadır. Üçüncü sayfada oy verme işlemi bulunmaktadır. Dördüncü sayfada ise seçtiğiniz ankete göre uygun sonuçlar uygun bir grafikle size gösterilmiştir.
Önemli Not: Projeyi çalıştırmak için baglanti.php dosyasını açıp;
veritabani_kullanici_adi
veritabani_kullanici_sifresi
veritabani_ismi
yerlerine uygun değerleri girmeniz gerekmektedir.

index.php Sayfası
  1.  <html>
  2. <head>
  3.    <title>Php İle Veritabanı Bağlantısı</title>
  4.    <meta http-equiv=”Content-Type” content=”text/html” charset=”iso-8859-9″ />
  5. <head>
  6. <body>
  7. <?php
  8. include_once(“baglanti.php”);
  9. baglan();
  10. switch($_REQUEST[‘ope’]){
  11.    case “view”:
  12.       view($_GET[‘surveyno’]);
  13.       print “<a href=’index.php’><< Geri Dön</a>”;
  14.       break;
  15.    case “vote”:
  16.       if (!isset($_POST[‘answer’])){
  17.          print “<font color=’red’>Lütfen Bir Yanıt Seçin!</font>
  18. \n”;
  19.          print “<a href=’index.php’><< Geri Dön</a>”;
  20.          exit;
  21.       }
  22.       print “Oy Verdiğiniz İçin Teşekkürler.
  23. \n”;
  24.       print “
  25. \nDaha Fazla Yanıt Vermek İçin.
  26. \nDevam Edin.
  27. \n”;
  28.       if(isset($_POST[‘answer’]) AND isset($_POST[‘surveyno’])){
  29.          $sql=”SELECT count(*) as sayi FROM answer WHERE answer='{$_POST[‘answer’]}’ AND sid={$_POST[‘surveyno’]} AND date='”.date(“Y-m-d”).”‘;”;
  30.          $result=mysql_query($sql) OR die(“Cannot query answers and dates”);
  31.          $value=mysql_result($result,0,0);
  32.          if($value==”0″)
  33.          {
  34.             $sql=”INSERT INTO answer(sid,answer,votes,date) VALUES({$_POST[‘surveyno’]},'{$_POST[‘answer’]}’,1,'”.date(“Y-m-d”).”‘)”;
  35.             $result=mysql_query($sql) OR die(“Cannot insert.”);
  36.          }
  37.          else
  38.          {
  39.             $sql=”UPDATE answer SET votes=votes+1 WHERE answer='{$_POST[‘answer’]}’ AND sid={$_POST[‘surveyno’]} AND date='”.date(“Y-m-d”).”‘;”;
  40.             $result=mysql_query($sql) OR die(“Cannot update.”);
  41.          }
  42.       }
  43.       viewList();
  44.       break;
  45.    default:
  46.       print ” “;
  47.       viewList();
  48.       break;
  49. }
  50. function viewList(){
  51.    $sql=”SELECT * FROM survey”;
  52.    $result=mysql_query($sql) or die(“Cannot select l:50”);
  53.    print “<ol>\n”;
  54.    while( $res=mysql_fetch_array($result) ){
  55.       $a[]=$res;
  56.       print “<li><a href=’?ope=view&surveyno={$res[‘sid’]}  ‘>{$res[‘question’]}</a></li>\n”;
  57.    }
  58.    print “</ol>\n\n”;
  59. }
  60. function view($surveyno){
  61.    $sql=”SELECT * FROM survey WHERE sid=$surveyno”;
  62.    $result=mysql_query($sql) or die(“Cannot select l:62”);
  63.    $sqlAnswer=”SELECT DISTINCT sid,answer FROM answer WHERE sid=$surveyno”;
  64.    $resultAnswer=mysql_query($sqlAnswer) or die(“Cannot select l:65.\n”);
  65.    $res=mysql_fetch_array($result);
  66.    print “<form method=’POST’ action=’index.php’>\n”;
  67.    print “<table border=’1′>\n”;
  68.    print “<tr><td colspan=3>{$res[‘question’]}</td></tr>\n”;
  69.    print ” <input type=’hidden’ name=’ope’ value=’vote’></td>\n”;
  70.    print ” <input type=’hidden’ name=’surveyno’ value='{$res[‘sid’]}’></td>\n”;
  71.    while( $resAnswer=mysql_fetch_array($resultAnswer) ){
  72.       print “<tr>\n”;
  73.       print ” <td weigth=’10’><input type=’radio’ name=’answer’ value='{$resAnswer[‘answer’]}’>\n”;
  74.       print ” <td>{$resAnswer[‘answer’]}</td>\n”;
  75.       // print ” <td>{$resAnswer[‘vote’]}</td>\n”;
  76.       print “</tr>\n”;
  77.    }
  78.    print “<tr>\n”;
  79.    print ” <td colspan=3>\n”;
  80.    print ”  <table border=’0′>\n”;
  81.    print “<tr>\n”;
  82.    print “<td><input type=’submit’ value=’Oy Ver’></td>\n”;
  83.    print “</form>\n”;
  84.    print “<form method=’POST’ action=’vote.php’>\n”;
  85.    print “<input type=’hidden’ name=’surveyno’ value='{$res[0]}’>\n”;
  86.    print “<input type=’hidden’ name=’question’ value='{$res[‘question’]}’>\n”;
  87.    print “<td width=’%100′ align=’right’><input type=’submit’ value=’Sonuclar’></td>\n”;
  88.    print “</tr>\n”;
  89.    print “</form>\n”;
  90.    print “</table>”;
  91.    print “</td>”;
  92.    print “</tr>\n”;
  93.    print “</table>\n”;
  94.    print “
  95. \n\n”;
  96. }
  97. ?>
  98. </body>
  99. </html>
baglanti.php kodu:
  1. <?php
  2. function baglan(){
  3.    if ($_SERVER[“SERVER_ADDR”] == “*******”){
  4.       $my = mysql_connect(“localhost”, “********”, “********”);
  5.       $mysqli = mysql_select_db(“********”)  or die (“Unable to select!”);
  6.    }
  7.    else{
  8.       mysql_connect(“localhost”,”veritabani_kullanici_adi”,”veritabani_kullanici_sifresi”) or die(“Cannot connect to database.\n”);
  9.       mysql_select_db(“veritabani_ismi”);
  10.    }
  11. }
  12. ?>
vote.php kodu:
  1. <?
  2. session_start();
  3. if (!isset($_SESSION[‘vote’]) || isset($_POST[‘surveyno’])){
  4.    $_SESSION[‘vote’] = $_POST[‘surveyno’];
  5.    $_SESSION[‘title’] =  $_POST[‘question’];
  6. }
  7. ?>
  8. <html>
  9. <head>
  10.    <title><?=$_SESSION[‘title’]?></title>
  11.    <meta http-equiv=”Content-Type” content=”text/html” charset=”utf-8″ />
  12. <head>
  13. <body>
  14. <a href=”vote.php”><img src=”grafik.php” border=0/></a>
  15. <a href=”index.php”><< Geri Don</a>
  16. </body>
  17. <html>

grafik.php kodu:

  1. <?php
  2. session_start();
  3. $vote = $_SESSION[‘vote’];
  4. include_once(“baglanti.php”);
  5. baglan();
  6. $sql=”SELECT DISTINCT date FROM answer WHERE sid=$vote ORDER BY date”;
  7. $result=mysql_query($sql) or die(“Cannot query answers”);
  8. while($dates[]=mysql_fetch_array($result));
  9. unset($dates[count($dates)-1]);
  10. $sql=”SELECT DISTINCT answer FROM answer WHERE sid=$vote”;
  11. $result=mysql_query($sql) or die(“Cannot query answers”);
  12. while($answers[]=mysql_fetch_array($result));
  13. unset($answers[count($answers)-1]);
  14. $sql=”SELECT * FROM answer WHERE sid=$vote”;
  15. $result=mysql_query($sql) or die(“Cannot query answers”);
  16. while($row=mysql_fetch_assoc($result))
  17. {
  18.     $data[$row[‘answer’]][$row[‘date’]]=$row[‘votes’];
  19. }
  20. $sql=”SELECT MAX(votes) FROM answer WHERE sid=$vote”;
  21. $result=mysql_query($sql) or die(“Cannot query answers”);
  22. //print “dsadas”;
  23. $votesmax=mysql_result($result,0,0);
  24. draw(300,300);
  25. function draw($height, $width){
  26.    //global $result, $array,$resultQuestion;
  27.    global $answers,$dates,$data,$votesmax;
  28.    $hmargin=40;
  29.    header (“Content-type: image/png”);
  30.    $myImage = ImageCreate($width+40,$height+(count($data)*15)+60);
  31.    $backGround = ImageColorAllocate ($myImage, rand(0, 255), rand(0, 255), rand(0, 255)  );
  32.    $white = ImageColorAllocate ($myImage, 255, 255, 255);
  33.    $red  = ImageColorAllocate ($myImage, 255, 0, 0);
  34.    $green = ImageColorAllocate ($myImage, 0, 255, 0);
  35.    $blue = ImageColorAllocate ($myImage, 0, 0, 255);
  36.    $grey = ImageColorAllocate ($myImage, 136,136,136);
  37.    imagestring($myImage, 5, 5, $height+15, $resultQuestion[‘question’], $white);
  38.    $counter=1;
  39.    $xaralik=$width/count($dates);
  40.    $yaralik=$height/$votesmax;
  41.    imageline($myImage,$hmargin,0,$hmargin,$height,$white);
  42.    imageline($myImage,$hmargin,$height,$width,$height,$white);
  43.    for($i=0;$i<10;$i++)
  44.     imagestring($myImage,3,0,$height-$height/10*$i,$votesmax/10*$i,$white);
  45.    for($a=0;$a<count($answers);$a++)
  46.    {
  47.     $color = ImageColorAllocate ($myImage, rand(0, 255), rand(0, 255), rand(0, 255));
  48.    for($d=0;$d<count($dates)-1;$d++)
  49.    {
  50.         $v1=$data[$answers[$a][0]][$dates[$d][0]];
  51.         $v2=$data[$answers[$a][0]][$dates[$d+1][0]];
  52.         imageline($myImage,$hmargin+$xaralik*$d,$height-$yaralik*$v1,$hmargin+$xaralik*($d+1),$height-$yaralik*$v2,$color);
  53.         imagestring($myImage,3,$hmargin+$xaralik*$d,$height+5,$dates[$d][0],$white);
  54.         imagestring($myImage,3,$hmargin+$xaralik*($d+1),$height+5,$dates[$d+1][0],$white);
  55.    }
  56.    imagestring($myImage,3,$hmargin,20+$height+15*$a,$answers[$a][0],$color);
  57.    }
  58.    imagestring($myImage, 5, $centerX, $height+($counter+2)*15, “”, $white);
  59.    ImagePNG($myImage);
  60.    ImageDestroy($myImage);
  61. }
  62. function calculateAngle(){
  63.    global $row;
  64.    $num = $row;
  65.    $result = 360 / $num;
  66.    return $result;
  67. }
  68. function aciCizgi($image,$x1,$y1,$length,$aci,$color){
  69.     $x2 = $x1 + sindeg2rad($aci) ) * $length;
  70.     $y2 = $y1 + cosdeg2rad($aci+180) ) * $length;
  71.     imageline($image,$x1,$y1,$x2,$y2,$color);
  72. }
  73. function aciString($image,$font,$x1,$y1,$length,$aci,$string,$color){
  74.     $x2 = $x1 + sindeg2rad($aci) ) * $length;
  75.     $y2 = $y1 + cosdeg2rad($aci+180) ) * $length;
  76.     imagestring($image, $font, $x2,$y2, $string, $color);
  77. }
  78. ?>
Google Plus ile Paylaş

Kısaca: seymanblog

Panelde şablon düzenle deyip, bu satırı aratarak buraya kısaca hakkımda yazısı yazabilirsiniz.
    BLOGGER YORUMLARI
    FACEBOOK YORUMLARI

0 yorum:

Yorum Gönder