|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
我先把我自己学习PHP的过程做一下概括: 比来一向在研讨PHP与MYSQL,感到PHP与MYSQL交互的函数都是进程化的,固然也有mysqli扩大,面向对象,Java和C#写多了以后,再写PHP,有些不顺应,感到又回到了学C的年月。明天进修了一些函数,纪录上去,以便往后健忘时,可以参考。 说 明函 数 名函 数 详 细函 数 说 明创立数据库毗连mysql_connect()resource mysql_connect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])
示例:$conn = @mysql_connect("localhost", "username", "password") or dir("不克不及毗连到Mysql Server");利用该毗连必需显示的封闭毗连创立数据库毗连mysql_pconnect()resource mysql_pconnect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])
示例:$conn = @mysql_pconnect("localhost", "username", "password") or dir("不克不及毗连到Mysql Server");利用该毗连函数不需求显示的封闭毗连,它相当于利用了毗连池封闭数据库毗连mysql_close()$conn = @mysql_connect("localhost", "username", "password") or die("不克不及毗连到Mysql Server");
@mysql_select_db("MyDatabase") or die("不克不及选择这个数据库,或数据库不存在");
echo "你已毗连到MyDatabase数据库";
mysql_close(); 选择数据库mysql_select_db()boolean mysql_select_db(string db_name [, resource link_id])
$conn = @mysql_connect("localhost", "username", "password") or die("不克不及毗连到Mysql Server");
@mysql_select_db("MyDatabase") or die("不克不及选择这个数据库,或数据库不存在"); 查询MySQLmysql_query()resource mysql_query (string query, [resource link_id])
$linkId = @mysql_connect("localhost", "username", "password") or die("不克不及毗连到Mysql Server");
@mysql_select_db("MyDatabase") or die("不克不及选择这个数据库,或数据库不存在");
$query = "select * from MyTable";
$result = mysql_query($query);
mysql_close();若SQL查询履行胜利,则前往资本标识符,掉败时前往FALSE。若履行更新胜利,则前往TRUE,不然前往FALSE查询MySQLmysql_db_query()resource mysql_db_query(string database, string query [, resource link_id])
$linkId = @mysql_connect("localhost", "username", "password") or die("不克不及毗连到MysqlServer");
$query = "select * from MyTable";
$result = mysql_db_query("MyDatabase", $query);
mysql_close();为了使代码明晰,不保举利用这个函数挪用获得和显示数据mysql_result()mixed mysql_result (resource result_set, int row [, mixed field])
$query = "select id, name from MyTable order by name";
$result = mysql_query($query);
$c_id = mysql_result($result, 0, "id");
$c_name = mysql_result($result, 0, "name");最复杂、也是效力最低的数据获得函数获得和显示数据mysql_fetch_row()array mysql_fetch_row (resource result_set)
$query = "select id, name from MyTable order by name";
$result = mysql_query($query);
while (list($id, $name) = mysql_fetch_row($result)) {
echo("Name: $name ($id) <br />");
}函数从result_set中获得全部数据行,将值放在一个索引数组中。凡是会结使list()函数利用获得和显示数据mysql_fetch_array()array mysql_fetch_array (resource result_set [, int result_type])
$query = "select id, name from MyTable order by name";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row["id"];
$name = $row["name"];
echo "Name: $name ($id) <br />";
}result_type的值有:
MYSQL_ASSOC: 字段名暗示键,字段内容为值
MYSQL_NUM: 数值索引数组,操作与mysql_fetch_ros()函数一样
MYSQL_BOTH: 即作为联系关系数组又作为数值索引数组前往。result_type的默许值。获得和显示数据mysql_fetch_assoc()array mysql_fetch_assoc (resource result_set)
相当于挪用 mysql_fetch_array(resource, MYSQL_ASSOC); 获得和显示数据mysql_fetch_object()object mysql_fetch_object(resource result_set)
$query = "select id, name from MyTable order by name";
while ($row = mysql_fetch_object($result)) {
$id = $row->id;
$name = $row->name;
echo "Name: $name ($id) <br />";
}在操作上与mysql_fetch_array()不异所选择的纪录mysql_num_rows()int mysql_num_rows(resource result_set)
#query = "select id, name from MyTable where id > 65";
$result = mysql_query($query);
echo "有".mysql_num_rows($result)."笔记录的ID大于65";只在肯定select查询所获得的纪录数时才有效。受影响的纪录mysql_affected_rows()int mysql_affected_rows([resource link_id])
$query = "update MyTable set name='CheneyFu' where id>=5";
$result = mysql_query($query);
echo "ID大于等于5的称号被更新了的纪录数:".mysql_affected_rows();该函数获得受INSERT,UPDATE或DELETE更新语句影响的行数获得数据库列表信息mysql_list_dbs()resource mysql_list_dbs([resource link_id])
mysql_connect("localhost", "username", "password");
$dbs = mysql_list_dbs();
echo "Databases: <br />";
while (list($db) = mysql_fetch_rows($dbs)) {
echo "$db <br />";
} 获得数据库名mysql_db_name()string mysql_db_name(resource result_set, integer index)该函数获得在mysql_list_dbs()所前往result_set中位于指定index索引的数据库名获得数据库表列表mysql_list_tables()resource mysql_list_tables(string database [, resource link_id])
mysql_connect("localhost", "username", "password");
$tables = mysql_list_tables("MyDatabase");
while (list($table) = mysql_fetch_row($tables)) {
echo "$table <br />";
}该函数获得database中一切表的表名获得数据库表名mysql_tablename()string mysql_tablename(resource result_set, integer index)
mysql_connect("localhost", "username", "password");
$tables = mysql_list_tables("MyDatabase");
$count = -1;
while (++$count < mysql_numrows($tables)) {
echo mysql_tablename($tables, $count)."<br />";
}该函数获得mysql_list_tables()所前往result_set中位于指定index索引的表名获得字段信息mysql_fetch_field()object mysql_fetch_field(resource result [, int field_offset])
mysql_connect("localhost", "username", "password");
mysql_select_db("MyDatabase");
$query = "select * from MyTable";
$result = mysql_query($query);
$fields = mysql_num_fields($result);
for($count = 0; $count < $fieds; $count++) {
$field = mysql_fetch_field($result, $count);
echo "<p>$field->name $field->type ($field->max_length) </p>";
}前往的对象共有12个对象属性:
name: 字段名
table: 字段地点的表
max_length: 字段的最大长度
not_null: 假如字段不克不及为null,则为1,不然0
primary_key: 假如字段为主键,则为1,不然0
unique_key: 假如字段是独一键,则为1, 不然0
multiple_key: 假如字段为非独一,则为1,不然0
numeric: 假如字段为数值则为1,不然0
blob: 假如字段为BLOB则为1,不然为0
type: 字段的数据类型
unsigned: 假如字段为无符号数则为1,不然为0
zerofill: 假如字段为“零填充”则为1, 不然为0获得查询的字段数mysql_num_fields()integer mysql_num_fields (resource result_set)
$query = "select id, name from MyTable order by name";
$result = mysql_query($query);
echo "这个查询的字段数是:".mysql_num_fields($result)."<br />";前往查询result_set中的字段数获得指定表的一切字段的字段名mysql_list_fields()resource mysql_list_fields (string database_name, string table_name [, resource link_id])
$fields = mysql_list_fields("MyDatabase", "MyTable");
echo "数据库MyDatabase中表MyTable的字段数: ".mysql_num_fields($fields)."<br />"; 获得指定的字段选项mysql_field_flags()string mysql_field_flags (resource result_set, integer field_offset) 获得指定的字段的最大长度mysql_field_len()integer mysql_field_len (resource result_set, integer field_offset)
$query = "select name from MyTable";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo mysql_field_len($result, 0)."<br />";假如mysql_field_len($reseult, 0) = 16777215
那末numer_format(mysql_field_len($result))等于16,777,215获得字段名mysql_field_name()string mysql_field_name (resource result_set, int field_offset)
$query = "select id as PKID, name from MyTable order by name";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo mysql_field_name($result, 0); // Result: PKID 获得字段类型mysql_field_type()string mysql_field_type (resource result_set, int field_offset)
$query = "select id, name from MyTable order by name";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo mysql_field_type($result, 0); // Result: int 获得字段地点表名mysql_field_table()string mysql_field_table (resource result_set, int field_offset)
$query = "select id as PKID, name from MyTable order by name";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo mysql_field_table($result, 0); //
可以说你的马步已经扎的差不多了,接下来就要开始练把势的时候了,如果有条件的话,用笔或者打印一个简易的PHP手册在身上,时不时的摸出来看看,记得,去WC也不能放过(^2^)。 |
|