7 Replies - 487 Views - Last Post: 03 July 2020 - 03:44 AM Rate Topic: -----

#1 fisicx   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 24-February 12

$wpdb noob

Posted 29 June 2020 - 03:37 AM

For years whenever I've needed to get wordpress option data I've used get_options().

But I need to run some queries on the data to build conditional dropdowns and using my tested method is sloooooow.

So I started here: https://codex.wordpr..._Reference/wpdb and can see what I'm supposed to do but not sure how.

The options table is called 'carlist' and contains a multi-dimensional array of car makes and models.

I want to run a query to get the makes (to build a dropdown). Once a make has been selected a second query gets the models for that make.

So my question is, how would I structure the query?

Wordpress gives this sort of exmaple:

$myrows = $wpdb->get_results( "SELECT id, name FROM mytable" );


Which in my case might be something like:

$makes = $wpdb->get_results( "SELECT carlist, make FROM $wpdb->options" );


I suspect that's totally wrong!

And there would also need to be a filter to remove duplicates.

This is all new and unfamiliar territory for me so any help at all would be most appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: $wpdb noob

#2 Ornstein   User is offline

  • D.I.C Head

Reputation: 105
  • View blog
  • Posts: 216
  • Joined: 13-May 15

Re: $wpdb noob

Posted 29 June 2020 - 04:36 AM

I get the feeling you've confused quite a few concepts here. Surely no one has added a "carlist" or "make" column on the options table? So are those columns on their own table - or is this data serialized or JSON encoded in the options table?
Was This Post Helpful? 0
  • +
  • -

#3 fisicx   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 24-February 12

Re: $wpdb noob

Posted 29 June 2020 - 05:49 AM

I added the option.

I converted an supplied CSV to an array and saved it to the database. Pretty standard wordpress functionality.

I just need to be able to query the data rather than load the whole array and run the filters.

If I can get this to work I can then look at creating a new table rather than adding it to the wp_options table.
Was This Post Helpful? 0
  • +
  • -

#4 Ornstein   User is offline

  • D.I.C Head

Reputation: 105
  • View blog
  • Posts: 216
  • Joined: 13-May 15

Re: $wpdb noob

Posted 29 June 2020 - 06:16 AM

I'd recommend just putting it in its own table from the start. You can't (sensibly) query the data that is stored as a serialized array in a single row on the options table.
Was This Post Helpful? 1
  • +
  • -

#5 fisicx   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 24-February 12

Re: $wpdb noob

Posted 29 June 2020 - 07:02 AM

OK. Then I need to learn how to do this!
Was This Post Helpful? 0
  • +
  • -

#6 fisicx   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 24-February 12

Re: $wpdb noob

Posted 01 July 2020 - 03:51 AM

That wasn't so hard.

I was able to create a table but the problem now is the 60,000 rows in the CSV.

I looped through the imported file and all was well until row 2353. The system fell over and kept reporting an fgetcsv error.

I suspect a server issue (memory or write limits of something).
Was This Post Helpful? 0
  • +
  • -

#7 Ornstein   User is offline

  • D.I.C Head

Reputation: 105
  • View blog
  • Posts: 216
  • Joined: 13-May 15

Re: $wpdb noob

Posted 03 July 2020 - 03:41 AM

You could perhaps post your code and the exact error/s (possibly in a new thread?)
Was This Post Helpful? 0
  • +
  • -

#8 fisicx   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 24-February 12

Re: $wpdb noob

Posted 03 July 2020 - 03:44 AM

I found the problem in the end. It was writing too fast for the server to cope with.

I've set it up to work in smaller batches and it's all good.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1