PHP返回在Kohana中不起作用
我试着运行下面的代码,它应该从数据库中选择值,但不选择PHP返回在Kohana中不起作用,php,kohana,Php,Kohana,我试着运行下面的代码,它应该从数据库中选择值,但不选择 $val = doautocomp('Brisbane'); HTTP::redirect($url_address.'/hotel-search/go/' . $val); exit; } else { HTTP::redirect($url_address.'/hotel-search/error'); exit; } }
$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);
}
我在正确理解您的代码时遇到一些问题。您正在使用一个具有良好数据库类的框架。为什么要使用“mysql\u connect”。您的“mysql_close()”位于“return”语句下面。所以也许你的最后一个连接没有正确关闭。在使用mysql之前,您是否尝试过返回“测试” 您应该更像这样编写代码:
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;
}
此代码看起来是结构化的。这段代码的上下文是什么?普通脚本还是控制器?您似乎根本没有在这里使用Kohana(除了与此无关的HTTP类)。尽量不要使用旧的mysql扩展,使用来自Kohana的查询生成器或ORM或更新的mysqli扩展。关于这个问题,请发布完整的代码或提供更多关于您的问题的信息。@Manuras Kohana实际上使用了mysql_uuo(或PDO),3.4将为我们带来MySQLi。(你说得很对)我觉得你在混合Kohana版本2.x和3.x的语法。它们是非常不同的动物。