2

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

16
  • improper paste, correcting. sorry Commented Feb 5, 2015 at 18:52
  • 5
    it doesnt look like you ever call on your method solist Commented Feb 5, 2015 at 18:52
  • 2
    Are you relying on name="solist" to call the function? If so, you need to do something like if(isset($_POST['solist'])){ // call the solist function } Commented Feb 5, 2015 at 18:55
  • 1
    You need to tell us how you're calling the function. Anything inside 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.php Commented Feb 5, 2015 at 19:02
  • 1
    "or try to use function 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 do function solist($DBH) Commented Feb 5, 2015 at 19:13

1 Answer 1

1

As per your originally/revised posted question/code:

Since you are relying on name="solist" as per your submit button to call the function, you need to do something to the effect of:

if(isset($_POST['solist'])){ // call the solist function echo solist(); } 

The function won't fire up on its own, it needs to be called.


"or try to use function solist(PDO $DBH)"

You don't need to do that. Just call the function; there is no need to pass a parameter, since you've already set it as global.

If you're not going to declare it as global, then just do function solist($DBH)

Sign up to request clarification or add additional context in comments.

1 Comment

The OP's code currently posted on screen was used from comments given, and did not mark it as an edit. If the DV was based on that, it needs to be retracted. The answer given here was based on OP's originally posted code that did not contain my solution and OP asked me to post it as an answer. An explanation would be beneficial so that if anything in the answer is wrong and/or false, then it will be corrected.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.