-4

我想使用一个表单来查询 Ubuntu Server 中的 mysql 数据库。数据库正在连接,但不知何故没有按照代码中的要求回显结果。这是我的示例代码

<?php
$var=$_REQUEST['IP'];
echo "$var";
mysql_connect('localhost','root','mysql','syslog')
or die("Unable to connect to the database");
$result=mysql_query("SELECT * FROM arp_table");
$row=mysql_fetch_row($result);
echo $row[0];
?>

我确信它正在连接到数据库,因为它没有显示死亡消息,但除了显示连接到表单的变量之外它没有做任何事情。

4

2 回答 2

1
<?php
    // instance of pdo
    $config['db'] = array
    (
        'host' => 'localhost',
        'username' => 'root',
        'password' => '',
        'dbname' => 'syslog'
    );

    $dbh = new PDO('mysql:host=' . $config['db']['host'] . 
    ';dbname=' . $config['db']['dbname'],
    $config['db']['username'],
    $config['db']['password']);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

   // in another file where you are including the connection script call upon global
   global $dbh;
  // $query becomes a pdo object

        $query = $dbh->prepare("SELECT * FROM `arp_table`");
    $query->execute();
    $result= $query->fetchAll();

        print_r($result);
  ?> 
于 2013-09-04T06:29:44.547 回答
1

根据您提供的代码,您传递给mysql_connect的参数是错误的。

resource mysql_connect ([ string $server = ini_get("mysql.default_host") [, string $username = ini_get("mysql.default_user") [, string $password = ini_get("mysql.default_password") [, bool $new_link = false [, int $client_flags = 0 ]]]]] )

第 4 个参数是 new_link 不是数据库的名称。试试下面的代码 -

<?php

    $var = $_REQUEST['IP'];    
    echo $var;

    mysql_connect( "localhost", "root", "mysql" ) or die("Unable to connect to the database");
    mysql_select_db( "syslog" );

    $result = mysql_query( "SELECT * FROM arp_table" );

    $row = mysql_fetch_row( $result );

    print_r( $row );

?>

希望有帮助:)

于 2013-09-04T06:55:43.170 回答