• I tried creating a shortcode with the Code Manager plugin using the code below, but it crashed when I tried to run it. It is a simple repeating query just to test the concept and it works on its own (outside of WordsPress) just fine.

    Is this beyond the shortcode capabilities? If not how do I fix it to make it work?

    <?php require_once('https://pabirds.org/Connections/siteguide2018.php'); ?>
    <?php require_once('https://pabirds.org/webassist/mysqli/rsobj.php'); ?>
    <?php
    $Recordset1 = new WA_MySQLi_RS("Recordset1",$siteguide2018,0);
    $Recordset1->setQuery("SELECT * FROM publications");
    $Recordset1->execute();
    ?>
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    </head>
    
    <body>
    <div>
      <p>Test PHP/SQL</p>
      <p>&nbsp;</p>
    </div>
    <p>&nbsp;</p>
    <?php
    $wa_startindex = 0;
    while(!$Recordset1->atEnd()) {
      $wa_startindex = $Recordset1->Index;
    ?>
      <div>
        <p><?php echo($Recordset1->getColumnVal("PubFileName")); ?></p>
      </div>
      <?php
      $Recordset1->moveNext();
    }
    $Recordset1->moveFirst(); //return RS to first record
    unset($wa_startindex);
    unset($wa_repeatcount);
    ?>
    
    </body>
    </html>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Passionate Programmer Peter

    (@peterschulznl)

    Hi @pabirds,

    Thank you for reaching out!

    You require_once statements contain full URLs. PHP sends a request to the server and gets the output of the PHP script. The response will not contain the actual PHP code. Unless the script itself outputs PHP code…?

    If your publications table is stored in your WordPress database, this will do:

    global $wpdb;
    $rows = $wpdb->get_results( ‘SELECT * FROM publications’ );
    foreach ( $rows as $row ) {
    // Process row…
    }

    Does this help?
    Peter

    Thread Starter pabirds

    (@pabirds)

    The table is not in the WordPress database.

    Plugin Author Passionate Programmer Peter

    (@peterschulznl)

    Ho @pabirds,

    So you need to connect to a remote MySQL database from your WordPress installation? In that case you cannot use global $wpdb.

    There are probably a number of ways to solve this. If you prefer to use the PHP files mentioned in your first post, you can copy those files to your uploads/code-manager folder and import them from your local path with require_once. You need to make sure to copy the whole file structure if these files import other files. Also make sure all imports are relative. To import them you can use the following code:

    $upload_dirs = wp_upload_dir(); // Get uploads directory
    $basedir = $upload_dirs['basedir']; // Get base directory
    $yourfile = $basedir . '/code-manager/siteguide2018.php'; // Get your PHP file

    But there is an easier way. If you install WP Data Access, you can add a remote database connection in WP Data Access and then use the WP Data Access API to connect to your remote database. The free version of WP Data Access will do. Adding a remote connection is explained here:

    https://wpdataaccess.com/docs/remote-databases/mysql-mariadb/

    And here is a code example you can use:

    $wpdadb = \WPDataAccess\Connection\WPDADB::get_db_connection( 'your-remote-database' );
    if ( null === $wpdadb ) {
    	// Connection failed...
    } else {
    	// Get 
    	$rows = $wpdadb->get_results( 'select * from publications', 'ARRAY_N' );
    	foreach ($rows as $row) {
    		foreach ($row as $col) {
    			echo $col . '<br/>';
    		}
    	}
    }

    Variable $wpdadb in the example above represents a WP Data Access object that inherits WordPress class wpdb. This allows you to use all wpdb methods. There are many example on the internet or you might already be familiar with it. ??

    Does this solve the issue?

    Thanks,
    Peter

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘php/sqli query’ is closed to new replies.