i've been working to do some basic conversions from mysqli to pdo as a learning exercise (and to future proof of course).
Here is the code i'd like to work
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Syndicate Gamers Server Officers</title> <link rel="stylesheet" href="main.css" /> </head> <body> <h1>Syndicate Gamers Server Officer Control Page</h1> <form action="" method="post"> <input type="submit" name="solist" value="SO List" /> <input type="submit" name="activelist" value="Active List" /> </form> <?php require_once "connect.php"; function solist() { global $DBH try { $STH = $DBH->query('select members.name as mname, syndicate_sourcebans.sb_srvgroups.name as sbg ,groups.g_title as fg,sg_servers.server_title as resp from members inner join groups on groups.g_id=members.OrigFGrp left join sg_servers on BIN(sg_servers.server_id)=BIN(members.SOServer) left join syndicate_sourcebans.sb_admins on SteamToInt(syndicate_sourcebans.sb_admins.authid)=members.steamid left join syndicate_sourcebans.sb_srvgroups on syndicate_sourcebans.sb_srvgroups.id=members.origsbgrp where members.member_group_id=17 order by sg_servers.server_title'); $STH->setFetchMode(PDO::FETCH_ASSOC); $STH->execute(); echo "<table> <tr> <th>Name</th> <th>Original Sourcebans Group</th> <th>Original Forum Group</th> <th>Assigned Server</th> </tr>"; while($row = $STH->fetch()) { echo "<tr>"; echo "<td>" . $row['mname'] . "</td>"; echo "<td>" . $row['sbg'] . "</td>"; echo "<td>" . $row['fg'] . "</td>"; echo "<td>" . $row['resp'] . "</td>"; echo "</tr>"; } } catch(PDOException $e){ print $e->getMessage(); } $DBH = null; } ?> </body> </html> connect.php
<?php $user="redacted"; $pass="redacted"; $dbname="redacted_db"; $db="db.redacted.net"; $DBH = new PDO("mysql:host=$db;dbname=$dbname", $user, $pass); $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); ?> Using the above code will populate the data requested upon pressing the button.
In lieu of the global call, i also attempted the below, which results in no data being presented from the DB.
function solist($DBH) { I've made modifications based on Fred and other's suggestions (mainly actually calling the function!!! thanks all) and from reading the links he provided with much success. The only problem remaining is attempting to drop the global per the advice of many other forums.
the primary suggestion from all was to "call" the function, so i added
if (isset($_POST['solist'])) { solist(); } to the bottom of my codeblock
name="solist"to call the function? If so, you need to do something likeif(isset($_POST['solist'])){ // call the solist function }function solist() {...}will NOT happen till you "call" the function, something which isn't shown nor mentioned in your question.echo solist();there. Function called. If you're indeed calling it, show us where. Functions don't happen on their own, that's why they're called "functions" => php.net/manual/en/language.functions.php - php.net/manual/en/functions.user-defined.php - php.net/manual/en/functions.arguments.phpfunction solist(PDO $DBH)" you don't need to do that. Just call the function, no need to pass a parameter, since you've already set it as global. If you're not going to declare global, then just dofunction solist($DBH)