PHP4 Óë MySQL Êý¾Ý¿â²Ù×÷º¯ÊýÏê½â

5年以前  |  阅读数:477 次  |  编程语言:PHP 

I ˵PHP¾Í²»Äܲ»ÌaMySQL£¬¶øÒª½²MySQL£¬ÄÇôPHPÒ²ÊDZØȻҪ±»ÌaÆð¡£PHPµÄѸËÙaÈÆð£¬Àe²»¿ªMySQL£¬¶øMySQLµÄ¹a*ºÓ¦Óã¬Ò²ÓePHPÐÝÆÝÏa¹Ø¡£

ÏÂÃaeÏeϸÖÎoPHP4ÖÐÓeMySQLÏa¹Ø²Ù×÷µÄº¯Êý£¨¹²32¸o£¬¿ªÍ¶¼Îªmysql_£(C)£º

<1>. Á¬½ÓÊý¾Ý¿a*þÎñÆ÷(database server)µÄº¯Êý£¨2¸o£(C)£º

(1).mysql_connect()
¸ñʽ£ºint mysql_connect(string [hostname] [ort],string [username],string [password]);

²ÎÊýÖеÄport²ÎÊý±iʾÊý¾Ý¿a*þÎñÆ÷µÄ¶Ë¿ÚºÅ£¬Ò»°aÓÃËuµÄĬÈ϶˿ںžͿÉÒÔÁË¡£
Èç¹u²»ÌiÈκβÎÊý£¬ÔoĬÈϵÄhostnameΪlocalhost£¬usernameΪroot£¬passwordΪ¿Õ¡£

º¯ÊýÖ´Ðгɹ¦£¬µ»ØÒ»¸oint ÀaÐ͵ÄÁ¬½ÓºÅ(link_identifier)£¬Ö´ÐÐʧ°Ü£¬µ»ØfalseÖµ¡£

Àý×Ó£º

$connect = mysql_connect("localhost","user","password");
if($connect) echo "Connect Successed!"; //Á¬½Ó³É¹¦£¬ÏÔʾConnect Successed!
else echo "Connect Failed!"; //Á¬½Óʧ°Ü£¬ÏÔʾConnect Failed!

?>

ÔÚÉÏÀýÖУ¬Èçmysql_connect()Ö´ÐÐʧ°Ü£¬½«ÏÔʾϵͳµÄ´iÎoÌaʾ£¬¶øºo¼ÌÐøÍuÏÂÖ´ÐС£ÄÇ£¬¸ÃÈçºÎÆÁ±ÎÕaÐ(C)ϵͳµÄ´iÎoÌaʾ²¢ÔÚʧ°Üºo½aÊø³ÌÐo£¿
ÔÚMySQLÖУ¬ÔÊÐiÔÚÊý¾Ý¿aº¯Êý֮ǰ¼ÓÉÏ@*uºÅ£¬ÆÁ±ÎϵͳµÄ´iÎoÌaʾ£¬Í¬Ê±ÓÃdie()º¯Êý¸ø³o¸uÒ×Ài½aµÄ´iÎoÌaʾ£¬È»ºodie()º¯Êý½«×Ô¶¯Í˳o³ÌÐo¡£

ÉÏÀý¿ÉÒÔ¸ÄΪ£º

$connect = @mysql_connect("localhost","user","password") or die ("Unable to connect database server!");

?>

Èçmysql_connect()Ö´ÐÐʧ°Ü£¬½«ÏÔʾ Unable to connect database server!ºo£¬Í˳o³ÌÐo¡£

(2).mysql_pconnect()
¸ñʽ£ºint mysql_pconnect(string [hostname] [ort],string [username],string [password]);
´Ëº¯ÊýÓe(1)µÄmysql_connect()»u±¾Ïaͬ£¬Çø±ðÔÚÓÚ£º

