Welcome Guest, Not a member yet? Register   Sign In
simple sql query is failing
#1

(This post was last modified: 06-25-2018, 12:07 PM by richb201.)

I am running:


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


where $sql="SELECT * FROM users WHERE email = '[email protected]'"

This is failing. The $result->num_rows = NULL after I make this call. I think the reason is that mysql doesn't like the email address enclosed in single quotes. Is there anything I can do to the $sql string to clean it up so mySQL returns a result? 

I also noticed that $result->result_id->num_rows =1
Is it possible that $result->num_rows is wrong and it should be checking $result->result_id->num_rows?
BTW, result_array is empty.
proof that an old dog can learn new tricks
Reply
#2

@richb201,

Have you run the query directly in MySQL first? Also, you should always verify that you are not returning an empty result set and if you are be able to handle it.

Here is how I check the result set:
return (count($result->result_array()) > 0) ? $result->result_array(): FALSE;

Then in the controller or view if the return value is FALSE then I account for an empty returned array/value.
Reply
#3

You could print out last DB query and any errors right after query call and see what it actually tried to run:
PHP Code:
$result $this->db->query($sql);
echo 
$this->db->last_query();
echo 
$this->db->error(); 

You have to make sure database configuration has save_queries set to true.
Reply
#4

(This post was last modified: 06-26-2018, 01:11 AM by neuron.)

(06-25-2018, 02:15 PM)php_rocs Wrote: @richb201,

Here is how I check the result set:
return (count($result->result_array()) > 0) ? $result->result_array(): FALSE;

CI has num_rows function, 
if($result && $result->num_rows() > 0) return fasle;

as I know there is no property named num_rows, but there is method num_rows() -> https://www.codeigniter.com/userguide3/d...::num_rows
Reply
#5

(06-25-2018, 02:16 PM)Pertti Wrote: You could print out last DB query and any errors right after query call and see what it actually tried to run:
PHP Code:
$result $this->db->query($sql);
echo 
$this->db->last_query();
echo 
$this->db->error(); 

You have to make sure database configuration has save_queries set to true.
Thanks. I have it enabled but where is the last query stored?
proof that an old dog can learn new tricks
Reply
#6

(This post was last modified: 06-26-2018, 07:58 AM by Pertti.)

(06-26-2018, 07:45 AM)richb201 Wrote: Thanks. I have it enabled but where is the last query stored?

First, you have to make sure in database config file, you've set save_queries to true.

They are then automatically saved in $this->db.

The sample code should print out most recent query and error message, if it failed on the website. It is of course very rough way to debug code and you shouldn't make that code public, but at least it should get you closer to what's going on with your script on your development environment.
Reply
#7

I don't think it is coming up with an error. It is just not finding my email. I think it is looking for '[email protected]' and not finding it. It should be looking for [email protected] (ie without the '). I know this because when I hard code in I get through that part of the code.
proof that an old dog can learn new tricks
Reply
#8

(06-26-2018, 10:55 AM)richb201 Wrote: I don't think it is coming up with an error. It is just not finding my email. I think it is looking for '[email protected]' and not finding it. It should be looking for [email protected] (ie without the '). I know this because when I hard code in I get through that part of the code.

Oh yeah the error there was just in case it does - but the last query should show if there are anything weird going into query, that's all.

Is the email exactly the same in DB, no spaces, no upper case letters or anything?
Reply
#9

Where is it echoing to? Not the terminal.
proof that an old dog can learn new tricks
Reply
#10

(06-26-2018, 01:20 PM)richb201 Wrote: Where is it echoing to? Not the terminal.

It's either terminal or browser, depends where you calling the script from.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB