Solved

Wordpress wpdb class

Posted on 2015-02-11
6
111 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 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

How to create your own WordPress Theme Before I begin with the theme design tutorial, I would like to give you the basics of what is required to make a WordPress Theme. The most basic WordPress theme only requires 2 files: a style.css file and an…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
The purpose of this video is to demonstrate how to make a WordPress Site faster and smaller in size by cleaning up the database. This will be demonstrated using a Windows 8 PC. Plugin WP Optimize will be used. Go to your WordPress login page. T…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

738 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