PHP从数据库中读取和比较

时间:2014-01-28 15:54:29

标签: php database csv compare

现在我正在使用此脚本从csv文件中读取字符串并显示匹配结果:

function bic_query($blz) {
$cdata = -1;
$fp = fopen(DIR_WS_INCLUDES . 'data/swift.csv', 'r');
while ($data = fgetcsv($fp, 1024, ",")) {
  if ($data[0] == $blz){
    $cdata = array ('blz' => $data[0],
                    'bic' => $data[7]);
                     return $cdata;
  }
}
}
...
$bic = $adata['bic'];

我想从数据库中读取值并显示结果为$ bic变量,而不是从csv文件中读取。感谢。

1 个答案:

答案 0 :(得分:-1)

阅读php.net's mysqli。您需要建立与数据库的连接。我建议创建一个名为MySQL的类,并在构造函数中设置连接变量,例如:

class MySQL {

    var $hostname;
    var $username;
    var $password;
    var $database;
    var $databaseLink;
    var $error;

    function __construct($port=3306) {
        $this->database = 'your database name';
        $this->username = 'username';
        $this->password = 'password';
        $this->hostname = 'hostname' . ':' . $port;

        $this->Connect();
     }

     public function Connect() {
         $this->databaseLink = mysql_connect($this->hostname, $this->username, $this->password);

     if(!$this->databaseLink) {
         $this->error = 'Could not connect: ' . mysql_error($this->databaseLink);
         return false;
     } else {
      return true;
    }

阅读本手册将为您提供其他帮助,但这将为您提供如何使用它的好主意