simple sql query is failing |
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
@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.
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); You have to make sure database configuration has save_queries set to true. (06-25-2018, 02:15 PM)php_rocs Wrote: @richb201, 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 (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:Thanks. I have it enabled but where is the last query stored?
proof that an old dog can learn new tricks
(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.
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
(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?
Where is it echoing to? Not the terminal.
proof that an old dog can learn new tricks
|
Welcome Guest, Not a member yet? Register Sign In |