Solved

Wordpress wpdb class

Posted on 2015-02-11
6
104 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Do you think that WordPress is just for blogs?  Think again!  WordPress is really a fantastic all around platform that you can use to develop websites on.  Integrated into its basic functionality is the ability to create pages using your choice of a…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
The purpose of this video is to demonstrate how to create a Printer Friendly PDF on a WordPress Page. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome Screenshot” Google Chrome Extension, and SmallPDF.com Log…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

777 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