简体   繁体   中英

Why is this line breaking my page?

My PHP file starts out with

<?php

$tm = new TeamManager();

?>

<script type="text/javascript">
    var team = <?php echo json_encode($tm->GetWholeTeam()); ?>;
</script>

and the rest isn't being read, for reasons unknown. I've narrowed it down to the line <?php echo json_encode($tm->GetWholeTeam()); ?> <?php echo json_encode($tm->GetWholeTeam()); ?> but no errors are showing even if I add

error_reporting(-1);
ini_set('display_errors', 'On'); 

to the top of the script. For reference, my TeamManager class is defined in

<?php

// Gets included in functions.php


final class MySqlInfo
{

    const TEAMTABLENAME = 'mems';
    const PROJSTABLENAME = 'projs';

    public static function getTeamTableName ( )
    {
        return self::TEAMTABLENAME;
    }

    public static function getProjectsTableName ( )
    {
        return self::PROJSTABLENAME;
    }
}

final class MethodResult
{

    public $succeeded; 
    public $message; 

    public function MethodResult ( $succeededInit = NULL, $messageInit = NULL )
    {
        $this->$succeeded = $succeededInit;
        $this->$message = $messageInit;
    }   

}

final class TeamMember 
{
    public $name; // team member name
    public $title; // team member title
    public $bio; // team member bio
    public $sord; // team member sort order
    public $picfn; // team member profile picture file name

    public function TeamMember ( $name_init, $title_init, $bio_init, $sord_init, $picfn_init )
    {
        $this->name = $name_init; 
        $this->title = $title_init;
        $this->bio = $bio_init;
        $this->sord = $sord_init;
        $this->picfn = $picfn_init;
    }
}

final class TeamManager
{

    public function TeamManager ( )
    {
        // ....
    }

    public function addMember ( TeamMember $M )
    {

        $q = "INSERT INTO " . MySqlInfo::TEAMTABLENAME . " (" . implode( ',', array($M->name, $M->title, $M->bio, $M->sord, $M->picfn) ) . ") VALUES ('" . implode('\',\'', array($_POST['fullname'], $_POST['title'], $_POST['bio'], $_POST['sord'], $targetFileName)) . "')";
        // ^ query for inserting member M to the database

        if (!$wpdb->query($q))
        {
            return new MethodResult(false, 'Query to insert new team member failed');
        }
        // else
        return new MethodResult(true, 'Successfully added new member' . $M->name);
    }

    public function getWholeTeam ( )
    {
        $q = "SELECT name,title,bio,sord,picfn FROM " . MySqlInfo::TEAMTABLENAME;
        $teamRows = $wpdb->query($q);
        return $teamRows; 
    }
}

?>

but I am not getting any errors on that page either. I've also run everything through an online PHP validator and do not get any errors. What gives?

Variable scope:

    $teamRows = $wpdb->query($q);
                ^^^^^

That is undefined within your getWholeTeam() method (and in all the methods that use it, in fact) You need a

 global $wpdb;

in there.

You should have AT LEAST gotten an undefined variable warning on $wpdb and a fatal error for "calling member function on non-object". That suggests you've got all debug options turned off: you need to enable error_logging and display_errors when you're debugging. They should never be off in the first place on a devel system.

Try this :

<?php

$tm = new TeamManager();

?>

<script type="text/javascript">
    var team = JSON.parse('<?php echo json_encode($tm->GetWholeTeam()); ?>');
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM