mysqli_query()期望参数1为mysqli

时间:2013-09-06 00:33:42

标签: php database mysqli

我有新的PHP编程,并希望创建访问我创建的数据库。我已经能够在PHP和我的数据库之间建立成功的连接,但是当我尝试运行简单查询时出现了问题。

我得到了可怕的消息mysqli_query()期望参数1是mysqli。我在整个互联网上看到了很多这方面的问题。我仍然无法解决我的情况。有人可以在这里解决我的代码:

$mysqli= mysql_connect($hostname,$username,$password,'japanesewords')
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
 mysqli_query($mysqli, 'SELECT * FROM japanesedefinition') or die(mysql_error($mysqli));

1 个答案:

答案 0 :(得分:1)

为什么不使用PDO

<?php

try {

    // config
    $dsn = 'mysql:dbname=japanesewords;host=127.0.0.1;charset=utf8';
    $username = 'root';
    $password = '';
    $options = array(
        // necessary for rowCount() on SELECT
        PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
        // for catching SQL errors as PDOException
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        // default fetch mode is used for iterating PDOStatement by foreach() 
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
    );

    // connect
    $pdo = new PDO($dsn, $username, $password, $options);

    // execute SQL
    $stmt = $pdo->query('SELECT * FROM japanesedefinition');

    // check row count
    if (!$stmt->rowCount()) {
        throw new Exception('no data');
    }

    // fetch results and display
    echo "<p>\n";
    foreach ($stmt as $row) {
        printf("foo: %s; bar: %s;<br />\n", $row->foo, $row->bar);
    }
    echo "</p>\n";

} catch (Exception $e) {

    printf("<p>%s</p>\n", $e->getMessage());

}

我不知道你是不是日本人,用日语注释用PHP连接到MySQL的摘要 http://qiita.com/mpyw/items/b00b72c5c95aac573b71