问题描述
非常令人沮丧,但 mysql 正在为此代码返回一个空集(不存在回显):
Been very frustrating but mysql is returning an empty set for this code (not echoing exists) :
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("repository", $con);
$url = stripslashes($url);
$url = mysql_real_escape_string($url, $con);
$exists = mysql_query("SELECT url FROM sites WHERE url = '$url' LIMIT 1");
if (mysql_num_rows($exists) == 1) {
echo "exists";
}
它不应该这样做,因为我已经测试了很多.
it should not be doing this because I've tested a good amount.
该表由一列url"组成,数据类型为 varchar(1000)(最大值).为测试目的存储的 url 是 http://en.wikipedia.org/wiki/Thailand
The table consists of one column "url", it is datatype varchar(1000) (the maximum). the url stored for test purposes is http://en.wikipedia.org/wiki/Thailand
推荐答案
这是您的第一步.从查询中完全删除 WHERE url = '$url'
并在使用它之前打印出 mysql_num_rows($exists)
.
Here's what you do as a first step. Remove the WHERE url = '$url'
from your query altogether and print out mysql_num_rows($exists)
before using it.
这应该足以说明它是否是两个最可能的问题之一:
That should be enough to tell if it's one of the two likeliest problems:
- 错误的 URL 导致没有返回任何行;或
- 由于数据库包含非预期内容而导致的错误行.
根据您迄今为止的评论,前者的可能性最大.如果结果证明您在没有where
子句的情况下返回了一行,则您必须弄清楚为什么您的 URL 不正确.这可能是区分大小写的问题或填充(大小)问题等.
Based on your comments to date, the former is the most likely. If it turns out you get a row back without the where
clause, you'll have to figure out why your URL is incorrect. This may be a case-sensitivity issue or a padding (size) issue, among other things.
如果,正如您在评论中提到的,like
在 =
不起作用的地方起作用,那么我们需要查看您的数据.
If, as you mention in a comment, like
works where =
doesn't, then we need to see your data.
执行(在数据库级别):
Execute (at the DB level):
select concat('[',url,']') from sites
并向我们展示确切的输出是什么.类似地,输出代码使用的 URL,例如:
and show us exactly what the output is. Similarly, output the URL being used by the code with something like:
print_r($url)
紧接在执行 mysql_query
之前.
请将这两个命令的输出附加到您的问题中.
Please append the output from both those commands to your question.
这篇关于mysql返回空集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!