2

I'm trying to populate a database table by reading some sql from a file.sql and putting it into a string before calling the query. But I seem to be unable to load the file into a string. What gives?

function db_data() {
    WP_Filesystem();
    global $wp_filesystem;
    global $wpdb;


    $table_name = $wpdb->prefix . "tableName";
    $file = "/sql/my_insert_query.sql";
    $sql_insert = "INSERT INTO $table_name (id, something) ";
    $sql_insert .= $wp_filesystem->get_contents( $file );
    $rows_affected = $wpdb->query( $sql_insert );
}

The $sql_insert turns out as an empty string. What am I doing wrong here?

EDIT:

Found the way to get to the right directory path. This is the finished code:

function db_data() {
    WP_Filesystem();
    global $wp_filesystem;
    global $wpdb;

    $table_name = $wpdb->prefix . "tableName";
    $sql_insert = "INSERT INTO $table_name (id, something) ";
    $dir = plugin_dir_path( __FILE__ );
    $file =  $dir."/sql/my_insert_query.sql";
    $sql_values = $wp_filesystem->get_contents( $file );
    $sql_insert .= $sql_values;
    $rows_affected = $wpdb->query( $sql_insert );
}

For the answer on finding and reading the sql file. Check the answer below.

5
  • is /sql/my_insert_query.sql the absolute path? Because if it's relative to your document root then it will come out empty. Commented Jun 13, 2014 at 14:21
  • @amenadiel It's not the absolute path. I used WP_PLUGIN_DIR but it came out without slashes so that didn't help either. So I should use the absolute path but how do I get to it? Because WP_PLUGIN_DIR gives me the path with the slashes missing. Commented Jun 13, 2014 at 14:38
  • how about $file = "./sql/my_insert_query.sql"; that should be relative to your wordpress root. Commented Jun 13, 2014 at 14:39
  • @amenadiel I tried it now but it still returns an empty string. Commented Jun 13, 2014 at 14:45
  • @amenadiel Thank you. I'm at work now and I need to go so I might not reply until Monday.. But I'd really appreciate your help. Commented Jun 13, 2014 at 14:47

1 Answer 1

2

Let's first start debugging the file path issue. There are two things you can use here.

function db_data() {
    WP_Filesystem();
    global $wp_filesystem;
    global $wpdb;
    echo 'Current Path is '. __DIR__;
    $file =  __DIR__."/sql/my_insert_query.sql";
    if(!is_readable($file)) {
        echo 'File not found or not readable '.$file;
    }     
    $table_name = $wpdb->prefix . "tableName";

    $sql_insert = "INSERT INTO $table_name (id, something) ";
    $sql_insert .= $wp_filesystem->get_contents( $file );
    $rows_affected = $wpdb->query( $sql_insert );
}

That will at least provide more insight about the filepath.

Sign up to request clarification or add additional context in comments.

2 Comments

I cannot use echo to see what's going on because it doesn't show up when I activate the plugin in WordPress but I used an insert query to see what happens in my database and I was able to find the right directory. And now my sql file reading and inserting works! Only problem is, DIR didn't work and WP_PLUGIN_DIR gives me the path without slashes.. I can't hardcode the path so how do I do it?
WP_PLUGIN_DIR should give you the path without trailing slash, same as DIR. If the latter didn't work, maybe you're using PHP 5.2. You can also replace it with dirname(__FILE__); but WP_PLUGIN_DIR is OK too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.