PHP - , - HTML.

PHP?
PHP 80% -, . - -.
:
- - ( )
- (GUI)
-. , - , PHP . PHP-:
- cookie -
Facebook, , PHP.
PHP?
- PHP (Windows, Linux, Unix, Mac OS X )
- PHP (Apache, IIS )
- PHP
- PHP
- PHP
PHP?
- PHP , HTML, CSS, JavaScript PHP
- PHP , HTML
- PHP
.php
PHP?
PHP- -, . , - -, PHP, PHP. - - PHP. - HTML- - . PHP- -, HTML-, PHP.
PHP - . , , , . .
PHP <?php
?>
HTML.
PHP - .
GNU/Linux
GNU/Linux Debian PHP :
sudo apt install php
Centos 6 7 PHP :
sudo yum install php
PHP, :
php file.php
- PHP. - Apache:
sudo apt install apache2 libapache2-mod-php
PHP, MySQL -, XAMPP ( - ) , WAMP Open Server.
PHP-
- , -. , :
- , , .
- , .
- .
- MVC (--), .
PHP:
PHP <?php
?>
. , PHP ;
.
, echo
Hello World!
<!DOCTYPE html> <html> <body> <h1>Merion Networks</h1> <?php echo "Hello World!"; ?> </body> </html>
:
Merion Networks Hello World!
print
.
:echo
,echo
,
PHP :
<?php // # , ?>
<? php / * * / ?>
, .
echo
<?php ECHO "Hello!<br>"; echo "Welcome to Developer News<br>"; EcHo "Enjoy all of the ad-free articles<br>"; ?>
. , $name
. $NAME
$NaMe
:
<?php $name = "Alex"; echo "Hi! My name is " . $name . "<br>"; echo "Hi! My name is " . $NAME . "<br>"; echo "Hi! My name is " . $NaMe . "<br>"; ?>
- PHP.
PHP , $variable_name
. , =
, .
:
<? php // "Hello!" greeting $greeting = "Hello!"; // 8 month $month = 8; // 2019 year $year = 2019; ?>
PHP
-
$
, . -
_
. - ,
A z
,09
_
. ,+ - % () . &
. - .
:
- $my_variable
- $anotherVariable
- $the2ndVariable
PHP , , , . , , . , PHP.
- $this
- $_GET
- $_POST
- $_SERVER
- $_FILES
PHP
, :
-
"Hello"
-
5
-
1.0
-
1 or 0
-
array("I", "am", "an", "array")
- NULL
- . ( ):
$x = "Hello!"; $y = 'Hello!';
- -2 147 483 648
2 147 483 647
.
:
- .
- .
- , .
$x = 5;
- .
$x = 5.01;
: TRUE () FALSE (). .
$x = true; $y = false;
.
$colors = array("Red", "Green", "Blue");
Null
Null - ,null
. , null
. , , null
.
<?php $greeting = "Hello!"; // , null $greeting = null; ?>
- , , . , .
<?php class Car { function Car() { $this->model = "Tesla"; } } // $Lightning = new Car(); // echo $Lightning->model; ?>
PHP
- , . . getresourcetype()
, .
<?php $c = mysql_connect(); echo get_resource_type($c) . "\n"; $fp = fopen("foo", "w"); echo get_resource_type($fp) . "\n"; $doc = new_xmldoc("1.0"); echo get_resource_type($doc->doc) . "\n";
(string) - . .
PHP.
.
$name = 'Joe';
, , .
$last_name = 'O\'Brian';
, .
$name = "Joe";
, , .
$quote = "Mary said, \"I want some toast,\" and then ran away.";
escape-. , , . \n
, \t
\\
.
PHP , .
$name = 'Joe'; $greeting = "Hello $name"; // "Hello Joe"
strlen()
.
<?php echo strlen("Developer News"); // 14 ?>
strwordcount()
:
<?php echo str_word_count("Developer News"); // 2 ?>
strrev()
:
<?php echo strrev("Developer News"); // sweN repoleveD ?>
strpos()
:
<?php echo strpos("Developer News", "News"); // 10 ?>
str_replace()
:
<?php echo str_replace("World", "Developer", "World News"); // Developer News ?>
- PHP. define()
- , (true false), , ( false). . , (, API).
, , .
<?php define("freeCodeCamp", "Learn to code and help nonprofits", false); echo freeCodeCamp; >?
, , .
class Human { const TYPE_MALE = 'm'; const TYPE_FEMALE = 'f'; const TYPE_UNKNOWN = 'u'; ............. }
.Human
,self::CONSTANT_NAME
. ,Human::CONSTANT_NAME
.
var_dump()
. . .
<?php $a = 32; echo var_dump($a) . "<br>"; $b = "Hello world!"; echo var_dump($b) . "<br>"; $c = 32.5; echo var_dump($c) . "<br>"; $d = array("red", "green", "blue"); echo var_dump($d) . "<br>"; $e = array(32, "Hello world!", 32.5, array("red", "green", "blue")); echo var_dump($e) . "<br>"; // echo var_dump($a, $b) . "<br>"; ?>
:
int(32) string(12) "Hello world!" float(32.5) array(3) { [0]=> string(3) "red" [1]=> string(5) "green" [2]=> string(4) "blue" } array(4) { [0]=> int(32) [1]=> string(12) "Hello world!" [2]=> float(32.5) [3]=> array(3) { [0]=> string(3) "red" [1]=> string(5) "green" [2]=> string(4) "blue" } } int(32) string(12) "Hello world!"
PHP , .
=
, ==
===
.
<
>
, +=
.
.
.=
.
PHP 7.0.X spaceship ( ) <=>
. y -1, 0 1, $a
, $b
.
<?php echo 1 <=> 1; // 0 echo 1 <=> 2; // -1 echo 2 <=> 1; // 1
If / Else / Elseif
If / Else - , .
. {} , , .
If
<?php if (condition) { statement1; statement2; }
. if
, .
Else
<?php if (condition) { statement1; statement2; } else { statement3; statement4; }
. else
.
Elseif
<?php if (condition1) { statement1; statement2; } elseif (condition2) { statement3; statement4; } else { statement5; }
: elseif
.
If / Else
<?php if (condition1) { if (condition2) { statement1; statement2; } else { statement3; statement4; } } else { if (condition3) { statement5; statement6; } else { statement7; statement8; } }
||
, xor
&&.
:
<?php if (condition1 && condition2) { echo 'Both conditions are true!'; } elseif (condition 1 || condition2) { echo 'One condition is true!'; } else (condition1 xor condition2) { echo 'One condition is true, and one condition is false!'; }
. , ( ).
If / Else
.
if (condition1): statement1; else: statement5; endif;
if / else.
, ( ), , , , .
If / Else:
if($user == !NULL { $message = 'Hello '. $user; } else { $message = 'Hello guest'; }
:
:
$a = () ? $b : $c;
, (true), $a $b, , $c
$message = 'Hello '.($user == !NULL ? $user : 'Guest');
Switch
PHP Switch
Switch JavaScript. , .
<?php // Switch Statement Example switch ($i) { case "free": echo "i is free"; break; case "code": echo "i is code"; break; case "camp": echo "i is camp"; break; default: echo "i is freecodecamp"; break; }
Break
break;
switch
. break;
, , , break;
.
:
<?php $j = 0; switch ($i) { case '2': $j++; case '1': $j++; break; default: break; }
$i = 1
, $j
:
1
$i = 2
, $j
:
2
break;
, , :
<?php switch ($i) { case '1': return 1; case '2': return 2; default: break; }
<?php switch ($i) { case '1': return 1; break; case '2': return 2; break; default: break; }
:
<?php // $diceNumber = mt_rand(1, 6); // $numText = ""; // switch switch($diceNumber) { case 1: $numText = "One"; break; case 2: $numText = "Two"; break; case 3: case 4: // case 3 4 $numText = "Three or Four"; break; case 5: $numText = "Five"; echo $numText; // break; // break return case. case 6: $numText = "Six"; echo $numText; break; default: $numText = "unknown"; } // echo 'Dice show number '.$numText.'.'; ?>
:
case = 1 > Dice show number One. case = 2 > Dice show number Two. case = 3 > Dice show number Three or Four. case = 4 > Dice show number Three or Four. case = 5 > FiveSixDice show number Six. case = 6 > SixDice show number Six. > Dice show number unknown.
, , .
break
.
for
.
<?php for($index = 0; $index < 5; $index ++) { echo "Current loop counter ".$index.".\n"; } ?> /* Output: Current loop counter 0. Current loop counter 1. Current loop counter 2. Current loop counter 3. Current loop counter 4. */
while
, .
<?php $index = 10; while ($index >= 0) { echo "The index is ".$index.".\n"; $index--; } ?> /* Output: The index is 10. The index is 9. The index is 8. The index is 7. The index is 6. The index is 5. The index is 4. The index is 3. The index is 2. The index is 1. The index is 0. */
Do...While
, .
<?php $index = 3; do { // 1 echo "Index: ".$index.".\n"; $index --; } while ($index > 0); ?> /* Output: Index: 3. Index: 2. Index: 1. */
foreach
.
foreach
/ .
:
foreach ($array as $value) { code to be executed; }
$value
, , .
<?php $fruits = array ("Orange", "Apple", "Banana", "Cherry", " Mango"); foreach ( $fruits as $value ) { echo "$value<br />"; } ?> Output: Orange Apple Banana Cherry Mango
- , .
:
function say_hello() { return "Hello!"; } echo say_hello();
:
function say_hello($friend) { return "Hello " . $friend . "!"; } echo say_hello('Tommy');
strtoupper - !
function makeItBIG($a_lot_of_names) { foreach($a_lot_of_names as $the_simpsons) { $BIG[] = strtoupper($the_simpsons); } return $BIG; } $a_lot_of_names = ['Homer', 'Marge', 'Bart', 'Maggy', 'Lisa']; var_dump(makeItBIG($a_lot_of_names));
, . , , , .
PHP : . , , .
- . . 0 , .
<?php $shopping_list = array("eggs", "milk", "cheese"); ?>
$shopping_list[0
] "eggs", $shopping_list[1]
"milk", $shopping_list[2]
"cheese".
- , , . , .
<?php $student_scores = array("Joe" => 83, "Frank" => "93", "Benji" => "90"); ?>
$student_scores['Joe']
83, $student_scores['Frank']
93, $student_scores['Benji']
90.
- , . , .
<?php $students = array( array("first_name" => "Joe", "score" => 83, "last_name" => "Smith"), array("first_name" => "Frank", "score" => 92, "last_name" => "Barbson"), array("first_name" => "Benji", "score" => 90, "last_name" => "Warner") ); ?>
first_name
:
$students[0]['first_name']
count()
( ) :
<?php $cars = array("Volvo", "BMW", "Toyota"); echo count($cars); ?>
PHP . .
sort()
sort()
/ (, A, B, C, D, E ... 5, 4, 3, 2, 1 ...)
<?php $freecodecamp = array("free", "code", "camp"); sort($freecodecamp); print_r($freecodecamp); ?>
:
Array ( [0] => camp [1] => code [2] => free )
rsort()
rsort()
/ (, Z, Y, X, W, V ... 5, 4, 3, 2, 1 ...)
<?php $freecodecamp = array("free", "code", "camp"); rsort($freecodecamp); print_r($freecodecamp); ?>
:
Array ( [0] => free [1] => code [2] => camp )
asort()
asort()
/ (, A, B, C, D, E ... 5, 4, 3, 2, 1 ...)
<?php $freecodecamp = array("zero"=>"free", "one"=>"code", "two"=>"camp"); asort($freecodecamp); print_r($freecodecamp); ?>
:
Array ( [two] => camp [one] => code [zero] => free )
ksort()
ksort()
/ (, A, B, C, D, E ... 5, 4, 3, 2, 1 ...)
<?php $freecodecamp = array("zero"=>"free", "one"=>"code", "two"=>"camp"); ksort($freecodecamp); print_r($freecodecamp); ?>
:
Array ( [one] => code [two] => camp [zero] => free )
arsort()
arsort()
/ (, Z, Y, X, W, V ... 5, 4, 3, 2, 1 ...)
<?php $freecodecamp = array("zero"=>"free", "one"=>"code", "two"=>"camp"); arsort($freecodecamp); print_r($freecodecamp); ?>
:
Array ( [zero] => free [one] => code [two] => camp )
krsort()
krsort()
/ (, Z, Y, X, W, V ... 5, 4, 3, 2, 1 ...)
<?php $freecodecamp = array("zero"=>"free", "one"=>"code", "two"=>"camp"); krsort($freecodecamp); print_r($freecodecamp); ?>
:
Array ( [zero] => free [two] => camp [one] => code )
- -. , .
, PHP, html. PHP post
get
.
<html> <body> <form method="get" action="target_proccessor.php"> <input type="search" name="search" /><br /> <input type="submit" name="submit" value="Search" /><br /> </form> <body> </html>
method
. action
, . name
, , PHP .
PHP . isset
, empty
is_numeric
.
, ,
isset
, . :
$firstName = $_GET['firstName'] if(isset($firstName)){ echo "firstName field is set". "<br>"; } else{ echo "The field is not set."."<br>"; }
$POST
$GET
.
HTML- :
<html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> </body> </html>, PHP
welcome.php
. HTTP POST.
, . Welcome.php
:
<html> <body> Welcome <?php echo $_POST["name"]; ?><br> Your email address is: <?php echo $_POST["email"]; ?> </body> </html>
:
Welcome John Your email address is john.doe@example.com
HTTP GET.
GET vs POST
GET, POST (, array (key1 => value1, key2 => value2, key3 => value3, ...)
). /, - , - .
GET, POST $ _GET
$ _POST
. , , - , , .
$_GET
- , URL.
$_POST
- , HTTP POST.
GET?
, GET, ( URL-). GET . 2000 . , URL-, . .
GET .
: GET !
POST?
, POST, ( HTTP-) .
, POST , .
, URL-, .
PHP
include
( require
) // ?, , , include
.
, PHP, HTML -.
PHP include require
PHP PHP ( , ), include
require
.
include
require
, :
require
E_COMPILE_ERROR
include
E_WARNING
,
. , (), () -. , , .
Syntax include 'filename'; or require 'filename';
:
, footer.php
, :
<?php echo "<p>Copyright © 1999-" . date("Y") . " wiki.merionet.ru</p>"; ?>
, include
:
<html> <body> <h1>Welcome to my home page!</h1> <p>Some text.</p> <p>Some more text.</p> <?php include 'footer.php';?> </body> </html>
PHP
PHP , , .
PHP readfile ()
readfile()
.
, webdictionary.txt
, , :
AJAX = Asynchronous JavaScript and XML CSS = Cascading Style Sheets HTML = Hyper Text Markup Language PHP = PHP Hypertext Preprocessor SQL = Structured Query Language SVG = Scalable Vector Graphics XML = EXtensible Markup Language
PHP ( readfile()
, ):
<?php echo readfile("webdictionary.txt"); ?>
AJAX = Asynchronous JavaScript and XML CSS = Cascading Style Sheets HTML = Hyper Text Markup Language PHP = PHP Hypertext Preprocessor SQL = Structured Query Language SVG = Scalable Vector Graphics XML = EXtensible Markup Language236
readfile()
, , , .
PHP - fopen()
fopen()
. , readfile()
.
. fopen()
, , . , fopen()
:
<!DOCTYPE html> <html> <body> <?php $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!"); echo fread($myfile,filesize("webdictionary.txt")); fclose($myfile); ?> </body> </html>
AJAX = Asynchronous JavaScript and XML CSS = Cascading Style Sheets HTML = Hyper Text Markup Language PHP = PHP Hypertext Preprocessor SQL = Structured Query Language SVG = Scalable Vector Graphics XML = EXtensible Markup Language
die()
exit()
.exit()
.
:
r
- .w
- . , .a
- . . . ,x
- .FALSE
, .r+
- .w+
- . , .a+
- . . . ,x+
- .FALSE
, .
fopen()
. , , PHP , .
fopen()
, , , , w
a
.
PHP fread()
fread()
.
fread()
, .
PHP webdictionary.txt
:
fread($myfile,filesize("webdictionary.txt"));
PHP fclose()
fclose()
.
- , . , , , !
fclose()
( , ), :
<?php $myfile = fopen("webdictionary.txt", "r"); // ... fclose($myfile); ?>
PHP fwrite()
fwrite()
.
fwrite()
, - , .
newfile.txt
:
<?php $myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); $txt = "John Doe\n"; fwrite($myfile, $txt); $txt = "Jane Doe\n"; fwrite($myfile, $txt); fclose($myfile); ?>
PHP JSON
JSON?
JSON (JavaScript Object Notation) .
JSON , .
PHP JSON.
:
- json_encode()
- json_decode()
PHP json_encode()
json_encode()
JSON.
, JSON:
<!DOCTYPE html> <html> <body> <?php $age = array("Peter"=>35, "Ben"=>37, "Joe"=>43); echo json_encode($age); ?> </body> </html>
:
{"Peter":35,"Ben":37,"Joe":43}
PHP json_decode()
json_decode()
JSON PHP .
JSON PHP:
<!DOCTYPE html> <html> <body> <?php $jsonobj = '{"Peter":35,"Ben":37,"Joe":43}'; var_dump(json_decode($jsonobj)); ?> </body> </html>
:
object(stdClass)#1 (3) { ["Peter"]=> int(35) ["Ben"]=> int(37) ["Joe"]=> int(43) }
json_decode()
. json_decode()
, true, JSON .
<!DOCTYPE html> <html> <body> <?php $jsonobj = '{"Peter":35,"Ben":37,"Joe":43}'; var_dump(json_decode($jsonobj, true)); ?> </body> </html>
:
array(3) { ["Peter"]=> int(35) ["Ben"]=> int(37) ["Joe"]=> int(43) }
PHP
(exception) ? , PHP.
PHP . .
- , , .
throw
. , .
, Uncaught Exception
.
, :
<?php function divide($dividend, $divisor) { if($divisor == 0) { throw new Exception("Division by zero"); } return $dividend / $divisor; } echo divide(5, 0); ?>
:
Fatal error: Uncaught Exception: Division by zero in C:\webfolder\test.php:4 Stack trace: #0 C:\webfolder\test.php(9): divide(5, 0) #1 {main} thrown in C:\webfolder\test.php on line 4
try...catch
, try ... catch
.
try { , } catch(Exception $e) { , }
: :
<!DOCTYPE html> <html> <body> <?php function divide($dividend, $divisor) { if($divisor == 0) { throw new Exception("Division by zero"); } return $dividend / $divisor; } try { echo divide(5, 0); } catch(Exception $e) { echo "Unable to divide."; } ?> </body> </html>
:
Unable to divide.
catch , , , . - Exception
, - $e
.
try ... catch ... finally
. finally
, . finally
, catch
.
:
try { , } catch(Exception $e) { , } finally { , ,
PHP
PHP5, PHP- - .
- .
?
- .
- , , - - , , .
- :
- ,
- - .
- , - .
, , .
class
, {}
. :
<?php class Fruit { // ... } ?>
Fruit
, $name
$color
set_name()
get_name()
$name
:
<?php class Fruit { // Properties public $name; public $color; // Methods function set_name($name) { $this->name = $name; } function get_name() { return $this->name; } } ?>
. , - .
- . . , , .
new
.
$apple
$banana
Fruit
:
<?php class Fruit { // Properties public $name; public $color; // Methods function set_name($name) { $this->name = $name; } function get_name() { return $this->name; } } $apple = new Fruit(); $banana = new Fruit(); $apple->set_name('Apple'); $banana->set_name('Banana'); echo $apple->get_name(); echo "<br>"; echo $banana->get_name(); ?>
:
Apple Banana
$ this
$this
.
:
<?php class Fruit { public $name; } $apple = new Fruit(); ?>
, $name
? :
1. ( set_name()
$this
):
<?php class Fruit { public $name; function set_name($name) { $this->name = $name; } } $apple = new Fruit(); $apple->set_name("Apple"); ?>
2. ( ):
<?php class Fruit { public $name; } $apple = new Fruit(); $apple->name = "Apple"; ?>
PHP MySQL
PHP .
MySQL - , PHP.
.
MySQL
PHP 5 MySQL, :
- MySQLi
- (PHP Data Objects)
: MySQLi PDO?
MySQLi, PDO :
PDO 12 , MySQLi MySQL.
, , PDO . . MySQLi , .
-, MySQLi API.
. SQL- -.
MySQL
MySQL, :
(- MySQLi):
<?php $servername = "localhost"; $username = "username"; $password = "password"; // $conn = new mysqli($servername, $username, $password); // if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; ?>
( MySQLi):
<?php $servername = "localhost"; $username = "username"; $password = "password"; // $conn = mysqli_connect($servername, $username, $password); // if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; ?>
PDO:
<?php $servername = "localhost"; $username = "username"; $password = "password"; try { $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?>
. PDO (myDB). PDO . , .
, . , :
(- MySQLi):
$conn->close();
( MySQLi):
mysqli_close($conn);
PDO:
$conn = null;
PHP MySQL
, :
- SQL- PHP.
- SQL- .
- .
-
NULL
.
INSERT INTO
MySQL:
INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)
(- MySQLi):
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?>
( MySQLi):
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')"; if (mysqli_query($conn, $sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } mysqli_close($conn); ?>
PDO:
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDBPDO"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')"; // use exec() because no results are returned $conn->exec($sql); echo "New record created successfully"; } catch(PDOException $e) { echo $sql . "<br>" . $e->getMessage(); } $conn = null; ?>
PHP MySQL
SELECT
:
SELECT column_name(s) FROM table_name
*
:
SELECT * FROM table_name
(- MySQLi):
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; } } else { echo "0 results"; } $conn->close(); ?>
SQL-, id
, firstname
lastname
MyGests
. $result
.
num_rows()
, .
, fetch_assoc() , . while()
id
, firstname
lastname
.
, , MySQLi:
( MySQLi):
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; } } else { echo "0 results"; } mysqli_close($conn); ?>
PDO. id
, firstname
lastname
MyGests
HTML:
<?php echo "<table style='border: solid 1px black;'>"; echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>"; class TableRows extends RecursiveIteratorIterator { function __construct($it) { parent::__construct($it, self::LEAVES_ONLY); } function current() { return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>"; } function beginChildren() { echo "<tr>"; } function endChildren() { echo "</tr>" . "\n"; } } $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDBPDO"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $conn->prepare("SELECT id, firstname, lastname FROM MyGuests"); $stmt->execute(); // set the resulting array to associative $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { echo $v; } } catch(PDOException $e) { echo "Error: " . $e->getMessage(); } $conn = null; echo "</table>"; ?>
WHERE
, , .
SELECT column_name(s) FROM table_name WHERE column_name operator value
PHP:
- PHP?
- PHP?
- PHP
- PHP-
- PHP
- PHP
- PHP
- PHP
- JSON PHP
- PHP
- PHP
- MySQL PHP