简体   繁体   中英

Empty result from sql query in codeigniter mysql

when I execute a sql query in codeigniter using $this->db->query($sql); it gave me an empty result. But when I directly execute the same query from mysql it gives me the correct result. If Anyone have faced this problem help me ? I have more sql queries in the same project and those are working fine.

query -

$sql = 'select r.no from routes r where r.id = 
                    (select rp.route_id from routes_and_places rp where rp.place_id = 
                            (select p.id from places p where p.name = \''.$place.'\'));';

When this executes I got exact query by print_r($sql); and I executed it directly in mysql and it gave me the expected result. If anyone know the solution please help

There is a problem in sql string you passed

$this->db->select('id');
$this->db->where('name',$place);
$sql1 = $this->db->get('places');
$sql1 = $sql1->row();

$this->db->select('route_id');
$this->db->where('place_id',$sql1->id);
$sql2 = $this->db->get('routes_and_places');
$sql2 =  $sql2->row();

$this->db->where('id',$sql2->route_id);
$sql3 = $this->db->get('routes');
$sql3 = $sql3->result();

print_r($sql3);

One by one print all result and check that. Try this !

Printing directly print_r($sql); won't give you results. Its just the sql query and now you have to execute it and get the results . Try this,

$sql = "select r.no from routes r where r.id = 
        (select rp.route_id from routes_and_places rp where rp.place_id = 
        (select p.id from places p where p.name = '".$place."'));";

$query = $this->db->query($sql);
$result = $query->result();

/* print query */
echo $this->db->last_query();

/* print results */
print_r($result);

finally I could found the solution. I get values for $place variable from google places API. so when I get places although it doesn't visible through $this->db->last_query(); it contains CSS tags for those places. That means google send CSS to highlight road names with places. After hours of working on this problem finally I got an error saying incorrect syntax and it showed some CSS code in where clause that means $place contains some CSS codes also. So that I remove those CSS tags from the String and now it's working fine.

Code to remove CSS tags/new line characters and all other things from Strings here

$text = strip_tags($step->html_instructions,"<style>"); // remove css from text
                    $substring = substr($text,strpos($text,"<style"),strpos($text,"</style>")+2);
                    $text = str_replace($substring,"",$text);
                    $text = str_replace(array("\t","\r","\n"),"",$text);
                    $text = trim($text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM