2

得到这个错误家伙“无法选择数据库名称”我是 PHP 新手,我正在学习。我正在创建一个 php 脚本,它将从我的数据库生成 XML 提要。这是代码:

<?php
$dbhost = "localhost";
$dbuser = "my database username";
$dbpass = "my password";
$dbname = "my database name";

$dbhandle = mysql_connect($dbhost, $dbuser, $dbpass) 
or die("Unable to connect to MySQL");

$selected = mysql_select_db("dbname",$dbhandle) 
or die("Could not select databasename");

$sql = "SELECT * FROM listings";
$q   = mysql_query($sql) or die(mysql_error());
$xml = "<listings>";
while($r = mysql_fetch_array($q)){
  $xml .= "<listings>";
  $xml .= "<listingsdb_title>".$r['listingsdb_title']."</listingsdb_title>";  
  $xml .= "<address>".$r['address']."</address>";
  $xml .= "<class_name>".$r['class_name']."</class_name>";  
  $xml .= "<listingsimages_thumb_file_name>".$r['listingsimages_thumb_file_name']."             </listingsimages_thumb_file_name>";    
  $xml .= "<beds>".$r['beds']."</beds>";   
  $xml .= "<baths>".$r['baths']."</baths>";  
  $xml .= "<sqm>".$r['sqm']."</sqm>";
  $xml .= "<author>".$r['author']."</author>";  
  $xml .= "<full_desc>".$r['full_desc']."</full_desc>";    
  $xml .= "<price>".$r['price']."</price>";  
  $xml .= "</listings>";  
}
$xml .= "</listings>";
$sxe = new SimpleXMLElement($xml);
$sxe->asXML("listings.xml");
?>
4

3 回答 3

0

坎格

$selected = mysql_select_db("dbname",$dbhandle)  to 
$selected = mysql_select_db($dbname,$dbhandle) 
于 2013-09-13T07:15:38.533 回答
0

我认为您正在尝试这样做...

$selected = mysql_select_db($dbname,$dbhandle) 
or die("Could not select databasename");

"dbname" => $dbname

于 2013-09-13T07:15:39.510 回答
0

错误显示您没有选择数据库..

您已经将数据库名称存储在$dbname变量中,现在只需将其传入mysql_select_db,如下所示。

$dbname = "my database name";
$selected = mysql_select_db($dbname,$dbhandle) or die("Could not select databasename");
于 2013-09-13T07:17:49.647 回答