您的位置:首页 > 编程语言 > PHP开发

PHP+MySQL项目开发——留言本开发笔记1

2014-03-10 14:12 405 查看
在本项目的开发过程中遇到一些不熟悉的函数,总结于此:

这个简单的例子演示如何连接数据库,执行查询语句,打印返回结果集和断开数据库等一系列基本的 MySQL 操作。

<?php

    /* 连接选择数据库 */

    $link =mysql_connect("mysql_host","mysql_user","mysql_password")

        or die("Could not connect : " .mysql_error());

    print "Connected successfully";

    mysql_select_db("my_database")
or die("Could not select database");

    /* 执行 SQL 查询 */

    $query ="SELECT * FROM my_table";

    $result =mysql_query($query)
or die("Query failed : ". mysql_error());

    /* 在 HTML 中打印结果 */

    print "<table>\n";

    while ($line =mysql_fetch_array($result,MYSQL_ASSOC))
{

        print "\t<tr>\n";

        foreach ($line as$col_value)
{

            print "\t\t<td>$col_value</td>\n";

        }

        print "\t</tr>\n";

    }

    print "</table>\n";

    /* 释放资源 */

    mysql_free_result($result);

    /* 断开连接 */

    mysql_close($link);
?>



目录

mysql_affected_rows -- 取得前一次 MySQL 操作所影响的记录行数

mysql_change_user -- 改变活动连接中登录的用户

mysql_client_encoding -- 返回字符集的名称

mysql_close -- 关闭 MySQL 连接
mysql_connect -- 打开一个到 MySQL 服务器的连接
mysql_create_db -- 新建一个 MySQL 数据库
mysql_data_seek -- 移动内部结果的指针
mysql_db_name -- 取得结果数据
mysql_db_query -- 发送一条 MySQL 查询
mysql_drop_db -- 丢弃(删除)一个 MySQL 数据库
mysql_errno -- 返回上一个 MySQL 操作中的错误信息的数字编码
mysql_error -- 返回上一个 MySQL 操作产生的文本错误信息
mysql_escape_string -- 转义一个字符串用于 mysql_query
mysql_fetch_array -- 从结果集中取得一行作为关联数组,或数字数组,或二者兼有
mysql_fetch_assoc -- 从结果集中取得一行作为关联数组
mysql_fetch_field -- 从结果集中取得列信息并作为对象返回
mysql_fetch_lengths -- 取得结果集中每个输出的长度
mysql_fetch_object -- 从结果集中取得一行作为对象
mysql_fetch_row -- 从结果集中取得一行作为枚举数组
mysql_field_flags -- 从结果中取得和指定字段关联的标志
mysql_field_len -- 返回指定字段的长度
mysql_field_name -- 取得结果中指定字段的字段名
mysql_field_seek -- 将结果集中的指针设定为制定的字段偏移量
mysql_field_table -- 取得指定字段所在的表名
mysql_field_type -- 取得结果集中指定字段的类型
mysql_free_result -- 释放结果内存
mysql_get_client_info -- 取得 MySQL 客户端信息
mysql_get_host_info -- 取得 MySQL 主机信息
mysql_get_proto_info -- 取得 MySQL 协议信息
mysql_get_server_info -- 取得 MySQL 服务器信息
mysql_info -- 取得最近一条查询的信息
mysql_insert_id -- 取得上一步 INSERT 操作产生的 ID
mysql_list_dbs -- 列出 MySQL 服务器中所有的数据库
mysql_list_fields -- 列出 MySQL 结果中的字段
mysql_list_processes -- 列出 MySQL 进程
mysql_list_tables -- 列出 MySQL 数据库中的表
mysql_num_fields -- 取得结果集中字段的数目
mysql_num_rows -- 取得结果集中行的数目
mysql_pconnect -- 打开一个到 MySQL 服务器的持久连接
mysql_ping -- Ping 一个服务器连接,如果没有连接则重新连接
mysql_query -- 发送一条 MySQL 查询
mysql_real_escape_string -- 转义 SQL 语句中使用的字符串中的特殊字符,并考虑到连接的当前字符集
mysql_result -- 取得结果数据
mysql_select_db -- 选择 MySQL 数据库
mysql_stat -- 取得当前系统状态
mysql_tablename -- 取得表名
mysql_thread_id -- 返回当前线程的 ID
mysql_unbuffered_query -- 向 MySQL 发送一条 SQL 查询,并不获取和缓存结果的行

mysql_connect(PHP 3, PHP 4 )

mysql_connect -- 打开一个到 MySQL 服务器的连接

说明resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]])

如果成功则返回一个 MySQL 连接标识,失败则返回 FALSE。

mysql_connect() 建立一个到 MySQL 服务器的连接。当没有提供可选参数时使用以下默认值:server = 'localhost:3306',username = 服务器进程所有者的用户名,password = 空密码。

server 参数可以包括端口号。例如 "hostname:port" 或者是到本地套接字的路径,例如本机上的 ":/path/to/socket"。

注: 无论指定 "localhost" 或者 "localhost:port" 作为 server,MySQL 客户端库将覆盖之并尝试连接到本地套接字(Windows 中的名字管道)。如果希望使用 TCP/IP 连接,用 "127.0.0.1" 替代 "localhost"。如果 MySQL 客户端库试图连接到错误的本地套接字,则应该在 PHP 配置中将 mysql.default_host 设为正确的路径并使 server 字段为空。

":port" 的支持是 PHP 3.0B4 起加入的。

":/path/to/socket" 的支持是 PHP 3.0.10 起加入的。

可以在函数名前加上 @ 来抑制失败时产生的错误信息。

如果用同样的参数第二次调用 mysql_connect(),将不会建立新连接,而将返回已经打开的连接标识。参数 new_link 改变此行为并使 mysql_connect() 总是打开新的连接,甚至当 mysql_connect() 曾在前面被用同样的参数调用过。参数 client_flags 可以是以下常量的组合:MYSQL_CLIENT_COMPRESS,MYSQL_CLIENT_IGNORE_SPACE 或者 MYSQL_CLIENT_INTERACTIVE。

注: new_link 参数自 PHP 4.2.0 起可用。

client_flags 参数自 PHP 4.3.0 起可用。

一旦脚本结束,到服务器的连接就会被关闭。除非之前已经调用了 mysql_close() 来关闭它。

MySQL 连接例子

<?php

    $link = mysql_connect("localhost","mysql_user","mysql_password")

        or die("Could not connect: " .mysql_error());

    print ("Connected successfully");

    mysql_close($link);
?>
mysql_fetch_row

(PHP 3, PHP 4 )

mysql_fetch_row -- 从结果集中取得一行作为枚举数组

说明

array mysql_fetch_row ( resource result)

返回根据所取得的行生成的数组,如果没有更多行则返回 FALSE。

mysql_fetch_row() 从和指定的结果标识关联的结果集中取得一行数据并作为数组返回。每个结果的列储存在一个数组的单元中,偏移量从 0 开始。

依次调用 mysql_fetch_row() 将返回结果集中的下一行,如果没有更多行则返回 FALSE。

注: 该函数返回的字段名是大小写敏感的。

list

(PHP 3, PHP 4 )

list -- 把数组中的值赋给一些变量

说明

void list ( mixed ...)

像 array() 一样,这不是真正的函数,而是语言结构。list() 用一步操作给一组变量进行赋值。

注: list() 仅能用于数字索引的数组并假定数字索引从 0 开始。 



例子 1. list() 例子

<?php

$info = array('coffee','brown','caffeine');

// Listing all the variables
list($drink,$color,$power)
= $info;

print "$drink is $color and $power makes it special.\n";

// Listing some of them
list($drink, ,$power) =$info;

print "$drink has $power.\n";

// Or let's skip to only the third one
list( , , $power) =$info;

print "I need $power!\n";

?>


例子 2. 使用 list() 的例子

<table>

<tr>

  <th>Employee name</th>

  <th>Salary</th>

</tr>

<?php

$result = mysql_query("SELECT id, name, salary FROM employees",$conn);

while (list ($id,$name,$salary) =mysql_fetch_row
($result)) {

    print (" <tr>\n".

           "  <td><a href=\"info.php?id=$id\">$name</a></td>\n".//传递ID参数

           "  <td>$salary</td>\n".

           " </tr>\n");

}

?>

</table>
警告
list() 从最右边一个参数开始赋值。如果你用单纯的变量,不用担心这一点。但是如果你用了具有索引的数组,通常你期望得到的结果和在list() 中写的一样是从左到右的,但实际上不是。是以相反顺序赋值的。 

例子 3. 在 list() 中使用数组索引

<?php

$info = array('coffee','brown','caffeine');

