用于显示表内容的PHP代码适用于一个数据库而不适用于另一个数据库

时间:2016-07-05 17:51:03

标签: php mysql database connect

我试图从我的一个名为" web"的数据库中显示表内容。我正在使用下面的代码,但是我得到错误"无法获取数据:没有选择数据库"。我已经用另一个数据库测试了这个代码并且工作正常,我只更改了数据库名称和表值等值。我在编码时相当新,但有什么我可能会遗失的吗?谢谢。

P.S。两个数据库的权限相同,空值为占位符。

<?php
   $opts = getopt("w:c:h");

   if (isset($opts['h'])) {
      print "-w whereString (Optional)\t";
      print "use \\\" if @ or , characters are used, such as -w email=\\\"null\\\"\n";
      print "\totherwise shows entire table\n";
      print "\n";
      print "-c columns seperated by comma and space in double quotes\n";
      print "\tdefault columns are: \"Function, Directory, Filename, HMC_or_LPAR, Description\"\n";
      exit(1);
   }


   if (isset($opts['w'])) {
      $whereString=$opts['w'];
   } else {
      $whereString="";
   }



   if (isset($opts['c'])) {
      $columns=$opts['c'];
   } else {
      $columns="Function, Directory, Filename, HMC_or_LPAR, Description";
   }


   $dbhost = 'localhost:null';
   $dbuser = 'null';
   $dbpass = 'null';
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   if(! $conn )
   {
     die("Could not connect: " . mysql_error() . "\n");
   }

if (empty($whereString)) {
   $sql = "SELECT $columns FROM rastrace";
} else {
   $sql = "SELECT $columns FROM rastrace WHERE $whereString";
}


mysql_select_db('web');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die("Could not get data: " . mysql_error() . "\n");
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    if (isset($row['Function']))          { print "Function::{$row['Function']}\n";                 }
    if (isset($row['Directory']))       { print "Directory::{$row['Directory']}\n";           }
    if (isset($row['Filename']))         { print "pmr::{$row['Filename']}\n";               }
    if (isset($row['HMC_or_LPAR']))  { print "HMC_or_LPAR::{$row['HMC_or_LPAR']}\n"; }
    if (isset($row['Description']))   { print "Description::{$row['Description']}\n";   } 
    print "--------------------------------\n";
} 
mysql_close($conn);
?>

0 个答案:

没有答案