An unrecoverable error has occured. You can find the error message below. It is advised to copy it to the clipboard for reference. Please continue to the update summary

Fatal error: Cannot use string offset as an array in /home/blah/textlinkads/textlinkads.module on line 322

The update summary says:

The update process was aborted prematurely while running update #1 in textlinkads.module. All other errors have been logged. You may need to check the watchdog database table manually.

Then there are these in watchdog:

Table 'blah.textlinkads' doesn't exist query: textlinkads_get_links SELECT * FROM textlinkads WHERE text IS NOT NULL && beforetext IS NOT NULL && aftertext IS NOT NULL LIMIT 6 in /home/blah/includes/database.mysql.inc on line 120.

The same thing happened on two sites, but the table is there after I run the update.

Comments

robertdouglass’s picture

Thanks. I'll look into it.

robertdouglass’s picture

Is data representing your links to be found in the table after it is created?

kbahey’s picture

Also, when cron.php runs, the same error occurs:

Fatal error: Cannot use string offset as an array in /home/blah/textlinkads/textlinkads.module on line 322

If it helps, both sites have no text link ads yet, and the table is empty.

robertdouglass’s picture

Mmmkay. I guess I didn't test the case where there were no links. You're running PHP5, right?

kbahey’s picture

Yes. I am running PHP5.

Part of the problem is that $data does not have anything. In my case, a print_r() shows:

Array
(
    [LINKS] => 

)
kbahey’s picture

Title: Error upon running update.php » Errors when having textlinkads enabled, but no ads sold.

Changing the title.

There are also other errors in the log from every page view, in addition to the cron error:

array_pop() [function.array-pop]: The argument should be an array in /home/blah/modules/textlinkads/textlinkads.module on line 349.

shuffle() expects parameter 1 to be array, null given in /home/blah/modules/textlinkads/textlinkads.module on line 346.

kbahey’s picture

Now I am in a bind ...

I use this module on a multisite install.

When I had those errors, I just hacked the code to prevent them from polluting cron and other log entries.

Then one site sold an ad, so I put the code back to what it is, and the link ads worked fine.
However, on the other site, I kept getting the errors above all the time.
So, I disabled the module on the second site.
That was fine for a few days, but then Text-Link-Ads.com sent me an email that the second site is not accessing the XML data, and will be taken off the inventory!

Catch-22 ...

Every site that has not sold ads will see these errors.

Can something be done about this?

