码迷,mamicode.com
首页 > Web开发 > 详细

PHP入门

时间:2015-02-25 18:29:58      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

<!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>

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

PHP入门

标签:

原文地址:http://www.cnblogs.com/ht-beyond/p/4299937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!