mysql 獲取所有的數據庫名字


mysql 獲取所有的數據庫名字


 

一、如果使用的是mysqli:

$con = @mysqli_connect("localhost", "root", "123456");
if (mysqli_connect_errno($con)){
  die('Could not connect: ' . mysqli_connect_error());
}

$sql = 'show databases;';
if (!($db_list = mysqli_query($con,$sql))){
    //這里按照mysql命令行的格式提示錯誤信息。
    $errInfo = 'ERROR';
    $errInfo .= ' ' .mysqli_errno($con);//錯誤代碼
    $errInfo .= ' ('.mysqli_sqlstate($con).')';//SQLSTATE錯誤代碼
    $errInfo .= ' : '.mysqli_error($con);//錯誤描述
    die($errInfo);
}

while ($db = mysqli_fetch_object($db_list)){
  echo $db->Database . "<br />";
}
mysqli_close($con);

二、如果使用的是mysql:

$con = mysql_connect("localhost", "user", "password");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

$db_list = mysql_list_dbs($con);

while ($db = mysql_fetch_object($db_list))
{
  echo $db->Database . "<br />";
}
mysql_close($con);

總結:

  mysql_connect()函數修改為mysqli_connect();
  mysql_error()函數修改為mysqli_error();
  mysql_list_dbs()函數不存在。
  mysql_fetch_object()函數修改為mysqli_fetch_object();
  mysql_close()函數修改為mysqli_close();


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM