查询mysql数据库时,php文件中的以下代码有效。但是当php文件试图连接到mysqli数据库时,它找不到数据库。如何更改以下代码才能使其正常工作?
感谢。
define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__)));
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
$db = JFactory::getDBO();
$sql = "SELECT filename FROM prefix_media_videos ORDER BY filename ASC";
$result = mysql_query($sql);
$var = array();
while ($row = mysql_fetch_array($result)) {
$var[] = $row['filename'];
}
答案 0 :(得分:0)
试试这个 -
设置MySQLi扩展程序:
$host = "";
$user = "";
$pass = "";
$dbName = ""; // FILL THESE IN
$db = new mysqli($host, $user, $pass, $dbName);
if($db->connect_errno){
echo "Failed to connect to MySQL: " .
$db->connect_errno . "<br>";
} // CHECK TO MAKE SURE IT CONNECTS
然后运行您的查询 -
$query = "SELECT filename FROM prefix_media_videos ORDER BY filename ASC";
$result = $db->query($query);
while ($row = $result->fetch_array(MYSQLI_NUM)) {
$var[] = $row->filename;
}
MySQLi是PHP扩展。不是数据库。
答案 1 :(得分:0)
首先在你的php中启用mysqli扩展。然后你可以像这样使用mysqli:
$db = new mysqli("host", "username", "password", "dbName");
if($db->connect_errno){
echo "Failed to connect to MySQL: " .
$db->connect_errno . "<br>";
}else{
$query = "SELECT filename FROM prefix_media_videos ORDER BY filename ASC";
$result = $db->query($query);
while ($row = $result->fetch_assoc()) {
$var[] = $row['filename'];
}
}
mysqli与php有关,而不是数据库。