PHP入门

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>第一个php程序</title>
</head>

<body>
<script language="php">
    echo ‘Hello World !‘ ;
</script>
<?    echo ‘Hello World !‘ ; ?>
<% echo ‘Hello World !‘ ; %>
<?php
    echo "Hello World !" ; //单行注释
    echo "<p>" ;
    /*
    多行注释
    */
    #shell脚本风格注释
    $boo = true ;
    if ($boo == true)
    {
        echo ‘变量$boo为真‘ ."<br>" ;
    }
    $str = ‘字符串‘ ;
    echo "$str" ;
    echo ‘$str‘ ."<br>" ;
    $arr = array(‘a1‘,‘a2‘,‘a3‘) ;
    echo "当前文件路径: " .__FILE__ ."<br>" ;    //预定义常量
    $str1 = $str ;   //赋值,使用各自内存
    $str2 = & $str1  //引用赋值(相当于另起一名字)
    
?>
<?php
    function fun1($sum)   //定义函数
    {
        return "$sum * $sum = ".$sum * $sum ;    
    }
    echo fun1(10) ."<br>" ;
    $con = mysql_connect("localhost","root","newman");//连接MySQL数据库
    if (!$con)
      {
          die(‘Could not connect: ‘ . mysql_error());
      }
/*    
    if (mysql_query("CREATE DATABASE my_db",$con))
      { //创建数据库
          echo "Database created";
      }
    else
      {
          echo "Error creating database: " . mysql_error();
      }
*/    
    mysql_select_db("my_db", $con); //选取数据库
/*
    $sql = "CREATE TABLE Persons 
    (
        personID int NOT NULL AUTO_INCREMENT, 
        PRIMARY KEY(personID),
        FirstName varchar(15),
        LastName varchar(15),
        Age int
    )";
*/
/*
    mysql_query("INSERT INTO Persons (FirstName, LastName, Age) 
    VALUES (‘Peter‘, ‘Griffin‘, ‘35‘)");
    mysql_query("INSERT INTO Persons (FirstName, LastName, Age) 
    VALUES (‘Glenn‘, ‘Quagmire‘, ‘33‘)");
*/    
    $result = mysql_query("SELECT * FROM Persons 
        WHERE FirstName=‘Peter‘");
    //$result = mysql_query("SELECT * FROM Persons ORDER BY age");
    mysql_query("UPDATE Persons SET Age = ‘36‘
        WHERE FirstName = ‘Peter‘ AND LastName = ‘Griffin‘");
    //mysql_query("DELETE FROM Persons WHERE LastName=‘Griffin‘");
    while($row = mysql_fetch_array($result))
      {
          echo $row[‘FirstName‘] . " " . $row[‘LastName‘] 
            . " " . $row[‘Age‘];
          echo "<br />";
      }
    mysql_close($con);
?>

</body>
</html>

第一次需运行注释内的创建数据库和数据库表,注释掉了向数据库表添加记录代码,以免多次添加。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。