PHP 操作MYSQL基础知识

WBOY
Freigeben: 2016-07-25 08:52:13
Original
885 Leute haben es durchsucht
  1. mysql_connect()
  2. resource mysql_connect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])
  3. 示例:$conn = @mysql_connect("localhost", "username", "password") or dir("不能连接到Mysql Server");
  4. 使用该连接必须显示的关闭连接
复制代码

建立数据库连接

  1. mysql_pconnect()
  2. resource mysql_pconnect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])
  3. 示例:$conn = @mysql_pconnect("localhost", "username", "password") or dir("不能连接到Mysql Server");
  4. 使用该连接函数不需要显示的关闭连接,它相当于使用了连接池
复制代码

关闭数据库连接

  1. mysql_close()
  2. $conn = @mysql_connect("localhost", "username", "password") or die("不能连接到Mysql Server");
  3. @mysql_select_db("MyDatabase") or die("不能选择这个数据库,或数据库不存在");
  4. echo "你已经连接到MyDatabase数据库";
  5. mysql_close();
复制代码

选择数据库

  1. mysql_select_db()
  2. boolean mysql_select_db(string db_name [, resource link_id])
  3. $conn = @mysql_connect("localhost", "username", "password") or die("不能连接到Mysql Server");
  4. @mysql_select_db("MyDatabase") or die("不能选择这个数据库,或数据库不存在");
复制代码

查询MySQL

  1. mysql_query()
  2. resource mysql_query (string query, [resource link_id])
  3. $linkId = @mysql_connect("localhost", "username", "password") or die("不能连接到Mysql Server");
  4. @mysql_select_db("MyDatabase") or die("不能选择这个数据库,或者数据库不存在");
  5. $query = "select * from MyTable";
  6. $result = mysql_query($query);
  7. mysql_close();
  8. 若SQL查询执行成功,则返回资源标识符,失败时返回FALSE。若执行更新成功,则返回TRUE,否则返回FALSE
复制代码

查询MySQL

  1. mysql_db_query()
  2. resource mysql_db_query(string database, string query [, resource link_id])
  3. $linkId = @mysql_connect("localhost", "username", "password") or die("不能连接到MysqlServer");
  4. $query = "select * from MyTable";
  5. $result = mysql_db_query("MyDatabase", $query);
  6. mysql_close();
  7. 为了使代码清晰,不推荐使用这个函数调用
复制代码

获取和显示数据

  1. mysql_result()
  2. mixed mysql_result (resource result_set, int row [, mixed field])
  3. $query = "select id, name from MyTable order by name";
  4. $result = mysql_query($query);
  5. $c_id = mysql_result($result, 0, "id");
  6. $c_name = mysql_result($result, 0, "name");
  7. 最简单、也是效率最低的数据获取函数
复制代码

获取和显示数据

  1. mysql_fetch_row()
  2. array mysql_fetch_row (resource result_set)
  3. $query = "select id, name from MyTable order by name";
  4. $result = mysql_query($query);
  5. while (list($id, $name) = mysql_fetch_row($result)) {
  6. echo("Name: $name ($id)
    ");
  7. }
  8. 函数从result_set中获取整个数据行,将值放在一个索引数组中。通常会结使list()函数使用
复制代码

获取和显示数据

  1. mysql_fetch_array()
  2. array mysql_fetch_array (resource result_set [, int result_type])
  3. $query = "select id, name from MyTable order by name";
  4. $result = mysql_query($query);
  5. while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  6. $id = $row["id"];
  7. $name = $row["name"];
  8. echo "Name: $name ($id)
    ";
  9. }
  10. result_type的值有:
  11. MYSQL_ASSOC: 字段名表示键,字段内容为值
  12. MYSQL_NUM: 数值索引数组,操作与mysql_fetch_ros()函数一样
  13. MYSQL_BOTH: 即作为关联数组又作为数值索引数组返回。result_type的默认值。
复制代码

获取和显示数据

  1. mysql_fetch_assoc()
  2. array mysql_fetch_assoc (resource result_set)
  3. 相当于调用 mysql_fetch_array(resource, MYSQL_ASSOC);
复制代码

获取和显示数据

  1. mysql_fetch_object()
  2. object mysql_fetch_object(resource result_set)
  3. $query = "select id, name from MyTable order by name";
  4. while ($row = mysql_fetch_object($result)) {
  5. $id = $row->id;
  6. $name = $row->name;
  7. echo "Name: $name ($id)
    ";
  8. }
  9. 在操作上与mysql_fetch_array()相同
复制代码

所选择的记录

  1. mysql_num_rows()
  2. int mysql_num_rows(resource result_set)
  3. #query = "select id, name from MyTable where id > 65";
  4. $result = mysql_query($query);
  5. echo "有".mysql_num_rows($result)."条记录的ID大于65";
  6. 只在确定select查询所获取的记录数时才有用。
