0

I am working on migrating over some custom post types from a non-wordpress cms that we will call "shows." I am using a script in functions.php to try and accomplish this, but I am having trouble. I know the database exists and is valid, but I am unable to query the row properly. Instead of actual values, I am simply getting the value 1. I Probably do not correctly understand a function somewhere in my code, but I can't seem to figure out what I am doing wrong.

function import_shows() {
  $conn = mysqli_connect(constant("DB_HOST"), constant("DB_USER"), constant("DB_PASSWORD"), "THE_DB");

  if (!$conn) {
    error_log("Connection failed: " . mysqli_connect_error(), 0);
    return;
  } 

  $sql = "SELECT * FROM shows";
  $result = mysqli_query($conn, $sql);

  if (mysqli_num_rows($result) > 0) {
      while($row = mysqli_fetch_assoc($result)) {
          $res = print_r($row, true);
          error_log($res, 0);

          error_log($row["id_show"]);
          $test++;
      } 
  }

  mysqli_close($conn);
  return;
}

When I go to check the error log after this has run, the first print is always 1 and the second print is always empty. Any ideas?

  • hmmm this is all generic PHP, I'm not sure this stack is the best place to ask, maybe stackoverflow? Have you tried using the wpdb class? – Tom J Nowell Mar 26 '18 at 21:46
  • You are right, which is why this is so frustrating. I will look into it, my understanding was that it could only work with the wordpress db, but I just saw on the documentation that it can be reinstantiated to other dbs. – Wold Mar 26 '18 at 21:51
0

I was able to come up with a solution according to advice from the comments. Working code:

function import_shows() {
  $olddb = new wpdb(constant("DB_USER"),constant("DB_PASSWORD"),"THE_DB",constant("DB_HOST"));

  $sql = "SELECT * FROM shows";
  $rows = $olddb->get_results($sql);

  foreach($rows as $obj) {
    error_log($obj->name);
  }

  return;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.