[英]Mysql query always returning 0 results
What im trying to do is checking if a value exists in the table.If it does not exist a Failed message should be displayed 我正在尝试检查表中是否存在值,如果不存在则应显示失败消息
$mysqli = new mysqli("localhost","root","", "myusers");
print($string);
if ($mysqli->connect_errno) {
printf("Failed1");
exit();
}
else
{
if ($result=$mysqli->query("SELECT 1 FROM `users` WHERE `k1`='$string' AND 'k2'='$string2'"))
{
if($result->num_rows == 0)
{
printf("Failed2");
}
This query is always returning Failed2 inspite of the fact that the values are existing in the table.what is the problem,The table has three values k0,k1 and k2 but i use only k1 and k2 for SELECT Query.Please help 尽管表中存在值,但此查询始终返回Failed2。这是什么问题,该表具有三个值k0,k1和k2,但是我仅将k1和k2用于SELECT Query。请帮助
EDIT: 编辑:
I have changed the code ask you have suggested by removing ' but now the query will not execute and will lead to Failed3 我已经更改了代码,要求您通过删除'来建议,但现在查询将不执行,并导致Failed3
<?php
$string1=$_POST['value1'];
$string2=$_POST['value2'];
$mysqli = new mysqli("localhost","root","", "myusers");
print($string);
if ($mysqli->connect_errno) {
printf("Failed1");
exit();
}
else
{
if ($result=$mysqli->query("SELECT 1 FROM `users` WHERE k1='$string1' AND k2='$string2'"))
{
if($result->num_rows == 0)
{
printf("Failed2");
}
else
{
//---------
}
}
else
{
printf("Failed3");
}
}
?>
You used '
s instead of backticks! 你用'
s,而不是反引号!
`k1`='$string' AND 'k2'='$string2'"
------------------^--^---------------- You have used single quotes!
Replace it with: 替换为:
SELECT 1 FROM `users` WHERE `k1`='$string' AND `k2`='$string2'
从表和列名称中删除反引号和引号,即
SELECT 1 FROM users WHERE k1 ='$string' AND k2='$string2'
Unless you have a column called 1. You dont want to Select 1 from users
除非您有一个名为1的列。否则您不想Select 1 from users
This will return one result only, if thats what you were looking for 如果多数民众赞成在寻找什么,这只会返回一个结果
SELECT * FROM `users` WHERE `k1`='$string' AND `k2`='$string2' LIMIT 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.