Solved

Wordpress wpdb class

Posted on 2015-02-11
6
101 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 38

Expert Comment

by:Tom Beck
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Transferring a wordpress site from a host or local dev server to another host can be a pain. So I've included my steps on how I have accomplished this task. Steps include an assumption that you have Cpanel access or Ftp access.. If you do not hav…
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…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The purpose of this video is to demonstrate how to Import and export files in WordPress. 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 : Click on Too…

771 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

11 Experts available now in Live!

Get 1:1 Help Now