(Another thing is that I am not getting emails from only this issue, don't know why).

robertdouglass’s picture

I'll do my best to look into this over the weekend. Thanks for your patience.

mcurry’s picture

Assigned: kbahey » Unassigned
Status: Closed (fixed) » Active
mcurry’s picture

Here's the offending XML returned by the TLA server for my publisher account (no ads yet.)

<?xml version="1.0" ?>
  <Links>
  </Links>

So, the function textlinkads_update_links() needs to handle this condition (no child elements of the <Links> element.

robertdouglass’s picture

Please test and see if this minor change addresses all of the problems reported (posting patch here, but commited to CVS already):

diff -u -F^f -r1.1.4.4 textlinkads.module
--- textlinkads.module  24 Aug 2006 13:00:18 -0000      1.1.4.4
+++ textlinkads.module  18 Sep 2006 17:50:06 -0000
@@ -318,8 +318,8 @@ function textlinkads_update_links($url, 
   db_query("DELETE FROM {textlinkads}");
   $parser = new xml2array();
   if ($data = $parser->parseXMLintoarray($xml)) {
-    // there is just one link
-    if ($data['LINKS']['LINK']['URL']) {
+    // there are no links or just one link
+    if (count($data['LINKS']) > 0 && $data['LINKS']['LINK']['URL']) {
       $links = array($data['LINKS']['LINK']);
     }
     else {

mcurry’s picture

robertdouglass’s picture

Thanks for the excellent information. Let's try two things so that I can understand what is happening:


  // Thing number 1: what is in xml?
  dprint($xml); // you need the devel mod for dprint

  // (still Thing number 1): does trimming $xml help in any way?
  trim($xml);

  if ($data = $parser->parseXMLintoarray($xml)) {
    // there are no links or just one link

    // Thing 2: since you're getting a string at this point, let's try building an !is_string() into the logic
    // We can't use is_array because in PHP5 strings are arrays
    if (!is_string($data) && count($data['LINKS']) > 0 && $data['LINKS']['LINK']['URL']) {

If you don't mind, I'd really like to get this cleared up... thanks for your help.

mcurry’s picture

Shouldn't

  // (still Thing number 1): does trimming $xml help in any way?
  trim($xml);

be

  // (still Thing number 1): does trimming $xml help in any way?
  $xml=trim($xml);

according to http://us3.php.net/trim (and my tests verify this) trim() doesn't modify the original string. So..

I will check out your suggestions later - I really can't do anything with this right now.

Is it possible for you to build a dummy test XML string with my data for debugging purposes.

Change:

 $xml = textlinkads_file_get_contents($url, $time_out);

to:

 $xml = "<?xml version=\"1.0\" ?>\n<Links>\n</Links>\n";
kbahey’s picture

I upgraded to the latest 4.7 module from CVS.

Upon running cron, it is ok on the site that has ads. ON the other site I get this error:

Fatal error: Cannot use string offset as an array in /home/blah/modules/textlinkads/textlinkads.module on line 322

flevour’s picture

StatusFileSize
new1.06 KB

This is a patch that fixes 2 errors that appeared in cron.php
The first part gets sure $data['LINKS'] is an array before accessing it, the second part makes $links an array just in case $links doesnt get populated by successive query.

flevour’s picture

Assigned: Unassigned » flevour
Status: Active » Needs review

Updating status. Please review the patch.

kbahey’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new1.2 KB

This patch fixes the issue for sites with empty.

It had one hunk that failed though, so I am attaching a patch generated from the version that I am running.

kbahey’s picture

Not yet perfect but does not crash and stop cron. It generates this error when cron runs:

Invalid argument supplied for foreach() in /home/blah/textlinkads/textlinkads.module on line 329.

However, the ads are downloaded correctly on the other site that has ads.

I tried moving the foreach and INSERT inside the if check for array, but that caused the ads not to be downloaded.

piraxter@drupal.org’s picture

Category: bug » support

I update Textadlink module and now show me these warning.

user warning: Table 'we_drpl1.textlinkads' doesn't exist query: SELECT * FROM textlinkads WHERE text IS NOT NULL && beforetext IS NOT NULL && aftertext IS NOT NULL LIMIT 4 in /home2/we/public_html/huajuapan/includes/database.mysql.inc on line 120.

kbahey’s picture

Category: support » bug

If this is an unrelated problem, then please file a new support request.

Setting back to a bug report.

I have it working OK now except for an annoying message in the log when cron runs. it does not cause cron to abort though as before, impacting other modules.

flevour’s picture

What kind of error? Can you post it here?

kbahey’s picture

Status: Reviewed & tested by the community » Fixed

I committed a fix for this with Rob's approval.

It is not perfect, since there is an error logged each time cron is run, but at least the functionality is there, and the module handles the case where ads are not sold on the site yet.

If someone is facing other errors, please open a new issue for that.

bryansd’s picture

I continue to get a similar error that was posted earlier. No ads currently being posted through textlinkads. Here is the error (slightly modifided):

Invalid argument supplied for foreach() in ../modules/textlinkads/textlinkads.module on line 329.

Bryan

kbahey’s picture

Yes, if you read above, my fix has the drawback that this error is logged. I also get it on my sites that have no ads.

However, it is a significant improvement over the previous situation, where TLA would stop adding your site to the inventory because the previous error caused cron to abort completely. It also had the side effect of cron not running for other modules because of that abort.

Someone else can provide a patch, and I (or Robert) will fix it.

Anonymous’s picture

Status: Fixed » Closed (fixed)
allen074’s picture

Priority: Normal » Critical

warning: Invalid argument supplied for foreach() in /home/centerne/public_html/modules/contrib/textlinkads/textlinkads.module on line 329.

i am still getting this error - where is this fix??

thanks

mcurry’s picture

Status: Closed (fixed) » Needs review
StatusFileSize
new1.16 KB

I see this, too, on my site(s) having no ads.

For the record, again, here's what the data looks like on a site with no ads:

  <?xml version="1.0" ?> <Links> </Links>

So, the existing code assumes that there are child elements of <Links> named <Link>; when this is not true, $links = ' ' (a single-character string) and the foreach fails since $links is not an array. The code needs to deal with non-existent child data, which as far as I can see, it does not in the current version.

Attached is a patch against the 4.7 code base that fixes the foreach problem on my site.

Please review and comment.

mcurry’s picture

Priority: Critical » Normal

Marking as normal priority, since this not that big of a problem (except for annoying error reports in the watchdog log)

kbahey’s picture

Assigned: flevour » kbahey
Status: Needs review » Fixed

Committed to 4.7. Thanks to inactivist.

Rob, is there a reason there is no HEAD version of that module?

robertdouglass’s picture

Thanks for the bugfixes.

The reason there is no HEAD version is because I dislike the (now outdated) paradigm of the "versionless" HEAD. I usually only comitted versions that worked with actual distributions (4-6, 4-7, etc.).

mcurry’s picture

Ah, thanks for the CVS version explanation. Problem is, it took me a few minutes to figure out why my CVS client was failing on checkout (defaults to the current head revision), so it wouldn't get any files. I had to manually specify the DRUPAL-4-7 branch before I could get the files from CVS.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Status: Active » Closed (fixed)