--------- µ±Êý¾Ý¿a²Ù×÷½aÊøÖ®ºo £¬ÓÉ(1)µÄmysql_connect()½¨Á¢µÄÁ¬½Ó½«×Ô¶¯¹Ø±Õ£¬¶ø(2)µÄmysql_pconnect()½¨Á¢µÄÁ¬½Ó½«¼ÌÐø´aeÔÚ£¬ÊÇÒ»ÖÖÎȹ̳־õÄÁ¬½Ó¡£
--------- ÔÚ(2)µÄmysql_pconnect()£¬Ã¿´ÎÁ¬½ÓÇ°£¬¶¼»a¼i²eÊÇ*ñÓÐʹÓÃͬÑuµÄhostname£¬use£¬passwordµÄÁ¬½Ó£¬Èç¹uÓУ¬ÔoÖ±½ÓʹÓÃÕa¸oÁ¬½ÓºÅ¡£
--------- (1)µÄmysql_connect()½¨Á¢µÄÁ¬½Ó¿ÉÒÔÓÃmysql_close()¹Ø±Õ£¬¶ø(2)µÄmysql_pconnect()²»ÄÜÓÃmysql_close()À´¹Ø±Õ¡£

<2>.¹Ø±ÕÊý¾Ý¿aÁ¬½Óº¯Êý(1¸o)£º

mysql_close()
¸ñʽ£ºint mysql_close(int link_identifier);
¹Ø±ÕÓÉmysql_connect()º¯Êý½¨Á¢µÄÁ¬½Ó£¬Ö´Ðгɹ¦£¬µ»ØtureÖµ£¬Ê§°ÜÔoµ»ØfalseÖµ¡£

Àý×ÓÈçÏ£º

$connect = @mysql_connect("hostname","user","password") or die("Unable to connect database server!");

$close = @mysql_close($connect) or die ("Unable to close database server connect!");

?>

×¢£ºmysql_close()²»ÄܹرÕÓÉmysql_pconnect()º¯Êý½¨Á¢µÄÁ¬½Ó¡£

<3>.Ñ¡ÔñÊý¾Ý¿aº¯Êý(1¸o)£º

