Solved

Wordpress wpdb class

Posted on 2015-02-11
6
102 Views
Last Modified: 2015-02-11
I'm trying to insert data into a WordPress database table using $wpdb.

Here is my php:

<?php
      $thefile = "upload/zips.txt";
      $open = fopen($thefile, "r");
      if($open) {
            global $wpdb;
            $data = file($thefile);
            $nl = count($data);
            $table = "nosa_zips";
            echo "count = " . $nl . "<br>";
            for ($n = 0; $n < $nl; $n++) {
                  echo "zip = " . $data[$n] . "<br>";
                  $wpdb->insert( $table, array('zip'=> intval($data[$n])), array('%d'));
                  $wpdb->print_error();
            }
            fclose($open);
      }            
?>

It does not work. It echos the count, the first zip code & nothing else.

The table is as simple as it gets; a column of zip codes, see attached.

The input file is a tab delimited saved excel file. Before I put the $wpdp calls in I ran it just echoing the data read from the file; it listed the zips.

What's wrong?

What's wrong?
zips-table.jpg
0
Comment
Question by:Richard Korts
  • 3
  • 2
6 Comments
 
LVL 38

Expert Comment

by:Tom Beck
ID: 40604139
The problem starts here, $nl = count($data);
There's nothing to count. You have to explode the file into an array based on the delimiter, a tab.

$data = file($thefile);
$dataSplit = explode("\t", $data);
$nl = count($dataSplit);
0
 

Author Comment

by:Richard Korts
ID: 40604146
To Tom Beck,

That's not true. There are 49 records in the file; it display's  the number 49 (on the echo).

As I said, if I run a version of this that just echos the input file content, it works perfectly.

The problem is with $wpdb; I'm not doing it right.
0
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
ID: 40604285
Probably $wpdb is not available yet where you have placed the script.

Try including wp-load.php immediately followed by the global.

require( 'wp-load.php' );
global $wpdb;

$thefile = "upload/zips.txt";
      $open = fopen($thefile, "r");
      if($open) {
            $data = file($thefile);
            $nl = count($data);
            $table = "nosa_zips";
            echo "count = " . $nl . "<br>";
            for ($n = 0; $n < $nl; $n++) {
                  echo "zip = " . $data[$n] . "<br>";
                  $wpdb->insert( $table, array('zip'=> intval($data[$n])), array('%d'));
                  $wpdb->print_error();
            }
            fclose($open);
      }
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 38

Expert Comment

by:Tom Beck
ID: 40604374
Heads-up on this:

$wpdb->insert( $table, array('zip'=> intval($data[$n])), array('%d'));

My zip code starts with a zero. Leading zeros will be removed when saving a zip as an integer.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40604427
Tom, great catch on the leading zero.

rkorts,

Is this code:

<?php
      $thefile = "upload/zips.txt";
      $open = fopen($thefile, "r");
      if($open) {
            global $wpdb;
            $data = file($thefile);
            $nl = count($data);
            $table = "nosa_zips";
            echo "count = " . $nl . "<br>";
            for ($n = 0; $n < $nl; $n++) {
                  echo "zip = " . $data[$n] . "<br>";
                  $wpdb->insert( $table, array('zip'=> intval($data[$n])), array('%d'));
                  $wpdb->print_error();
            }
            fclose($open);
      }            
?>

Open in new window


all by itself or is it part of an overall WordPress page/post/cpt/template?
0
 

Author Closing Comment

by:Richard Korts
ID: 40604452
That did it; they all loaded.

Thanks!!

Maybe I can use $wpdb after all.

Now I hope I can do a "SELECT * where $xxx = 43004" for example.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

WordPress has made it easy to create scalable sites based on their powerful CMS, but as great as WordPress is, there are some SEO issues that haven’t been addressed out of the box.  Fortunately these problems can be solved with a few plugins.  The f…
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now