如何在php中调试mysql_query()函数

时间:2013-04-06 09:11:23

标签: php mysql login

此致

我是PHP的新手。这是我为登录页面编写的一个小登录脚本。它检查数据库中的用户名和密码。

 <?php

$con = mysqli_connect("localhost","root","saw","gehriroute");

if(mysqli_connect_errno())
{
    echo "could not connect to the database";
    exit;
}
else
{
    echo "database runing <br>";
}

$uname = trim($_POST['uname']);
$password = trim($_POST['password']);

echo "username and password are $uname and $password <br>";

$query =  "select count(*) from login where username = '".$uname."' and password = '".$password."' ";



$result = mysqli_query($query,$con);

if(!$result)
{
    echo "query failed";
}



$row = mysqli_fetch_row($result);
$count = $row[0];

if($count > 0 )
{
    echo "yes your are logged in";
}
else
{
    echo "wrong username or password";
}

?>

它没有连接到数据库的问题,但它也没有响应。我打赌脚本dosnt传递了mysql_error()函数。我尝试使用mysql_error()函数调试它,它什么也没有返回。我可以使用什么来调试和纠正错误?

输出:

database runing
username and password are ateev and saw
query failed

6 个答案:

答案 0 :(得分:1)

为什么不首先通过命令行或工作台运行SQL?

然后,这将使您确保查询有效并且您获得了预期的结果。

修改

另外,如果您阅读了mysqli_query手册页

$result = mysqli_query($query,$con);

不正确,应该是

$result = mysqli_query($con, $query);

答案 1 :(得分:1)

可以试试这个:

if (!mysqli_query($link, $query)) { //if the query fails find the error
    echo mysqli_error($link);
}

答案 2 :(得分:1)

我们试试这段代码吧。它现在有效。

<?php

$con = mysqli_connect("localhost","root","saw","gehriroute");

if(mysqli_connect_errno())
{
    echo "could not connect to the database";
    exit;
}
else
{
    echo "database runing <br>";
}


$uname = trim($_POST['uname']);
$password = trim($_POST['password']);

echo "username and password are $uname and $password <br>";

$query =  "select count(*) from login where username = '".$uname."' and password = '".$password."' ";

$result = mysqli_query($con,$query);   // Connection link should first

//echo "Error: ".mysqli_info();

$count = mysqli_num_rows($result);    // Get Count row like this..

if($count > 0 )
{
    echo "yes your are logged in";
}
else
{
    echo "wrong username or password";
}

?>

答案 3 :(得分:0)

尝试在查询后添加。这将检查/确定您的查询是成功执行还是返回错误

$result = mysqli_query($query,$con);

if(!$result){
  // means your query failed
  // error checking...
   printf("Error: %s\n", mysqli_error($con));
}
else {
   echo 'query executed.';
}

答案 4 :(得分:0)

很可能没有调用php脚本。尝试将.php文件设置为echo 'x';,然后查看x是否输出。如果没有,那么你调用错误的脚本并需要指定正确的文件名(例如: myScript.php )。

继承人working example of what your trying to do。只需将SELECT Code, Name FROM Country ORDER BY Name替换为您的查询。

快乐的编码:)

答案 5 :(得分:0)

  

Echo $查询;出口

复制Query并将其运行到您的Query Executer!

如果有任何

,您将收到SQL错误

其他明智的查询将成功运行。