Active Record (a bit of hand-holding needed). |
[eluser]atw[/eluser]
Hi guys, I have two questions: 1. why doesn't my query/function/model (below) work? 2. Is it possible to print the current SQL Query in a page (for debugging) without making part of the query ambiguous (as I have done below)? This is the method I am working on in my model (the top comments are the query I am trying to recreate--which does work against this schema): Code: // SELECT *, ($table.won - $table.lost) AS average, ($table.won + $table.lost) AS played If I edit the final where clause segment to be ambiguous: Code: $this->db->where('active', 'y'); Code: SELECT *, (avg_singles.won - avg_singles.lost) AS average, However, I get the error (line 37 is "return $result" in the function above: Code: A PHP Error was encountered This is the relevant part of my database schema: Code: -- Many thanks, atw
[eluser]NateL[/eluser]
Enable Profiling in your constructor within your controller: Code: <?php Also - read your error. I think the problem is you're trying to echo the results out in the Model. If you enable profiling, you won't have to echo the results in the model. the Controller will handle all that
[eluser]atw[/eluser]
Profiling == AWESOME! I haven't got as far as that in my learning process I don't however, think you are correct regarding 'you’re trying to echo the results out in the Model'. That worked perfectly earlier, when I was trying a much simpler get-everything-from-one-table-test [lol] I have now commented out ALL echo's and print_r's from anywhere outside my view and still it doesn't work. Profiling shows this is my SQL query: Code: SELECT *, (avg_singles.won - avg_singles.lost) AS average, (avg_singles.won + avg_singles.lost) AS played Q1. Does this SQL look syntactically correct (if that's a real word)? Q2. Is there a better way to do my (very basic) JOINs? I am using: Code: * MySQL 5.0.32 Many thanks, atw
[eluser]NateL[/eluser]
I could be wrong pop open phpMyAdmin and run that query. Do you get results?
[eluser]atw[/eluser]
INTERESTING! I just followed a hunch (the back ticks looked wrong). I went into phpMyAdmin and ran the query (copied verbatim from the profiler) and it returned zero rows. Followed my hunch and removed the back ticks and it worked. So, I just edited my model, like so: Code: $this->db->select('*'); Note the addition of a third FALSE argument to the WHEREs. Will this be okay? Why has it caused a problem (is it my version of MySQL)?
[eluser]David Johansson[/eluser]
[quote author="atw" date="1248657939"]INTERESTING! I just followed a hunch (the back ticks looked wrong). I went into phpMyAdmin and ran the query (copied verbatim from the profiler) and it returned zero rows. Followed my hunch and removed the back ticks and it worked. So, I just edited my model, like so: Code: $this->db->select('*'); Note the addition of a third FALSE argument to the WHEREs. Will this be okay? Why has it caused a problem (is it my version of MySQL)?[/quote] Your change is correct! The problem was that you were searching for a row where the field player.id had the exact value 'avg_singles.player_id' but you rather wanted the field player.id to have the same value as the field avg_singles.player_id, by adding FALSE you get you intended result.
[eluser]atw[/eluser]
That's awesome. Thank you very much for the help! I still have a lot to learn regarding SQL. In fact I have so much to learn period and new stuff keeps coming along to learn. How am I ever going to learn it all before I die? Nightmare! atw |
Welcome Guest, Not a member yet? Register Sign In |