• Resolved melrrm

    (@melrrm)


    Can I select views using the $wpdb class?

    I am unable to select Views in my (custom, non-wordpress) MySQL database using the wpdb class. Is this something that is not allowed, or is my code just wrong? Is there a way to query views the same way I can query tables using wpdb?

    For context, this is the wpdb class: https://codex.www.ads-software.com/Class_Reference/wpdb. With that class, I am able to query TABLES of my database. But not Views.

    I have tried using the query function, as well as treating a view the same way I treat a table, but it does not work. It returns empty.

    Query method:

    $test = $mydb->query(
        $mydb->prepare(
        "
                 SELECT name FROM $mydb->$view_name
         WHERE id = 1"
         )
        );
    
        echo $test; //returns empty; should return a name

    Treating-it-like-a-table method:

    $test = $mydb->get_var(
        "select name from $view_name WHERE id = 1"
        );
    
    echo $test; //returns empty; should return a name

    Any suggestions?

Viewing 1 replies (of 1 total)
  • Thread Starter melrrm

    (@melrrm)

    Ah, never mind! I figured out my problem.

    My view was named something that was causing an error. As well, “name” is a reserved keyword.

    It works now!

Viewing 1 replies (of 1 total)
  • The topic ‘Selecting MySQL views using $wpdb class’ is closed to new replies.