Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Odd php error..

Credits go to BAF, bamccaig, and Matthew Leverton for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
Odd php error..
Thomas Fjellstrom
Member #476
June 2000
avatar

Yes, I suck at coming up with topics, so sue me ;)

I've been working on two php apps today, and both are giving me the same headache. Some how, php is caching the code and only refreshing when it feels like it. And it doesn't happen all the time, I was making good progress all afternoon till a bout an hour ago...

Heres the error I get from php:

[Thu Mar 29 18:29:59 2007] [error] [client 192.168.1.20] PHP Fatal error:
mysql error: [1146: Table 'moodle.mdl_teachmeto_activeusers' doesn't exist] in
EXECUTE("UPDATE mdl_teachmeto_activeusers SET lastactivity = '2007-03-29 18:29:59' WHERE userid = '5'")
in /var/www/.../htdocs/lib/adodb/adodb-errorhandler.inc.php on line 77, referer: http://.../chat/

And the funny thing? its right, that table doesn't exist, but it doesn't exist that way in the php code either. grep finds NO references to any mdl_teachmeto_* tables.

I'm stuck trying to figure out why php+apache can't seem to notice the code has changed, and hacking with a couple third party apps and libs. All in JS and PHP, neither of which I like very much. ::)

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

BAF
Member #2,981
December 2002
avatar

Well, you can specify the DB prefix in moodle, IIRC, so check for teachmeto_ references, as the mdl would be pulled from a configuration.

And, unless you have some type of caching setup, it will always execute the PHP every time the page is requested.

bamccaig
Member #7,536
July 2006
avatar

Can you tell us anything else? Maybe give us some code? What DBMS are you using? It appears related to ADOdb or the database.

Thomas Fjellstrom said:

"UPDATE mdl_teachmeto_activeusers SET lastactivity = '2007-03-29 18:29:59' WHERE userid = '5'"

Is that your SQL or something from PHP? Should the 5 be in single-quotes...?

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

Well, you can specify the DB prefix in moodle, IIRC, so check for teachmeto_ references, as the mdl would be pulled from a configuration.

Ah, the teachmeto stuff is an addition. Its not part of moodle, but it does access some of moodle's dirty bits.

Quote:

And, unless you have some type of caching setup, it will always execute the PHP every time the page is requested.

Good to know.

Quote:

Can you tell us anything else?

Thats all I can think of, it just decides not to notice that some code has changed.

Quote:

Maybe give us some code? What DBMS are you using? It appears related to ADOdb or the database.

Using mysql and ADODB, through moodle's api.

Quote:

Is that your SQL or something from PHP? Should the 5 be in single-quotes...?

Its from php, or possibly moodle's api.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

BAF
Member #2,981
December 2002
avatar

That SQL is fine, as far as the 5 being in quotes goes.

Thomas Fjellstrom
Member #476
June 2000
avatar

Lets chock that one up to stupidity (mostly on the part of the original programmer >:E)

Now I have another problem that I don't quite get:

[Thu Mar 29 20:42:06 2007] [error] [client 192.168.1.20] PHP Catchable fatal error:
Object of class stdClass could not be converted to string in
/var/www/.../htdocs/chat/chat.php on line 345, referer: http://.../chat/

Quote:

$teachers = array(); ...
/* line 345 */ return array_unique($teachers);

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

bamccaig
Member #7,536
July 2006
avatar

BAF said:

That SQL is fine, as far as the 5 being in quotes goes.

It's like a dynamically typed SQL!? {runs away crying}

Are most SQL implementations like that or only MySQL's?

BAF
Member #2,981
December 2002
avatar

That almost sounds like a running-php4-on-php5 or php5-on-php4 type of error.

Quote:

It's like a dynamically typed SQL!? {runs away crying}

If you knew SQL you would know that (IIRC) you are actually supposed to quote everything like that, for security reasons.

bamccaig
Member #7,536
July 2006
avatar

I do know SQL and I've never seen that before. In fact, if I'm not mistaken SQL Server will return errors if you quote a numeric field.

What security benefit is there to quoting everything?

BAF
Member #2,981
December 2002
avatar

Apparently I am wrong. I'll take note that you are not "supposed" to quote your integral types. As far as I know it works either way, however.

Thomas Fjellstrom
Member #476
June 2000
avatar

ok, It may be a php version error, the person that wrote this crap up and ditched on my friend...

