PHP无法在XAMPP中完成MYSQL查询

时间:2016-10-06 16:56:03

标签: php mysql xampp

我有一个问题。这是一个简单的php,它应该显示表中的行数。

<?php

$link = mysql_connect("localhost", "root", "1234") or die("Couldn't connect");
mysql_select_db("regulas", $link);

$result = mysql_query("SELECT * FROM index", $link) or die("Couldn't finish query");
$num_rows = mysql_num_rows($result);

echo $num_rows;

?>

但是,页面显示:

Couldn't connect

我在自己的计算机上使用xampp服务器:localhost

我做错了什么?

THX 朱

2 个答案:

答案 0 :(得分:0)

您必须提供密码,WAMP或XAAXMP中没有默认密码试试这个:

    <?php

$link = mysql_connect("localhost", "root","", "1234") or die("Couldn't   connect");
mysql_select_db("regulas", $link);

$result = mysql_query("SELECT * FROM index", $link) or die("Couldn't finish query");
$num_rows = mysql_num_rows($result);

echo $num_rows;

?>

请阅读此处的手册:http://php.net/manual/en/function.mysqli-connect.php

答案 1 :(得分:0)

我将mysql更改为mysqli并将索引更改为siteIndex

<?php

$ link = mysqli_connect(&#34; localhost&#34;,&#34; root&#34;,&#34; 102030 !!&#34;,&#34; custs&#34;)或死亡( &#34;无法连接&#34;);

$ result = mysqli_query($ link,&#34; SELECT * FROM siteIndex&#34;)或死亡(&#34;无法完成查询&#34;); $ num_rows = mysqli_num_rows($ result);

echo $ num_rows;

&GT;