list($a[0],$a[1],$a[2])
=$info;
var_dump($a);
?>
产生如下输出(注意单元顺序和 list() 语法中所写的顺序的比较):

array(3) {
[2]=>
string(8) "caffeine"
[1]=>
string(5) "brown"
[0]=>
string(6) "coffee"
}

nl2br  在HTML中的每一个回车前加上“<br/>”注意:nl2br中间是小写的L不是数字1

(PHP 3, PHP 4 )

nl2br -- Inserts HTML line breaks before all newlines in a string 

Description

string nl2br ( string string)

Returns string with '<br />' inserted before all newlines.

注: Starting with PHP 4.0.5, nl2br() is now XHTML compliant. All versions before 4.0.5 will return string with '<br>' inserted before newlines instead of '<br />'.

例子 1. using nl2br()

<?php

echo nl2br("foo isn't\n bar");

?>

this will output :

foo isn't<br />

bar

stripslashes 去掉字符串中的"/",addslashes()加上"/"

(PHP 3, PHP 4 )

stripslashes -- Un-quote string quoted with addslashes()

Description

string stripslashes ( string str)

Returns a string with backslashes stripped off. (\' becomes ' and so on.) Double backslashes (\\) are made into a single backslash (\).

An example use of stripslashes() is when the PHP directive magic_quotes_gpc is on (it's on by default), and you aren't inserting this data into a place (such as a database) that requires escaping. For example, if you're simply outputting data straight from
an HTML form.

例子 1. A stripslashes() example

<?php
$str = "Is your name O\'reilly?";

// Outputs: Is your name O'reilly?
echo stripslashes($str);
?>

htmlspecialchars 将HTML特殊字符转换为其实体

(PHP 3, PHP 4 )

htmlspecialchars -- Convert special characters to HTML entities

Description

string htmlspecialchars ( string string [, int quote_style [, string charset]])

Certain characters have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. This function returns a string with some of these conversions made; the translations made are those most useful for everyday
web programming. If you require all HTML character entities to be translated, use htmlentities() instead.

This function is useful in preventing user-supplied text from containing HTML markup, such as in a message board or guest book application. The optional second argument, quote_style, tells the function what to do with single and double quote characters. Thedefault
mode
, ENT_COMPAT, is the backwards compatible mode which only translates the double-quote character and leaves the single-quote untranslated. IfENT_QUOTES
is set, both single and double quotes are translated and ifENT_NOQUOTES is set neither single nor double quotes are translated.

The translations performed are:

'&' (ampersand) becomes '&'

'"' (double quote) becomes '"' when ENT_NOQUOTES is not set.

''' (single quote) becomes ''' only when ENT_QUOTES is set.

'<' (less than) becomes '<'

'>' (greater than) becomes '>' 

例子 1. htmlspecialchars() example

<?php

$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);

echo $new; // <a href='test'>Test</a>

?>

Note that this function does not translate anything beyond what is listed above. For full entity translation, see htmlentities(). Support for the optional second argument was added in PHP 3.0.17 and PHP 4.0.3.

The third argument charset defines character set used in conversion. The default character set is ISO-8859-1. Support for this third argument was added in PHP 4.1.0.

Following character sets are supported in PHP 4.3.0 and later.

表格 1. Supported charsets

CharsetAliasesDescription
ISO-8859-1ISO8859-1Western European, Latin-1
ISO-8859-15ISO8859-15Western European, Latin-9. Adds the Euro sign, French and Finnish letters missing in Latin-1(ISO-8859-1).
UTF-8 ASCII compatible multi-byte 8-bit Unicode.
cp866ibm866, 866DOS-specific Cyrillic charset. This charset is supported in 4.3.2.
cp1251Windows-1251, win-1251, 1251Windows-specific Cyrillic charset. This charset is supported in 4.3.2.
cp1252Windows-1252, 1252Windows specific charset for Western European.
KOI8-Rkoi8-ru, koi8rRussian. This charset is supported in 4.3.2.
BIG5950Traditional Chinese, mainly used in Taiwan.
GB2312936Simplified Chinese, national standard character set.
BIG5-HKSCS Big5 with Hong Kong extensions, Traditional Chinese.
Shift_JISSJIS, 932Japanese
EUC-JPEUCJPJapanese 
注: Any other character sets are not recognized and ISO-8859-1 will be used instead.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  MySQL 笔记 php