首页 PHP 正文
260

PHP如何连接mysql

  • yiqingpeng
  • 2015-04-28
  • 0
  •  
$link=mysql_connect($server,$user,$password) or die("Could not connect the server!");
mysql_select_db($db,$link) or die("Database is not existed!");
mysql_query('set names utf8', $link);//第二个参数可以省略,它会默认使用上一次打开的链接。
$result = mysql_query('select * from tbl', $link);
$row=mysql_fetch_row($result);//该方法返回的自然索引数组,如果要返回关联数组用mysql_fetch_assoc(),还可以用mysql_fetch_array()返回混合型数组,不过通过它的第二个参数(MYSQL_ASSOC,MYSQL_NUM,MYSQL_BOTH)可以指定返回结果的类型。
while($row){
       $data .=",'".$row[1]."':'".$row[2]."'";
       $row=mysql_fetch_row($result);
}
mysql_free_result($result);
mysql_close($link);

正在加载评论...