在php文件的头部使用了namespace 结果报错mysqli不在LearningPHP1中,
结论:如果定义了命名空间,在使用mysqli做连接的时候就要指明mysqli所在的命名空间。
<?php
//localhost/app/php/index.php
echo "IE快捷键 Ctrl+Tab下一标签页 Ctrl+Shift+Tab前一标签页<br/>";
// 1.变量的声明
echo "<h2>1.变量的声明</h2>";
//$x=5; $y=6; $z=$x+$y; echo $z;
//2.变量作用域
echo "<h2>2.变量作用域</h2>";
$x2= 5;
function myTest()
{
//访问全局变量$x需要加上global关键字
//也可以这样访问$x : $GLOBALS['x']
global $x2;
$y=10;
echo "<p>测试函数内变量</p>";
echo "变量x2为:$x2";
echo "<br/>";
echo "变量y为:$y<br/>";
}
myTest();
//3.当一个函数运行结束时,内部变量都会被删除,通过static关键字可以不删除
echo "<h2>3.static关键字</h2>";
function test2()
{
static $x1 = 0;
echo "static x1: $x1<br/>";
$x1++;
}
test2();
test2();
test2();
//4.参数传递
echo "<h2>4.参数传递</h2>";
$x41 = "郑文强";
function myTest3($x41)
{
echo "传入的是:$x41".'<br/>';
echo '函数名为:'. __FUNCTION__ ;
}
myTest3($x41);
//5. echo/print
echo "<h2>5.echo/print</h2>";
/*
echo - 可以输出一个或多个字符串
print - 只允许输出一个字符串,返回值总为 1
echo 是一个语言结构,使用的时候可以不用加括号,也可以加上括号:echo 或 echo()。
*/
echo "I'm about to learn PHP!<br/>";
echo "This ","String ","was ","made ","with multiple parameters.<br/>";
$txt1="Learn PHP";
$txt2="w3cschool.cc";
$cars=array("Volvso","BMW","Toyota");
echo $txt1,"<br/>";
echo "Study PHP at $txt2<br/>";
echo "My car is a $cars[0]";
//6.数据类型
echo "<h3>6.数据类型--整型</h3>";
$x61 = 5985;
var_dump($x61);
echo "<br>";
$x62 = -345; // 负数
var_dump($x62);
echo "<br>";
$x63 = 0x8C; // 十六进制数
var_dump($x63);
echo "<br>";
$x64 = 047; // 八进制数
var_dump($x64);
echo "<h3>数据类型--浮点型</h3>";
$x65= 10.365;
var_dump($x65);
echo "<br>";
$x66 = 2.4e3;
var_dump($x66);
echo "<br>";
$x67 = 8E-5;
var_dump($x67);
echo "<h3>数据类型--数组</h3>";
$cars=array("Volvo","BMW","Toyota");
var_dump($cars);
echo "<h3>数据类型--对象</h3>";
class House
{
var $color;
function House($color="green"){
$this->color = $color;
}
function what_color(){
return $this->color;
}
}
echo "<h3>数据类型--常量 ,第三个参数默认为false,即区分大小写,这里设为true 不区分大小写</h3>";
define("GREETING","欢迎访问 xxoo.com",true);
echo GREETING,"<br/>";
echo greeting;
//7.字符串
echo "<h2>7.字符串 并置运算符</h2>";
$txt71="Hello Beijing ";
$txt72="What A Nice Day";
$txt73 = $txt71.$txt72;
echo $txt73;
/*
strlen($txt73); 字符串长度
strpos() 函数用于在字符串内查找一个字符或一段指定的文本。如果在字符串中找到匹配,
该函数会返回第一个匹配的字符位置。如果未找到匹配,则返回 FALSE
*/
//8.运算符
echo "<h2>8.运算符</h2>";
echo "<h3>取模运算符</h3>";
$x81=10; $y81=6;
echo ( $x81 % $y81 );
echo "<h3>三元运算符</h3>";
$test81 = '菜鸟教程';
$username = isset($test81)?$test81:'nobody';
echo $username,PHP_EOL;
//9.判断IF语句
echo "<h2>9.判断IF语句</h2>";
//判断当前时刻
$h91=date("H");
if($h91<16){
echo "Have A Good Day!";
}else{
echo "Have A Good Night!";
}
//10.Switch 语句
echo "<h2>10.Switch语句</h2>";
$favcolor10="blue";
switch ($favcolor10)
{
case "red":
echo "你喜欢的颜色是红色!";
break;
case "blue":
echo "你喜欢的颜色是蓝色!";
break;
case "green":
echo "你喜欢的颜色是绿色!";
break;
default:
echo "你喜欢的颜色不是 红, 蓝, 或绿色!";
}
//11.数组
echo "<h2>11.数组</h2>";
$cars11=array("Volvo","BMW","Toyota");
echo "I like " . $cars11[0] . ", " . $cars11[1] . " and " . $cars11[2] . ".<br/>";
$arrlength=count($cars11);
for($x=0;$x<$arrlength;$x++)
{
echo $cars[$x];
echo "<br>";
}
$age11=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age11['Peter'] . " years old.";
//遍历关联数组
foreach($age11 as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
//12.魔术常量
echo '该文件位于 " ' . __FILE__ . ' "<br/>';
echo '这是第"'. __LINE__ .'"行<br/>';
//除非是根目录 否则不能包括末尾的斜杠
echo '该文件位于"'. __DIR__ .'"';
echo '命名空间为:"', __NAMESPACE__, '"'; // 输出 "MyProject"
//13.连接数据库
echo "<h2>连接数据库</h2>";
$servername = "www.qyying.cn";
$username = "blog";
$password = "123";
$dbname = "myblog";
//13.1 面向对象创建连接
$conn0 = new mysqli($servername, $username, $password,$dbname);
// 检测连接
if ($conn0->connect_error) {
die("连接失败: " . $conn0->connect_error);
}
echo "面向对象连接成功<br/>";
//13.2 面向过程创建连接
$conn1 = mysqli_connect($servername, $username, $password);
// 检测连接
if (!$conn1) {
die("Connection failed: " . mysqli_connect_error());
}
mysqli_close($conn1);
echo "面向过程连接成功<br/>";
//13.3 PDO创建连接
try {
$conn2 = new PDO("mysql:host=$servername;dbname=myblog", $username, $password);
$conn2=null;
echo "PDO连接成功";
}
catch(PDOException $e)
{
echo "出错啦".$e->getMessage()."<br/>";
}
//14.创建数据表
//使用 sql 创建数据表
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
if ($conn0->query($sql) === TRUE)
{
echo "Table MyGuests created successfully";
}else
{
echo "创建数据表错误: " . $conn0->error;
}
/*
//插入一条记录
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
if ($conn0->query($sql) === TRUE) {
echo "新记录插入成功";
} else {
echo "Error: " . $sql . "<br>" . $conn0->error;
}
//插入多条记录
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', 'mary@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', 'julie@example.com')";
if ($conn0->multi_query($sql) === TRUE) {
echo "新记录插入成功";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
*/
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn0->query($sql);
if ($result->num_rows > 0) {
// 输出每行数据
while($row = $result->fetch_assoc())
{
echo "<br> id: ". $row["id"]. " - Name: ". $row["firstname"]. " " . $row["lastname"];
}
}
else
{
echo "0 个结果";
}
$conn0->close();
?>