• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

RESOLVED Trouble converting [php_insert] into Snippet

Jscudder

New member
Messages
3
Likes
1
Points
1
#1
I am having a problem converting a page that used two [php_insert]s to connect to and select from an external SQL database. Here is the insert for connecting to the database:
----------------------------------------------------------
PHP:
// connection to the sql database
    $myDB = new wpdb('names', 'xxxxxxxxxxx', 'names', 'localhost');
    $myDB->show_errors();

// display parameters
$where = '';
$start = '';
$end = '';

// sorting commands
if (isset($_GET["start"])) {
   $start = $_GET["start"];}

if (empty($start)) {
   $start = 'a'; $end = 'z';
}
else {
   $end= $start;
}
----------------------------------------------------------

And here is the code for selecting from the database:

----------------------------------------------------------
PHP:
$sql = "SELECT ID_PERSON, First, Last, City, State, ZIP, Region, Contact, Email, Pastor FROM community WHERE community.ID_FORMER = 0 AND Region != '6' ";

$where = "ORDER BY Last, First ";

$rows = $myDB->get_results($sql.$where);

// $rows = $myDB->get_results("SELECT ID_PERSON, First, Last, City, State, ZIP, Region, Contact, Email, Pastor
// FROM community WHERE community.ID_FORMER = 0 AND Region != '6' ORDER BY Last, First");

foreach ($rows as $obj):
if ($obj->First != "First")
{
// begin display based on first letter of last name
$first = strtolower($obj->Last);
$first = substr($first, 0, 1);

// break loop conditional
if (strcmp($first, $start) >= 0 && strcmp($first, $end) <= 0)
{
  $contact = $obj->Contact;

  echo "<tr>";
  //echo "<td width='20%'>"; echo "</td>";

  if ($obj->Pastor == '1') {echo "<td>";  echo ("<a href='../participant/?id="); echo $obj->ID_PERSON; echo "'>";
    echo "<font color='#800000'><b>"; echo $obj->First; echo " "; echo $obj->Last; echo "</a><font/></td>";}
  else {echo "<td>"; echo ("<a href='../participant/?id="); echo $obj->ID_PERSON; echo "'>"; echo $obj->First;
    echo " "; echo $obj->Last; echo "</a></td>";}

  echo "<td>"; echo $obj->City; echo "</td>";
echo "<td style='text-align:center'>"; echo $obj->State; echo "</td>";
echo "<td style='text-align:center'>"; echo $obj->ZIP; echo "</td>";
echo "<td style='text-align:center'>"; echo $obj->Region; echo "</td>";
echo "<td style='text-align:center'>"; echo $obj->Contact; echo "</td>";
echo "<td style='text-align:left'>"; echo $obj->Email; echo "</td>";
echo "<td style='text-align:center'>"; echo $obj->ID_PERSON; echo "</td>";

  //echo "<td >"; echo "</td>";
  echo "</tr>";

  }
}
endforeach;
----------------------------------------------------------

With the old Woody Snippets plugin they worked great. I converted them to 2 separate snippets named [wbcr_php_snippet id="7884" title="Connect to SQL Database"] and [wbcr_php_snippet id="7888" title="Select from SQL Database"] respectively.

Upon inserting the above shortcode where the old [insert_php] [/insert_php] code was, my page was completely blank except for parts of the header graphic. Is there a problem with my old code? I and using the latest Wordpress 5.2.3 version. I had no problem at all creating and inserting a TXT snippet.

John
 
Last edited:

Temyk

Developer & Support
Messages
1,129
Likes
42
Points
48
#2
Hello.

Different snippets inserted in the same place do not see each other's variables. All variables that you use between snippets must be declared as global in both snippets.
Add this line to the beginning of both snippets:
PHP:
global $myDB;
global $start;
global $end;