我尝试运行以下代码,它应该从数据库中选择值而不是选择。
$val = doautocomp('Brisbane');
HTTP::redirect($url_address.'/hotel-search/go/' . $val);
exit;
}
else {
HTTP::redirect($url_address.'/hotel-search/error');
exit;
}
}
function seoUrl($string) {
$string = strtolower($string);
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
$string = preg_replace("/[\s-]+/", " ", $string);
$string = preg_replace("/[\s_]/", "-", $string);
return $string;
}
function doautocomp($mval) {
$linker = mysql_connect('localhost', 'root', 'pass');
$result3 = mysql_query("SELECT name FROM the.autocompletes where name LIKE '".$mval."%' LIMIT 0,1");
while($row = mysql_fetch_array($result3))
{
$val1 = $row['name'];
$mval = seoUrl($val1);
}
return $mval;
mysql_close($linker);
}
我也试过,但仍然无法正常工作。 返回测试
$val = doautocomp('Brisbane');
HTTP::redirect($url_address.'/hotel-search/go/' . $val);
exit;
}
else {
HTTP::redirect($url_address.'/hotel-search/error');
exit;
}
}
function seoUrl($string) {
$string = strtolower($string);
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
$string = preg_replace("/[\s-]+/", " ", $string);
$string = preg_replace("/[\s_]/", "-", $string);
return $string;
}
function doautocomp($mval) {
$linker = mysql_connect('localhost', 'root', 'pass');
$result3 = mysql_query("SELECT name FROM the.autocompletes where name LIKE '".$mval."%' LIMIT 0,1");
while($row = mysql_fetch_array($result3))
{
$val1 = $row['name'];
$mval = seoUrl($val1);
}
return "test"; //$mval;
mysql_close($linker);
}
答案 0 :(得分:2)
我在理解你的代码时遇到了一些问题。您正在使用具有良好数据库类的框架。为什么使用“mysql_connect”。您的“mysql_close()”位于“return”语句下。所以也许你的最后一个连接没有关闭。您是否在使用mysql之前尝试返回“TEST”?
您应该更像这样编写代码:
function doautocomp($mval) {
$row = Database::instance()->query(Database::SELECT,
DB::select('name')
->from('autocompletes')
->where('name','LIKE',$mval.'%')
->limit(1)
)->current();
if ($row === FALSE)
{
return "";
}
$val1 = $row['name'];
$mval = seoUrl($val1);
return $mval;
}