Difference between revisions of "PHP 6"
From Ilianko
(Created page with "*mysqli_connect("localhost", 'books','qwerty','books'))) *mysqli_query($con, "SET NAMES utf8") *mysqli_real_escape_string(); *mysqli_query(); *mysqli_fetch_assoc();") |
|||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | [[Category:PHP]] | |
− | + | == Връзки с MySQL == | |
− | * | + | |
+ | Основни функции | ||
+ | |||
+ | *mysqli_connect(); | ||
*mysqli_query(); | *mysqli_query(); | ||
*mysqli_fetch_assoc(); | *mysqli_fetch_assoc(); | ||
+ | *mysqli_real_escape_string(); | ||
+ | |||
+ | |||
+ | |||
+ | == Извличане на информация от база данни == | ||
+ | |||
+ | <code><pre> | ||
+ | <?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> | ||
+ | |||
+ | </pre></code> | ||
+ | |||
+ | <code><pre> | ||
+ | <?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> | ||
+ | </pre></code> | ||
+ | |||
+ | == Запис на данни == | ||
+ | |||
+ | <code><pre> | ||
+ | <?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> | ||
+ | </pre></code> | ||
+ | |||
+ | |||
+ | <code><pre> | ||
+ | <?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> | ||
+ | |||
+ | </pre></code> | ||
+ | |||
+ | |||
+ | |||
+ | == БД == | ||
+ | <code><pre> | ||
+ | |||
+ | -- phpMyAdmin SQL Dump | ||
+ | -- version 3.3.2deb1ubuntu1 | ||
+ | -- http://www.phpmyadmin.net | ||
+ | -- | ||
+ | -- Host: localhost | ||
+ | -- Generation Time: Oct 24, 2013 at 06:06 PM | ||
+ | -- Server version: 5.1.70 | ||
+ | -- PHP Version: 5.3.2-1ubuntu4.21 | ||
+ | |||
+ | SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; | ||
+ | |||
+ | |||
+ | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; | ||
+ | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; | ||
+ | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; | ||
+ | /*!40101 SET NAMES utf8 */; | ||
+ | |||
+ | -- | ||
+ | -- Database: `student` | ||
+ | -- | ||
+ | |||
+ | -- -------------------------------------------------------- | ||
+ | |||
+ | -- | ||
+ | -- Table structure for table `Answers` | ||
+ | -- | ||
+ | |||
+ | CREATE TABLE IF NOT EXISTS `Answers` ( | ||
+ | `id` int(11) NOT NULL AUTO_INCREMENT, | ||
+ | `id_question` int(11) NOT NULL, | ||
+ | `answer` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, | ||
+ | `check` tinyint(1) NOT NULL, | ||
+ | PRIMARY KEY (`id`) | ||
+ | ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=19 ; | ||
+ | |||
+ | -- | ||
+ | -- Dumping data for table `Answers` | ||
+ | -- | ||
+ | |||
+ | INSERT INTO `Answers` (`id`, `id_question`, `answer`, `check`) VALUES | ||
+ | (1, 1, '1901', 0), | ||
+ | (2, 1, '1895', 0), | ||
+ | (3, 1, '1903', 1), | ||
+ | (4, 1, '1916', 0), | ||
+ | (5, 2, '1939', 0), | ||
+ | (6, 2, '1941', 0), | ||
+ | (7, 2, '1936', 1), | ||
+ | (8, 2, '1930', 0), | ||
+ | (9, 1, '1989', 0), | ||
+ | (10, 2, '1901', 0), | ||
+ | (11, 3, 'Преврат', 1), | ||
+ | (12, 3, 'Победа', 0), | ||
+ | (13, 4, '3 X 1989', 1), | ||
+ | (14, 4, '3 X 1990', 1), | ||
+ | (15, 8, 'Юрий Гагарин', 1), | ||
+ | (16, 8, 'Нийл Армстронг', 0), | ||
+ | (17, 8, 'Георги Иванов', 0), | ||
+ | (18, 8, 'Александър Александров', 0); | ||
+ | |||
+ | -- -------------------------------------------------------- | ||
+ | |||
+ | -- | ||
+ | -- Table structure for table `Questions` | ||
+ | -- | ||
+ | |||
+ | CREATE TABLE IF NOT EXISTS `Questions` ( | ||
+ | `id` int(11) NOT NULL AUTO_INCREMENT, | ||
+ | `question` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, | ||
+ | PRIMARY KEY (`id`) | ||
+ | ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ; | ||
+ | |||
+ | -- | ||
+ | -- Dumping data for table `Questions` | ||
+ | -- | ||
+ | |||
+ | INSERT INTO `Questions` (`id`, `question`) VALUES | ||
+ | (1, 'Кога е първото заседание на парламента?'), | ||
+ | (2, 'Кога е създаден първият електронен компютър?'), | ||
+ | (3, 'Какво се е случило на 9 IX 1944'), | ||
+ | (4, 'Кога е паднала Берлинската стена?'), | ||
+ | (8, 'Кой е първият космонавт?'); | ||
+ | |||
+ | |||
+ | </pre></code> |
Latest revision as of 05:44, 25 October 2013
Връзки с 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>
БД
-- phpMyAdmin SQL Dump
-- version 3.3.2deb1ubuntu1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Oct 24, 2013 at 06:06 PM
-- Server version: 5.1.70
-- PHP Version: 5.3.2-1ubuntu4.21
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `student`
--
-- --------------------------------------------------------
--
-- Table structure for table `Answers`
--
CREATE TABLE IF NOT EXISTS `Answers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_question` int(11) NOT NULL,
`answer` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`check` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=19 ;
--
-- Dumping data for table `Answers`
--
INSERT INTO `Answers` (`id`, `id_question`, `answer`, `check`) VALUES
(1, 1, '1901', 0),
(2, 1, '1895', 0),
(3, 1, '1903', 1),
(4, 1, '1916', 0),
(5, 2, '1939', 0),
(6, 2, '1941', 0),
(7, 2, '1936', 1),
(8, 2, '1930', 0),
(9, 1, '1989', 0),
(10, 2, '1901', 0),
(11, 3, 'Преврат', 1),
(12, 3, 'Победа', 0),
(13, 4, '3 X 1989', 1),
(14, 4, '3 X 1990', 1),
(15, 8, 'Юрий Гагарин', 1),
(16, 8, 'Нийл Армстронг', 0),
(17, 8, 'Георги Иванов', 0),
(18, 8, 'Александър Александров', 0);
-- --------------------------------------------------------
--
-- Table structure for table `Questions`
--
CREATE TABLE IF NOT EXISTS `Questions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`question` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
--
-- Dumping data for table `Questions`
--
INSERT INTO `Questions` (`id`, `question`) VALUES
(1, 'Кога е първото заседание на парламента?'),
(2, 'Кога е създаден първият електронен компютър?'),
(3, 'Какво се е случило на 9 IX 1944'),
(4, 'Кога е паднала Берлинската стена?'),
(8, 'Кой е първият космонавт?');