PHP 6

From Ilianko
Revision as of 13:28, 24 October 2013 by Anko (talk | contribs)

Връзки с MySQL

Основни функции

  • mysqli_connect();
  • mysqli_query();
  • mysqli_fetch_assoc();
  • mysqli_real_escape_string();


Извличане на информация от база данни

<?php 
  //conection:
  $link = mysqli_connect("localhost","student","3405","student"); 
  mysqli_query($link, "SET NAMES utf8");
  mb_internal_encoding('utf-8');
  
?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8"  />
  </head>
<body>
 <form action="check.php" method="POST">
 <?php

//consultation:
$query = "SELECT * FROM  `Questions`";

//execute the query.
$result = mysqli_query($link, $query);


//display information:
$x = 0;
while($row = mysqli_fetch_array($result)) {
  
  $x++;	
  echo $x.'  -  '.$row['question'].'<br>';
 
  $query = 'SELECT * FROM  `Answers` WHERE  `id_question` = '.$row['id'];
  $otgovori =  mysqli_query($link, $query);
  
  $i = 97; 
  while($otgovor = mysqli_fetch_array($otgovori)) {	
    echo  chr($i++).'<input type="radio" name="question['.$row['id'].']"
 value="'.$otgovor['id'].'">'.$otgovor['answer']."<br /> \n"; 
  }
}
 

?>
  <input type="submit" name='submit' value="Submit">
  </form>
</body>
</html>

<?php

$link = mysqli_connect("localhost","student","3405","student"); 

mysqli_query($link, "SET NAMES utf8");


?>
<!DOCTYPE html>
<html>
  <head>
  	
  	
    <meta charset="UTF-8"  />
  </head>
<body>
<?php

  $ch = 0; $an = 0;

  if(isset($_POST['submit']))
  {
     
	foreach($_POST['question'] as $question => $otgovor)
	{
	  $an++;	
		
	  $query = "SELECT  `check` FROM  `Answers` 
	  WHERE  `id` = ".$otgovor." AND  `id_question` = ".$question;
	
	  $result = mysqli_query($link, $query);
	  
	  $check = mysqli_fetch_array($result);
	  
	  $ch = $ch + $check['check'];
	  
	  
	}
	  print ($ch/$an*100).' % Resultat<br>';
  }




?>





<?php
  mb_internal_encoding('utf-8');
  


//conection:


//consultation:
$query = "SELECT * FROM  `Questions`";

//execute the query.
$result = mysqli_query($link, $query);


//display information:
while($row = mysqli_fetch_array($result)) {
  	
  echo $row['id'].'  -  '.$row['question'].'<br>';
 
  $query = 'SELECT * FROM  `Answers` WHERE  `id_question` = '.$row['id'];
  $otgovori =  mysqli_query($link, $query);
  
  $i = 97; 
  while($otgovor = mysqli_fetch_array($otgovori)) {
  	
	
	if ( $_POST['question'][$row['id']] == $otgovor['id'])
	{
	   if(  $otgovor['check'] )
	   {	
	echo '<span style="background: #00aa00">';
	echo  chr($i++).' '.$otgovor['answer']."<br /> \n";
	echo '</span>' ;
	   }
	   else {
		   echo '<span style="background: #FFaaaa">';
	echo  chr($i++).' '.$otgovor['answer']."<br /> \n";
	echo '</span>' ;
	   }
	
	}
	else {
		echo  chr($i++).' '.$otgovor['answer']."<br /> \n";
	}
	
  }


}
 

?>

</body>
</html>

Запис на данни

<?php

$link = mysqli_connect("localhost","student","3405","student");

if(!$link)
{ 
	echo "Greshka s BD";
	
	exit();
	
}
 
mysqli_query($link, "SET NAMES utf8");



print_r($_POST);


if(isset($_POST['submit']))
{

$query = "INSERT INTO  `student`.`Questions` (
			`id` ,
			`question`
		)	
		VALUES (
		NULL ,  '".$_POST['question']."'
		)";


  mysqli_query($link, $query);
}
?>
<!DOCTYPE html>
<html>
  <head>
  	
  	
    <meta charset="UTF-8"  />
  </head>
<body>


<form action=""  method=POST>
	
	<input type=text name=question />
	
	 
	<input type="submit"  name=submit value="New Question">
	
	
	
</form>


<?php


$link = mysqli_connect("localhost","student","3405","student");

if(!$link)
{ 
	echo "Greshka s BD";
	
	exit();
	
}
 
mysqli_query($link, "SET NAMES utf8");


if( isset($_POST['submit']))
{

$query = "INSERT INTO  `student`.`Answers` (
`id` ,
`id_question` ,
`answer` ,
`check`
)
VALUES (
NULL ,  ".mysqli_real_escape_string($_POST['question'])).",  '".mysqli_real_escape_string($_POST['answer'])."',  '".mysqli_real_escape_string($_POST['check'])."'
);" ;


print $query;
mysqli_query($link, $query);
}

?>
<!DOCTYPE html>
<html>
  <head>
  	
  	
    <meta charset="UTF-8"  />
  </head>
<body>





<form action="" method="POST">


    <select   name=question>
<?php

//consultation:
$query = "SELECT * FROM  `Questions`";

//execute the query.
$result = mysqli_query($link, $query);


//display information:
while($row = mysqli_fetch_array($result)) {
  	
  echo '<option value='.$row['id'].'>'.$row['question'].'</option>';

}

?>     
</select>
    
    
    <input type=text name=answer />
    <input type=text maxlength="1" size="1" name=check />
    
    <input type="submit" name=submit value="New Answer" />
    
</form>