[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Wordpress wpdb class

Posted on 2015-02-11
6
Medium Priority
?
132 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What's this? As a volunteer Page Editor for Experts-Exchange.com, I have noticed that many authors also have blogs, and pull articles from their blogs to post at Experts-Exchange. I appreciate each author sharing their blog content with our site …
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…
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…
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…
Suggested Courses

650 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