由于MySQL 4.1中更改了密码加密算法,原来的加密函数password()更改为old_password(),而新的加密算法名称为password(),所以在PHP中使用以前的mysql库不更改算法的话将与新的MySQL4.1中的冲突,于是PHP5中新增加了一个MySQL接口mysqli扩展库来与新的MySQL4.1对应。
mysqli库既可以使用与以前的mysql库相同的使用方法(就是多了一个i),也可以使用PHP5中面向对象的方法(mysqi类),不可避免的,要使用新版本的PHP和MySQL就不可避免地要使用mysqli库。
下面是使用mysqli库的例子:
下面的这个是使用传统的全函数方式的例子:
<?php
$link = mysqli_connect('localhost','user','yourpassword', 'sablog');
if (!$link)
{
printf("Can't connect to MySQL Server. Errorcode: %sn", mysqli_connect_error());
exit;
}
if ($result = mysqli_query($link, 'SELECT blogid, title FROM angel_blog'))
{
print("\n");
while( $row = mysqli_fetch_assoc($result) )
{
printf("%s\t(%s)\n", $row['blogid'], $row['title']);
}
mysqli_free_result($result);
}
mysqli_close($link);
?>
下面的这个是使用面向对象方法的例子:
<?php
$mysqli = new mysqli('localhost', 'user', 'yourpassword', 'sablog');
if (mysqli_connect_errno())
{
printf("Can't connect to MySQL Server. Errorcode: %sn", mysqli_connect_error());
exit;
}
if ($result = $mysqli->query('SELECT blogid, title FROM angel_blog'))
{
print("\n");
while( $row = $result->fetch_assoc() )
{
printf("%s\t(%s)\n", $row['blogid'], $row['title']);
}
$result->close();
}
$mysqli->close();
?>