mysql_select_db()
¸ñʽ£ºint mysql_select_db(string database name , int link_identifier);
Ñ¡ÔñÖ¸¶¨µÄdatabase name £¬³É¹¦£¬µ»Ø1¸oÕaeÖµ£¨True£(C)£¬Ê§°Ü£¬Ôoµ»Ø1¸oFalseÖµ
Àý×Ó1£º
$select = mysql_select_db(\'forum\' , $connect);
if($select)
{echo "connect db forum successed!";}
else
{echo "connect db forum failed!";}
?>

Àý×Ó2£º
$select = mysql_select_db("forum",$connect) or die("Can not connect this DB!");
?>

×¢£º´Ëº¯ÊýÏaµ±ÓÚÔÚMySQLÖеÄUSEÓi¾a£ºÈç USE forum

<4>.SQL²eѯº¯Êý(2¸o)£º

1¡¢mysql_query()
¸ñʽ£ºint mysql_query(string sqlquery , int link_identifier);
ÏoþÎñÆ÷¢Ò»¶Î±e×¼SQLÓi¾aÇeÇo¡£Èç¹uʧ°Ü£¬Ôo*µ»ØÒ»FalseÖµ¡£

Àý×Ó£º
$connect = mysql_connect($hostname,$user,$pwd);
$select = mysql_select_db($dbname,$connect);
$query = mysql_query($sql , $connect);
if($query) echo "Successed !";
else echo "Failed !";
?>
´Ëº¯ÊýÒ»¶¨ÒªÓemysql_select_db()º¯ÊýÅaºÏʹÓ㬵¥¶ÀʹÓÃËu¾ÍûÓÐÒaÒaÁË£¡

2¡¢mysql_db_query()
¸ñʽ£ºint mysql_db_query(string database , string sqlquery , int link_identifier);

Ôڴ˺¯ÊýÖбØÐeÖ¸¶¨Êý¾Ý¿aÃudatabaseºÍSQLÓi¾asqlquery£¬Èçʧ°ÜÔo*µ»ØFalse¡£

Àý×Ó£º

$connect = mysql_connect($hostname , $user , $pwd);
$query = mysql_db_query($dbname , $sql , $connect);
if($query) echo "Successed !";
else echo "Failed !";
?>

mysql_db_query()Óemysql_query()µÄÇø±ð¾ÍÔÚÓÚÇ°Õß¿ÉÒÔ²»ÓÃʹÓÃmysql_select_db()À´Ñ¡ÔñÊý¾Ý¿adatabase£¬¶øÔÚÖ´ÐÐSQLÓi¾aµÄͬʱ£¬½øÐÐÑ¡ÔñÊý¾Ý¿a¡£ <5>.Êý¾Ý¿a¼Ç¼²Ù×÷º¯Êý(5¸o)£º

1¡¢mysql_fetch_array()
¸ñʽ£ºarray mysql_fetch_array(int query);

Ö´Ðгɹ¦£¬µ»Ø1¸oÊý×e£¬¸ÃÊý×e±£´aeÓÐÏÂÒ»Ìo¼Ç¼µÄÖµ£¬ÈçÖ´ÐÐʧ°Ü£¬Ôoµ»ØFalseÖµ¡£
*µ»ØµÄÊý×e¼È¿ÉÒÔÓÃϱeÀ´±iʾ£¬Ò²¿ÉÒÔÓÃ×Ö¶ÎÃuÀ´±iʾ¡£

Àý×Ó£º
$query = mysql_query($sql , $connect);
while($arrary = mysql_fetch_array($query))
{

echo $array[column1]." | ".$array[column2];
//echo $array[0]." | ".$array[1];

}
?>
Note : Êý×eµÄϱeÊÇ´Ó0¿ªÊ¼µÄ£¡

2¡¢mysql_fetch_row()
¸ñʽ£ºarray = mysql_fetch_row(int query);

Óe1µÄmysql_fetch_array()º¯Êý¹¦ÄÜ»u±¾Ïaͬ¡£Çø±ðÔÚÓÚmysql_fetch_row()Ö»ÄÜÒÔÊý×eϱeÀ´±iʾ¡£
³É¹¦µ»Ø1¸oÊý×e£¬Ê§°Üµ»ØFalseÖµ¡£

Àý×Ó£º
$query = mysql_query($sql , $connect);
while($row = mysql_fetch_row($query))
{
echo $row[0] . " | " . $row[1] . "
";
}
?>
Note : mysql_fetch_row()º¯ÊýÖ»ÄÜÓÃÊý×eϱeÀ´±iʾ£¬²¢´Ó0¿ªÊ¼¡£
Ái£ºmysql_fetch_row()±Èmysql_fetch_array()Ö´ÐÐËٶȿi£¬²¢ÇÒ¶¼ÊǶÔÏÂÒ»ÐÐÊý¾Ý½øÐжÁÈ¡¡£

3¡¢mysql_result()
¸ñʽ£ºint mysql_result(int query , int row , string filedname);

ÔÚmysql_result()ÖУ¬²ÎÊýrow±ØÐe´Ó0¿ªÊ¼£¬²ÎÊýfiledname±ØÐeÊÇÕaeʵµÄ×Ö¶ÎÃu£¬²»ÄÜÓÃϱeÀ´±iʾ¡£
Ö´Ðгɹ¦£¬µ»ØÓÉÊý¾Ý¿aÖÐÈ¡³oµÄ×ֶεÄÊýÖµ£¬Ê§°ÜÔoµ»ØFalseÖµ¡£

Àý×Ó£º
$query = mysql_query($sql , $connect);
echo mysql_result($query , 0 , "column1")."
";
echo mysql_result($query , 1, "column1")."
";
echo mysql_result($query , 2, "column1")."
";
?>

Note : ´Ëº¯Êý¹¦ÄÜÉÙ£¬µ«Ê¹ÓÃ*½±a¡£

4¡¢mysql_fetch_object()
¸ñʽ£º object mysql_fetch_object(int query)

ÄܶÔÖ¸¶¨µÄ×ֶνøÐÐÑ­»¶ÁÈ¡£¬Ö´Ðгɹ¦£¬½«ÒÔ¶ÔÏoobjectµÄÐÎʽµ»ØÊýÖµ£¬Ê§°ÜÔo*µ»ØFalseÖµ¡£

Àý×Ó£º
$query = mysql_query($sql , $connect);
while($object = mysql_fetch_object($query))
{
echo $object->column1 . "
";
echo $object->column2 . "
";
echo $object->column3 . "
";
}
?>

Note : mysql_fetch_object()º¯ÊýÖ´Ðгɹ¦ºo*µ»ØµÄÊÇ1¸o¶ÔÏoobject £¡
²Ù×÷ÈçÏ£º
$object->×Ö¶ÎÃu

5¡¢mysql_data_seek()
¸ñʽ£ºint mysql_data_seek(int row , int query);
Òƶ¯Óαeµ½Ö¸¶¨µÄÐÐ(row_number)
Ö´Ðгɹ¦£¬µ»ØÕaeÖµ£¬Ê§°Ü£¬µ»ØFalseÖµ¡£
´Ëº¯Êý¿ÉÒÔÓemysql_fetch_array()»omysql_fetch_row()ÅaºÏʹÓ㬼´ÔÚʹÓÃmysql_data_seek()º¯ÊýÖ®ºo£¬¾Í¿ÉÒÔÓÃmysql_fetch_array()»omysql_fetch_row()º¯ÊýÀ´ÏÔʾָ¶¨µÄÐÐÁË¡£

Àý×Ó£º
$query = mysql_query($sql , $connect);
$seek = mysql_data_seek($query , 2);
$arrary = mysql_fetch_array($query);
echo $array[column1]."
";
echo $array[column2]."
";
?>

<6>.Êý¾Ý¿a¼¶databaseµÄ²Ù×÷º¯Êý(2¸o)£º

1¡¢mysql_create_db()
¸ñʽ£ºint mysql_create_db(string database name , int link_identifier);

ͨ¹ý³ÌÐoÀ´½¨Á¢1¸oÊý¾Ý¿adatabase£¬µ±È»ÄaÒ²¿ÉÒÔÓÃmysql_query()»oÕßmysql_db_query()º¯ÊýÀ´½¨Á¢»oɾ³ýdatabase

µ«ÎÒÃÇ¿ÉÒÔÓÃÕa¸oº¯Êý¸uΪ½±aµØ½¨Á¢1¸odatabase¡£
³É¹¦
µ»Ø1¸oÕaeÖµ£¬Ê§°Ü£¬Ôo*µ»Ø1¸ofalse¡£

Àý×Ó£º

$connect = mysql_connect("$hostname","$user","$pwd");
$create = mysql_create_db("dbtest" , $connect);
if($create) echo "create database dbtest successed!";
else echo "create database dbtest failed!";

?>

2¡¢mysql_drop_db()
¸ñʽ£ºint mysql_drop_db(string database name , int link_identifier);

ͨ¹ý³ÌÐoÀ´É¾³ý1¸oÊý¾Ý¿adatabase¡£

µ«ÎÒÃÇ¿ÉÒÔÓÃÕa¸oº¯Êý¸uΪ½±aµØɾ³ý1¸odatabase¡£
³É¹¦
µ»Ø1¸oÕaeÖµ£¬Ê§°Ü£¬Ôo*µ»Ø1¸ofalse¡£

Àý×Ó£º

$connect = mysql_connect("$hostname","$user","$pwd");
$create = mysql_drop_db("dbtest" , $connect);
if($create) echo "drop database dbtest successed!";
else echo "drop database dbtest failed!";

?>

Note : ÈçÓÃmysql_query()»omysql_db_query()£¬ÔoSQLÓi¾aӦΪ£º
(1)create database dbtest
(2)drop database dbtest 7) Êý¾Ý¿aÐÅÏ¢º¯Êý£¨2¸o£(C)£º

