Note that there are some explanatory texts on larger screens.

plurals
  1. POSearch mysql databse with string parsed through URL
    text
    copied!<p>I am parsing a url through the <code>rest API</code> to my <code>model</code> to retrieve data from my <code>mysql</code> database. Since the data is parsed as an <code>encoded url</code> mysql returns <code>zero results</code> for my search query as the url is parsed as,</p> <p><code>http://www.test.comindex.php/rest/resource/qstn/questionTitle/search%val</code></p> <p>and i think mysql tried to search the database with the value <code>search%val</code> without decoding the url.</p> <p>Is there any work around for this matter. I've been struggling with this issue for quite some time now and im kinda new to this. :( </p> <p><strong>Javascript parsing data</strong></p> <pre><code> function doViewQ(qTitle) { var searchQ = document.getElementById("searchQVal").value; searchQ = encodeURIComponent(searchQ); qTitle = encodeURIComponent(qTitle); $.ajax({ url: '&lt;?php echo base_url();?&gt;index.php/rest/resource/qstn/questionTitle/' + qTitle, success: function(data) { var jsonObj = JSON.parse(data); alert(jsonObj); document.getElementById('qres').innerHTML = '&lt;tr&gt;&lt;td&gt;&lt;strong&gt;&lt;a href="javascript:doViewQ()"&gt;'+ decodeURIComponent(jsonObj[0].questionTitle) + '&lt;/a&gt;&lt;/strong&gt;&lt;/br&gt;'+ decodeURIComponent(jsonObj[0].questionBody) + '&lt;/td&gt;&lt;/tr&gt;' ; }, type: "get" }); } </code></pre> <p><strong>REST api controller</strong></p> <pre><code> public function doGet() { // we assume the URL is constructed using name/value pairs $args = $this-&gt;uri-&gt;uri_to_assoc(2); switch ($args['resource']) { case 'qstn' : $res = $this-&gt;student-&gt;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; } } </code></pre> <p><strong>MODEL retreving data from MySQL DB</strong></p> <pre><code> 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-&gt;db-&gt;where($where); $result = $this-&gt;db-&gt;get('questions'); // return the results as an array - in which each selected row appears as an array return $result-&gt;result_array; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload