10

コードでこのエラーが発生しましたが、コードで解決する方法がわかりません。

<?php
session_start();
include_once"connect_to_mysql.php";

$db_host = "localhost";
// Place the username for the MySQL database here
$db_username = "root"; 
// Place the password for the MySQL database here
$db_pass = "****"; 
// Place the name for the MySQL database here
$db_name = "mrmagicadam";

// Run the actual connection here 
$myConnection= mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("mrmagicadam") or die ("no database");        
$sqlCommand="SELECT id, linklabel FROM pages ORDER BY pageorder ASC";
$query=mysqli_query($myConnection, $sqlCommand) or die(mysql_error());
$menuDisplay="";


while($row=mysql_fetch_array($query)) {
    $pid=$row["id"];
    $linklabel=$row["linklabel"];
$menuDisplay='<a href="index.php?pid=' .$pid . '">' .$linklabel. '</a><br/>';
}
mysqli_free_result($query);

?>

これはエラーです:

警告:mysqli_query()は、パラメーター1がmysqliであると想定しています。リソースは、17行目のC:\ xampp \ htdocs \ limitless\connect_to_mysql.phpで指定されています。

私が間違っていることは何ですか?

4

2 に答える 2

26

mysqlimysqlの拡張機能が混在していますが、機能しません。

使用する必要があります

$myConnection= mysqli_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql"); 

mysqli_select_db($myConnection, "mrmagicadam") or die ("no database");   

mysqliは元の拡張子よりも多くの改良が加えられているmysqlため、 を使用することをお勧めしますmysqli

于 2012-04-15T08:48:37.660 に答える
7

不適切な構文を使用しています。ドキュメントmysqli_query()を読むと、2 つのパラメーターが必要であることがわかります。

混合 mysqli_query ( mysqli $link 、文字列 $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

mysql $link一般に、データベースを照会するために確立された mysqli 接続のリソース オブジェクトを意味します。

したがって、この問題を解決するには2つの方法があります

mysqli_query();

$myConnection= mysqli_connect("$db_host","$db_username","$db_pass", "mrmagicadam") or die ("could not connect to mysql"); 
$sqlCommand="SELECT id, linklabel FROM pages ORDER BY pageorder ASC";
$query=mysqli_query($myConnection, $sqlCommand) or die(mysqli_error($myConnection));

または、使用mysql_query()(これは現在廃止されています)

$myConnection= mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("mrmagicadam") or die ("no database");        
$sqlCommand="SELECT id, linklabel FROM pages ORDER BY pageorder ASC";
$query=mysql_query($sqlCommand) or die(mysql_error());

コメントで指摘されているように、ダイを使用してエラーを取得することに注意してください。視聴者に機密情報を誤って提供する可能性があります。

于 2012-04-15T08:56:36.117 に答える