• So I am writing a fairly complex custom plugin with my own tables (inside the main wp database), and I’m trying to save some time using PEAR functions on basic items.

    I want to run the PEAR datagrid function on a three table join query to paginate and autosort my results for a reports section.

    My issue here is that the examples I find want me to access the database using a connect string right in the script, and I’m not having that (for obvious security reasons, etc.). I’m not sure how to call the bind function on the wpdb object in a script like this. I’m sure it’s extremely simple, but I’m not seeing it. The bind function apparently accesses the database on its own, performs the query for you using the options (connect string).

    Can anyone give me any guidance on how to call this bind function using a wpdb object with wpdb->prepare() and then wpdb->get_results()? I have tried a bunch of different options with no luck.

    I added more info in the comments inside the code.

    function sample_datagrid() {
    		global $wpdb;
    		// This is how I'm supposed to supply the login credentials,
    		require 'mydblogin.php';
    
    		// but I want to use the $wpdb object to pull data from my custom tables.
    		// The PEAR datagrid functions access the db and execute the query for you,
    		// so I'm not sure if I can use this with a $wpdb object.
    
    		require 'Structures/DataGrid.php';
    
    		$datagrid = new Structures_DataGrid(2);
    
    		// This "options" section (commented out here) would need to go. I definitely don't want to put my access info right in the script.
    		// $options = array('dsn' => "mysql://$user:$password@$db_host/$db_name");
    
    		// I would prefer to use a \$wpdb->prepare statement here
    		$sql = $wpdb->prepare("SELECT * FROM wp_user");
    
    		$q = $wpdb->get_row($sql); 
    
    		// Then right here I would want to be able to reference the $q variable. I have tried doing this several ways,
    		// including running it on the \$sql variable instead of the $q in my own script.
    		// I have also tried running it with some of the \$wpdb functions (i.e. \$wpdb->connect or whichever).
    		// This is the line I'm not figuring out.
    		$bind = $datagrid->bind($q, $wpdb);
    		// The version in the example is: $bind = $datagrid->bind($sql, $options);
    		// but I want to use the $wpdb object (as shown in the line above) for obvious reasons.
    
    		if (PEAR::isError($bind))
    		{
    			error_log('DataGrid Error: '. $bind->getMessage());
    			$gridsource = '';
    		}
    
    		// (I removed the rest of the sample script because I don't think it's necessary, but can add if someone wants it.
    
    	} // End function sample_datagrid()

    Any help is much appreciated.

    Thanks!

  • The topic ‘How to use PEAR Datagrid with wpdb objects’ is closed to new replies.