在MySqli查询后无法回显某些内容?

时间:2013-12-30 07:14:29

标签: php mysql mysqli

今天,我有一个关于PHP的问题。

我知道根据我的设置确实正在调用此函数。但是,只有第一个请求的回声'正在展示(我用过"猫",长篇故事......)。

我知道很多时候这很简单就像错过了最终引用,但我的PHP并没有给我任何错误。我做错了什么?在我的代码之后,我无法获得任何结果。

在我这样做之前:

$connection = new mysqli('localhost', 'root', 'root', 'VV_root') or die("There was an error connecting; " . mysqli_connect_error);

然后这是我的功能。

function knock_on() {
echo("cats");
// It is asssumed that from here, all information is to be kept as secure as possible in the transactions
// Get all the ingredients
$username = $_POST['username'];
$password = $_POST['password'];
// Now, we have all the ingredients
// Mix password !IMPORTANT!

// Now, run the query to see if we get any matches.
$query = $connection->query("select * from user where username = " . $username . " & password = " . $password) or die("Something went wrong");
echo("cats2");
}

4 个答案:

答案 0 :(得分:2)

您正在遇到范围问题。您的$connection函数中没有knock_on()变量。将$connection作为参数传递给函数定义。

喜欢这个..

function knock_on($connection) { //Forget the global keyword thing.

答案 1 :(得分:0)

首先打开错误,然后您的查询出错:

$query = $connection->query("select * from user where username = " . $username . " & password = " . $password) or die("Something went wrong");

您需要使用AND运算符而不是&

 username = " . $username . " AND password = " . $password

修复第二个:$connection变量在knock_on()函数中不可用 Shankar Damodaran提到。

答案 2 :(得分:0)

请注意,此代码未经过测试。

$connection = new mysqli('localhost', 'root', 'root', 'VV_root') or die("There was an error connecting; " . mysqli_connect_error);

function knock_on() {
   global $connection;
echo("cats");
// It is asssumed that from here, all information is to be kept as secure as possible in the transactions
// Get all the ingredients
$username = $_POST['username'];
$password = $_POST['password'];
// Now, we have all the ingredients
// Mix password !IMPORTANT!

// Now, run the query to see if we get any matches.
$query = $connection->query("select * from user where username = " . $username . " & password = " . $password) or die("Something went wrong");
if($query) {
   return true;
} else {
   return false;
}
}

答案 3 :(得分:0)

我必须修理几件事,所以这是我的新工作代码

$connection = new mysqli("localhost", "root", "root", "VV_root");

实际功能:

function knock_on() {
echo("cats");
// It is asssumed that from here, all information is to be kept as secure as possible in the transactions
// Get all the ingredients
$username = $_POST['username'];
$password = $_POST['password'];
// Now, we have all the ingredients
// Mix password !IMPORTANT!

// Now, run the query to see if we get any matches.
if ($connection) {
$query = $connection->query("select * from user where username = '" . $username . "' and password = '" . $password . "'") or die("Something went wrong");
}
else {
echo($mysqli->connect_error);
}
echo("cats2");

}