复制代码

受影响的记录

  1. mysql_affected_rows()
  2. int mysql_affected_rows([resource link_id])
  3. $query = "update MyTable set name="CheneyFu" where id>=5";
  4. $result = mysql_query($query);
  5. echo "ID大于等于5的名称被更新了的记录数:".mysql_affected_rows();
  6. 该函数获取受INSERT,UPDATE或DELETE更新语句影响的行数
复制代码

获取数据库列表信息

  1. mysql_list_dbs()
  2. resource mysql_list_dbs([resource link_id])
  3. mysql_connect("localhost", "username", "password");
  4. $dbs = mysql_list_dbs();
  5. echo "Databases:
    ";
  6. while (list($db) = mysql_fetch_rows($dbs)) {
  7. echo "$db
    ";
  8. }
复制代码

获取数据库名

  1. mysql_db_name()
  2. string mysql_db_name(resource result_set, integer index)
  3. 该函数获取在mysql_list_dbs()所返回result_set中位于指定index索引的数据库名
复制代码

获取数据库表列表

  1. mysql_list_tables()
  2. resource mysql_list_tables(string database [, resource link_id])
  3. mysql_connect("localhost", "username", "password");
  4. $tables = mysql_list_tables("MyDatabase");
  5. while (list($table) = mysql_fetch_row($tables)) {
  6. echo "$table
    ";
  7. }该函数获取database中所有表的表名
复制代码

获取数据库表名

  1. mysql_tablename()
  2. string mysql_tablename(resource result_set, integer index)
  3. mysql_connect("localhost", "username", "password");
  4. $tables = mysql_list_tables("MyDatabase");
  5. $count = -1;
  6. while (++$count echo mysql_tablename($tables, $count)."
    ";
  7. }
  8. 该函数获取mysql_list_tables()所返回result_set中位于指定index索引的表名
复制代码

获取字段信息

  1. mysql_fetch_field()
  2. object mysql_fetch_field(resource result [, int field_offset])
  3. mysql_connect("localhost", "username", "password");
  4. mysql_select_db("MyDatabase");
  5. $query = "select * from MyTable";
  6. $result = mysql_query($query);
  7. $fields = mysql_num_fields($result);
  8. for($count = 0; $count $field = mysql_fetch_field($result, $count);
  9. echo "

    $field->name $field->type ($field->max_length)

    ";
  10. }
复制代码

返回的对象共有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

获取查询的字段数

  1. mysql_num_fields()
  2. integer mysql_num_fields (resource result_set)
  3. $query = "select id, name from MyTable order by name";
  4. $result = mysql_query($query);
  5. echo "这个查询的字段数是:".mysql_num_fields($result)."
    ";
复制代码

返回查询result_set中的字段数

获取指定表的所有字段的字段名

  1. mysql_list_fields()
  2. resource mysql_list_fields (string database_name, string table_name [, resource link_id])
  3. $fields = mysql_list_fields("MyDatabase", "MyTable");
  4. echo "数据库MyDatabase中表MyTable的字段数: ".mysql_num_fields($fields)."
    ";
复制代码

获取指定的字段选项

  1. mysql_field_flags()
  2. string mysql_field_flags (resource result_set, integer field_offset)
复制代码

获取指定的字段的最大长度

  1. mysql_field_len()
  2. integer mysql_field_len (resource result_set, integer field_offset)
  3. $query = "select name from MyTable";
  4. $result = mysql_query($query);
  5. $row = mysql_fetch_row($result);
  6. echo mysql_field_len($result, 0)."
    ";
  7. 如果mysql_field_len($reseult, 0) = 16777215
  8. 那么numer_format(mysql_field_len($result))等于16,777,215
复制代码

获取字段名

  1. mysql_field_name()
  2. string mysql_field_name (resource result_set, int field_offset)
  3. $query = "select id as PKID, name from MyTable order by name";
  4. $result = mysql_query($query);
  5. $row = mysql_fetch_row($result);
  6. echo mysql_field_name($result, 0); // Result: PKID
复制代码

获取字段类型

  1. mysql_field_type()
  2. string mysql_field_type (resource result_set, int field_offset)
  3. $query = "select id, name from MyTable order by name";
  4. $result = mysql_query($query);
  5. $row = mysql_fetch_row($result);
  6. echo mysql_field_type($result, 0); // Result: int
复制代码

获取字段所在表名

  1. mysql_field_table()
  2. string mysql_field_table (resource result_set, int field_offset)
  3. $query = "select id as PKID, name from MyTable order by name";
  4. $result = mysql_query($query);
  5. $row = mysql_fetch_row($result);
  6. echo mysql_field_table($result, 0); // Result: MyTable
复制代码


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage