php所支持的mysql函数,PHP的内置MySQL函数大全

MySQL数据库服务器是一种最流行的开源数据库。大多数php应用程序的开发使用mysql数据库。PHP有很多内置的MySQL函数,可以和MySQL来执行查询。这些功能有助于您管理您的数据库并加速发展。

下面是使用最多的列表PHP / MYSQL内建函数在PHP。

1. mysql_affected_rows

mysql函数mysql_affected_rows()将返回的行数影响或记录任何更新、插入或删除查询。通过使用这个功能,你可以查看纽约的成功更新。例如,如果您想要检查是否密码进行了更改,您可以简单地使用这个函数更新后行动。如果它返回一个然后打印成功否则不是。

/* mysql connection is required here to run this script */

/* Update records */

mysql_query(“UPDATE user SET password=’xyz’WHERE id=’101′”);

printf(“Updatedrecords:%d\n”,mysql_affected_rows());

/* Output; Updated Records: 1 */

?>

2. mysql_fetch_field

你可以得到所有的细节中使用的字段的一个表mysql_fetch_field()函数。这个函数返回一个对象,并使用所需的信息,我们可以对这个领域。

/* mysql connection is required here to run this script */

$result=mysql_query(“SELECT id,email FROM people WHERE id=’42′”);

echomysql_num_fields($result);

/* OUTPUT: returns 2 because id,email === two fields */

?>

3. mysql_fetch_lengths

这个函数 mysql_fetch_lengths() 返回一个数组和所有元素的数组存储数据的长度在每个字段的那一行

/* mysql connection is required here to run this script */

$result=mysql_query(“SELECT id,email FROM people WHERE id=’42′”);

$lengths=mysql_fetch_lengths($result);

print_r($row);

print_r($lengths);

?>

Output:

Array

(

[id] => 42

[email] => user@example.com

)

Array

(

[0] => 2

[1] => 16

)

4. mysql_fetch_row

函数的作用是 mysql_fetch_row() 返回一个记录以结果标识符。它返回的一组数据作为一个数组。来获取值数组必须使用数组偏移盯着从0。每次调用这个函数的作用是:返回下一个记录。

/* mysql connection is required here to run this script */

$result=mysql_query(“SELECT id,email FROM people WHERE id=’42′”);

$row=mysql_fetch_row($result);

echo$row[0];// 42

echo$row[1];// the email value

?>

5. mysql_field_flags

这个函数是用来收集旗帜相关联的一个表中的字段。这需要一个结果集和实地田野的偏移值并返回各种标志的分离与单一的空间。这里所有返回旗帜之间用一个空格分开,所以我们可以使用PHP的爆炸函数创建一个数组的旗帜。

/* mysql connection is required here to run this script */

$result=mysql_query(“SELECT id,email FROM people WHERE id=’42′”);

$flags=mysql_field_flags($result,0);

echo$flags;

print_r(explode(‘’,$flags));

?>

Output:

not_null primary_key auto_increment

Array

(

[0] => not_null

[1] => primary_key

[2] => auto_increment

)

6. mysql_field_type

The function mysql_field_type will return the type of field associated with the field. Different field types are varchar, char, int, blob, text, datetime etc… This command takes one result set as input along with a field identifier or an offset. It returns a string holding field type details.

mysql_connect(“localhost”,“mysql_username”,“mysql_password”);

mysql_select_db(“mysql”);

$result=mysql_query(“SELECT*FROM people”);

$fields=mysql_num_fields($result);

$rows=mysql_num_rows($result);

$table=mysql_field_table($result,0);

echo“Your’”.$table.“‘table has”.$fields.“fieldsand”.$rows.“record(s)\n”;

echo“Thetable has the following fields:\n”;

for($i=0;$i

$type=mysql_field_type($result,$i);

$name=mysql_field_name($result,$i);

$len=mysql_field_len($result,$i);

$flags=mysql_field_flags($result,$i);

echo$type.“”.$name.“”.$len.“”.$flags.“\n”;

}

mysql_free_result($result);

mysql_close();

?>

输出:

你的“人”表有4个字段和1记录(s)

这个表有以下领域:

64 not_null primary_key二进制字符串名称

int和11岁的not_null

字符串地址100 not_null

国家100年not_null enum字符串

7. mysql_pconnect

这个函数是用来使持久性连接到MySQL数据库的PHP脚本。Mysql_pconnect功能可以连接甚至住在脚本执行结束了。它不关闭连接像mysql_connect案例,它将关闭连接一旦脚本执行结束了。另一个区别是它试图找到任何现有的连接是否存在并返回一个标识符是否存在。

使用mysql_pconnect的主要目的是为了维护功能持久性连接到mysql服务器。函数mysql_close()persisitence无法关闭连接。

mysql_pconnect(“$servername”,“$dbuser”,“$dbpassword”);

?>

8. mysql_connect

mysql_connect函数是用来连接Mysql databas。 我们可以检查函数的成功通过检查结果。我们会得到一个真正的结果以防建立连接。在此基础上我们甚至可以打印一条消息,说明细节。此函数接受三个参数,第一个是主机名用户名和密码,然后然后。我们也可以把端口号随着主机名也。

这是函数连接到mysql数据库

mysql_connect(“$servername”,“$dbuser”,“$dbpassword”);

?>

9. mysql_create_db

您可以创建数据库的mysql服务器通过使用mysql_create_db函数。如果足够权限的用户然后该函数将MySQL服务器中创建数据库。

例子:

/* mysql connection details*/

$servername=‘localhost’;

$dbusername=‘username’;

$dbpassword=‘password’;

$link=mysql_connect(“$servername”,“$dbusername”,“$dbpassword”)

ordie(“Notable to connect to server”);

/* Create new database*/

if(mysql_create_db(“new_db”)){

print(“Databasecreated successfully

“);

}else{

print(“Errorcreating database:

“.mysql_error());

}

?>

10. mysql_db_name

通过使用这个功能,你可以列出所有数据库的mysql服务器上托管。下面是示例代码来获得所有mysql数据库的列表。

/*  mysql connection is required here to run this script */

$list_of_dbs=mysql_list_dbs();

$i=0;

$total=mysql_num_rows($list_of_dbs);

while($i

echomysql_db_name($list_of_dbs,$i).”

“;

$i++;}

?>

11. mysql_num_rows

函数的作用是:返回总表的行数。这个函数将一起使用mysql选择查询。这个函数是广泛应用于不同的php脚本。

例子:

/* mysql connection is required here to run this script */

$query=mysql_query(“SELECT*FROM people”);

$number=mysql_num_rows($query);

echo“Totalrecordsinpeople table=”.$number;

?>

12. mysql_query

这个函数是用来执行一个sql查询到mysql数据库。你可以写任何sql查询与插入、选择、更新、删除等,通过查询到mysql数据库。执行的结果的查询将会被监控状态。我们能得到真或假基于成功或失败的状态的查询。

例子:

/* mysql connection is required here to run this script */

$query=“selectname,address,countryfrompeople”;

$status=mysql_query($query)ordie(“sqlquery failed”);

if($status){echo“sqlqueryissuccessful”;}

else{echo“sqlquery failed”;}

?>

13. mysql_errno

这个函数是用来显示错误后的数字的执行sql查询。

例子:

/* mysql connection is required here to run this script */

$querry=mysql_query(“SELECT test_field FROM people”);

echo“ErrorNumber=”.mysql_errno();// will print 1054

?>

在上面的表是一个不存在的人test_field字段或列。这就是为什么它会显示errorno。

14. mysql_error

这个函数是用来显示错误消息之后执行sql查询的。

例子:

/* mysql connection is required here to run this script */

$querry=mysql_query(“SELECT test_field FROM people”);

echo“Errormessage=”.mysql_error();

// Displaye error message after executing a query

?>

15. mysql_close

函数mysql_close()是用于关闭mysql连接。 它可以接受一个可选的参数作为链接并关闭它。如果没有指定链接标识符然后最近打开的连接关闭。没有必要使用mysql_close()函数作为所有的连接都收在脚本的末尾的执行。

例子:

/* mysql connection is required here to run this script */

mysql_close()

?>

注意:mysql_close()函数将不关闭的持久连接由使用“永久链接)

16. mysql_fetch_array

这个函数获取一个结果行作为一个关联数组,一个数值型数组,或两者兼而有之

示例代码:

/* mysql connection is required here to run this script */

$result=mysql_query(“SELECT id,name FROM people”);

while($row=mysql_fetch_array($result)){

echo$row[0];

echo$row[1];

}

?>

17. mysql_result

函数的作用是:用来获得大型结果数据。你可以通过数字偏移位置领域的论点来获得更快的记录。

示例代码:

/* mysql connection is required here to run this script */

$result=mysql_query(‘SELECT name FROM work.employee’);

echomysql_result($result,2);// outputs third employee’s name

?>

18. mysql_fetch_assoc

这个函数retuns关联数组,对应于所获取的行。

示例代码:

/* mysql connection is required here to run this script */

$result=mysql_query(‘SELECT name FROM work.employee’);

$my_assoc_array=mysql_fetch_assoc($result);

print_r($my_assoc_array);// print entire associative array

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值