1¡¢mysql_fetch_field()
¸ñʽ£ºobject mysql_fetch_field(int query , int [field_offset]);

µ»Ø1¸o¶ÔÏo£¬¼´Ò»¹þÏ£±i£¬Ï±eÓУº
table : ±iÃu
name : ×Ö¶ÎÃu
max_length : ¸Ã×ֶεÄ×i´o³¤¶È
not_null : ×Ö¶ÎΪnot nullÔo
µ»Ø1£¬ñÔoµ»Ø0
primary_key : ×Ö¶ÎΪprimary keyÔoµ»Ø1£¬ñÔoµ»Ø0
unique_key : ×Ö¶ÎΪunique keyÔo
µ»Ø1£¬ñÔoµ»Ø0
multiple_key : ×Ö¶ÎΪÇunique keyÔoµ»Ø1£¬ñÔoµ»Ø0
numeric : ×Ö¶ÎΪnumericÔoµ»Ø1£¬ñÔoµ»Ø0
blob : ×Ö¶ÎΪblobÔo
µ»Ø1£¬ñÔoµ»Ø0
type : ×ֶεÄÀaÐÍ
unsigned : ×Ö¶ÎΪunsignedÔoµ»Ø1£¬ñÔoµ»Ø0
zerofill : ×Ö¶ÎΪzero filledÔo
µ»Ø1£¬ñÔoµ»Ø0

ÒýÓøñʽΪ£º¶ÔÏoÃu->ϱeÃu

ʹÓô˺¯Êý¿ÉÒԵõ½±iÃu¡¢×Ö¶ÎÃu¡¢ÀaÐÍ.......

Àý×Ó£º

$query = mysql_query($sql , $connect);
while($object = mysql_fetch_field($query))
{
echo "table name : ".$object->table."
";
echo "field name : ".$object->name."
";
echo "primary key : ".$object->primary_key."
";
echo "not null : ".$object->not_null."
";
echo "field type : ".$object->type."
";
echo "field max length : ".$object->max_length."
";
}
?>

Note : ¹þÏ£±iµÄÊÇ´Ó0×ø±e¿ªÊ¼µÄ£¬¼´µÚÒ»¸o×Ö¶ÎΪ¹þÏ£±iÖеÄ0Ïi¡£
Èç¹uÎÒÃÇÏeÖ±½ÓµÃµ½¹þÏ£±iµÄµÚÈýÏi¼´µÚÈý¸o×ֶεÄÐÅÏ¢£¬¿ÉÓÃÈçϸñʽ£º
$query = mysql_query($sql , $connect);
$object = mysql_fetch_field($query , 2);
echo "table name : ".$object->table."
";
echo "field name : ".$object->name."
";
echo "primary key : ".$object->primary_key."
";
echo "not null : ".$object->not_null."
";
echo "field type : ".$object->type."
";
echo "field max length : ".$object->max_length."
";
?>

ÆaʵÕaÒ²¿ÉÒÔͨ¹ýÏÂÃaeÕa¸oº¯ÊýÀ´´iµ½Í¬ÑuµÄÄ¿µÄ¡£

2¡¢mysql_field_seek()
¸ñʽ£ºint mysql_field_seek(int $query , int field_offset);

½«ÓαeÒƵ½Ö¸¶¨µÄ×Ö¶ÎÉÏ¡£
Àý×Ó£º

$query = mysql_query($sql , $connect);
$seek = mysql_field_seek($query , 2);
$object = mysql_fetch_field($query);
echo "table name : ".$object->table."
";
echo "field name : ".$object->name."
";
echo "primary key : ".$object->primary_key."
";
echo "not null : ".$object->not_null."
";
echo "field type : ".$object->type."
";
echo "field max length : ".$object->max_length."
";
?>

ÕaÑuÒ²´iµ½ÓeÉÏÀýͬÑuµÄÒªÇo¡£

  1. È¡Êý¾Ý¿aÃuºÍ±iÃu£¨2¸o£(C)£º

1¡¢mysql_list_dbs()
¸ñʽ£º int mysql_list_dbs(int link_identifier);
È¡µÃËuÓпÉÓõÄÊý¾Ý¿aÃu(database name)¡£

Àý×Ó£º

$connect = mysql_connect($host , $usr ,$pwd);
$dbs = mysql_list_dbs($connect);
$rows = mysql_num_rows($dbs);
echo "database total : ".$rows;
$i = 0;
while($i<$rows)
{
$db_name[$i] = mysql_tablename($dbs , $i);
echo $db_name[$i];
$i++;
}
?>
¼´¿ÉÒÀ´ÎÏÔʾ³oMySQLÖÐËuÓеÄÊý¾Ý¿aÃu×Ö£¨database name £(C)¡£
Note : Ïaµ±ÓÚMySQLÖÐµÄ show databasesÃuÁi

2¡¢mysql_list_tables()
¸ñʽ£ºint mysql_list_tables(string database name);
ÏÔʾ¸ÃÊý¾Ý¿aÏÂËuÓеıiµÄÃu×ÖTable name¡£

Àý×Ó£º

$connect = mysql_connect($host , $usr , $pwd);
$tables = mysql_list_tables("mysql");
$rows = mysql_num_rows($tables);
echo "Table total : ".$rows;
$i = 0;
while($i<$rows)
{
$table_name[$i] = mysql_tablename($tables , $i);
echo $table_name[$i];
$i++;
}

?>

¼´¿ÉÒÀ´ÎÏÔʾ³omysqlÏÂËuÓеıiµÄÃu×Ö
Note : Ïaµ±ÓÚMySQLÖÐµÄ show tablesÃuÁi(ÏÈÒªÓà use mysqlÃuÁiÑ¡ÖÐ1¸oÊý¾Ý¿a)
£¨³o´¦£º*çÉÁ£(C)

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例