gpt4 book ai didi

php - 通过URL解析字符串搜索mysql数据库

转载 作者:行者123 更新时间:2023-11-30 00:53:34 25 4
gpt4 key购买 nike

我正在通过 rest API 将 URL 解析到我的 model,以从我的 mysql 数据库中检索数据。由于数据被解析为编码的url,mysql为我的搜索查询返回零结果,因为url被解析为,

http://www.test.comindex.php/rest/resource/qstn/questionTitle/search%val

我认为 mysql 尝试使用值 search%val 搜索数据库而不解码 url。

这个问题有解决办法吗?我已经在这个问题上挣扎了一段时间了,而且我对此还很陌生。 :(

Javascript解析数据

     function doViewQ(qTitle) {

var searchQ = document.getElementById("searchQVal").value;
searchQ = encodeURIComponent(searchQ);
qTitle = encodeURIComponent(qTitle);
$.ajax({
url: '<?php echo base_url();?>index.php/rest/resource/qstn/questionTitle/' + qTitle,

success: function(data) {

var jsonObj = JSON.parse(data);

alert(jsonObj);

document.getElementById('qres').innerHTML = '<tr><td><strong><a href="javascript:doViewQ()">'+ decodeURIComponent(jsonObj[0].questionTitle) + '</a></strong></br>'+ decodeURIComponent(jsonObj[0].questionBody) + '</td></tr>' ;


},
type: "get"
});
}

REST API Controller

    public function doGet()
{
// we assume the URL is constructed using name/value pairs
$args = $this->uri->uri_to_assoc(2);
switch ($args['resource']) {
case 'qstn' :
$res = $this->student->getQ($args);
if ($res === false) {
show_error('Unsupported request',404);
}
else {
// assume we get back an array of data - now echo it as JSON
echo json_encode($res);
}
break;

default:
show_error('Unsupported resource',404);
break;
}
}

MODEL 从 MySQL DB 检索数据

    public function getQ($args)
{
// use array keys as column names for db lookup
$where = array();
$valid_column_names = urldecode(array('questionTitle'));

// use only those $args vals (from URL) that match col names in students table
foreach ($valid_column_names as $key) {
if (isset($args[$key])) {
$where[$key] = $args[$key];
}
}
if (count($where) == 0) { // stop full select on table
return false;
}
$this->db->where($where);
$result = $this->db->get('questions');
// return the results as an array - in which each selected row appears as an array
return $result->result_array;

}

最佳答案

我认为你需要 PHP 的 rawurldecode():

echo rawurldecode('foo%20bar%40baz'); // foo bar@baz

PHP documentation

关于php - 通过URL解析字符串搜索mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20774113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com