1 function tmt_findMyTeachers()
2 {
3
4 global $USER;
5 $isTeacher = 0;
6 $teachers = array();
7
8 $COURSES = get_my_courses($USER->id);
9
10 foreach($COURSES as $COURSE)
11 {
12
13 $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
14 $myteachers = get_role_users(3, $context);
15 $myteacher2 = get_role_users(4, $context);
16
17 if ($myteachers)
18 foreach ($myteachers as $teacher)
19 array_push($teachers, $teacher);
20
21 if ($myteacher2)
22 foreach ($myteacher2 as $teacher)
23 array_push($teachers, $teacher);
24
25 }
26
27 return array_unique($teachers);
28
29 }

From my Very limited knowledge of php, I'd say that looks ok...

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

bamccaig
Member #7,536
July 2006
avatar

I looked it up on dev.mysql.com. Apparently MySQL will convert between types when necessary. So if you tried:
SELECT 1 + '1' AS Sum;

Result should be:
+-----+
| Sum | 
+-----+
|   2 | 
+-----+

That's why I ran away crying..........

I don't like that... Dynamic typing is evil...

BAF
Member #2,981
December 2002
avatar

It looks like get_role_users returns an array of classes or something, and array_unique has to be able to convert everything to strings. That's the only possible issue I can see with that code.

[edit]
Add a 'var_dump($teachers);exit(0);' before the return, and paste what it prints.

bamccaig
Member #7,536
July 2006
avatar

Thomas Fjellstrom
Member #476
June 2000
avatar

The problem with trying to var_dump it, is it only works through AJAX ::) its quite hard to get it to pass in some error/debug crap.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

BAF
Member #2,981
December 2002
avatar

Then you could do:

file_put_contents('teacher.log', var_export($teachers));

then post teacher.log.

bamccaig
Member #7,536
July 2006
avatar

  • Why define an $isTeacher variable and not use it?

  • What does get_my_courses() return?

  • What does get_context_instance() return?

  • What kind of variable name is $context?

  • What does get_role_users() return?

  • Why create two $myteacher[s] variables if the second isn't really used until you're done with the first?

  • Through all of the confusion this function brings me there are no comments!?

Thomas Fjellstrom
Member #476
June 2000
avatar

Its empty..

And the get_roles_user functions seem to get the roles in an array of something casted to (object)s.

bamccaig: this is not my code, please believe me ;(

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

bamccaig
Member #7,536
July 2006
avatar

Thomas Fjellstrom said:

And the get_roles_user functions seem to get the roles in an array of something casted to (object)s.

The roles of what?

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

The roles of what?

Of the user? Like if you're a teacher, or student, or lesbian, etc.

edit: actually thats not true...

They return other user objects that are assigned to that "role" (in this case, two forms of teacher).

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

BAF
Member #2,981
December 2002
avatar

Quote:

  1. Why define an $isTeacher variable and not use it?

  2. What does get_my_courses() return?

  3. What does get_context_instance() return?

  4. What kind of variable name is $context?

  5. What does get_role_users() return?

  6. Why create two $myteacher[s] variables if the second isn't really used until you're done with the first?

  7. Through all of the confusion this function brings me there are no comments!?

Don't ask him, he didn't write it. Nobody really knows what's going on. :P

Quote:

Its empty..

And the get_roles_user functions seem to get the roles in an array of something casted to (object)s.

How could it be empty and still give those errors? This baffles me. What if you just try return $teachers; dropping the array_unique() call temporarily to see if it at least approaches how it is supposed to work?

bamccaig
Member #7,536
July 2006
avatar

BAF said:

Don't ask him, he didn't write it. Nobody really knows what's going on.

He has more information than I do... I'm just trying to grasp what this function is supposed to do...

BAF said:

What if you just try return $teachers; dropping the array_unique() call temporarily to see if it at least approaches how it is supposed to work?

What if you rewrote the system, just to be sure.....?

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

What if you rewrote the site, just to be sure?

I wish, It'll take less time to just find the last bits that are going haywire.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

BAF
Member #2,981
December 2002
avatar

Why rewrite code when you can fix it? The new code won't look much different, and that code at least did something at some point, so it's easier just to fix it, IMO.

Anyway, I'm off to bed. I'll post more help in the morning.

bamccaig
Member #7,536
July 2006
avatar

BAF said:

Why rewrite code when you can fix it? The new code won't look much different, and that code at least did something at some point, so it's easier just to fix it, IMO.

Have you ever heard of The Daily WTF, recently renamed Worse Than Failure?

Everyday you'll find examples of how the code DIDN'T work at some point (i.e. NEVER worked), and yet it's still part of an enterprise level system... If the guy that wrote that function had a lot to do with the site you might be better off rewriting it from scratch.

Patching it to work won't improve maintenance in the future. It'll It might speed up launch day (possibly implementing buggy code), but eventually maintenance might outweigh the benefits of keeping code now.

And I hope that the new code would look much different. {smiley - when will EFW be over with!?!??!?}

 1   2   3 


Go to: