Jump to content

PHP PDO Empty Page?


zeus1999999

Recommended Posts

Hello community!

I have a problem i have this script (below) and when i access the file with right switch cases i become a white screen

<?php


    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "regressiontest";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        switch($operation){
            case "managerList": {
                $stmt = $conn->query("
                                    SELECT
                                        *
                                    FROM
                                        regressiontest.campaign_run;");

            } break;

        }


        $stmt->execute();

        $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 

        $output = $stmt->fetchAll();

        switch($compute){
            case "json": {
                echo json_encode($output, JSON_NUMERIC_CHECK);              
            }
        }


    }
    catch(PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
    $conn = null;
?>

and this cases works fine

<?php


    //check for error output
    if(isset($_GET['error'])){
        if($_GET['error'] == "false"){
            error_reporting(0);
        } else if($_GET['error'] == "true"){
            ini_set('error_reporting', E_ALL);
        } else {
            error_reporting(0);
        }
    } else {
        error_reporting(0);
    }

    //check for timelimit
    if(isset($_GET['timelimit'])){ set_time_limit($_GET['timelimit']); } else { set_time_limit(0); }

    //check for operation
    if(isset($_GET['operation'])){ $operation = $_GET['operation']; } else { exit; }

    //check for compute
    if(isset($_GET['compute'])){ $compute = $_GET['compute']; } else { exit; }

    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "regressiontest";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        switch($operation){
            case "managerFilter": {
                $stmt = $conn->prepare("
                                    SELECT 
                                        GROUP_CONCAT(DISTINCT (campaign_name) order by campaign_name asc) AS uniqueCampaignName,
                                        GROUP_CONCAT(DISTINCT (test_type) order by test_type asc) AS uniqueTestType,
                                        GROUP_CONCAT(DISTINCT (testee) order by testee asc) AS uniqueTestee,
                                        GROUP_CONCAT(DISTINCT (testee_version) order by testee_version asc) AS uniqueTesteeVersion
                                    FROM
                                        regressiontest.campaign_run;
                                    ");                 
            } break;

            case "testcaseStatistics": {

                $testcaseId = $_GET['testcaseId'];

                $stmt = $conn->prepare("
                                    SELECT 
                                        terminal,
                                        AVG(runtime_ms) AS avg_runtime_ms,
                                        COUNT(id) AS count_runs,
                                        COUNT(CASE
                                            WHEN result = 0 THEN 1
                                            ELSE NULL
                                        END) AS passed_runs,
                                        COUNT(CASE
                                            WHEN result = 1 THEN 1
                                            ELSE NULL
                                        END) AS failed_runs,
                                        COUNT(id) - COUNT(CASE
                                            WHEN result = 0 THEN 1
                                            ELSE NULL
                                        END) - COUNT(CASE
                                            WHEN result = 1 THEN 1
                                            ELSE NULL
                                        END) AS aborted_runs,
                                        COUNT(CASE
                                            WHEN result = 0 THEN 1
                                            ELSE NULL
                                        END) / COUNT(id) * 100 AS success_rate,
                                        COUNT(CASE
                                            WHEN result = 1 THEN 1
                                            ELSE NULL
                                        END) / COUNT(id) * 100 AS fail_rate
                                    FROM
                                        tc_result
                                    WHERE
                                        tc_id = ".$testcaseId."
                                    GROUP BY terminal
                                    ");

            } break;

            case "testcaseRuns": {

                $testcaseId = $_GET['testcaseId'];

                $stmt = $conn->prepare("
                                    SELECT 
                                        tc_result.campaign_id,
                                        tc_result.tc_id,
                                        tc_result.id,
                                        tc_result.terminal,
                                        tc_result.createdate,
                                        tc_result.runtime_ms,
                                        tc_result.trx,
                                        tc_result.result,
                                        campaign_run.testee
                                    FROM
                                        tc_result
                                            INNER JOIN
                                        campaign_run ON tc_result.campaign_id = campaign_run.id
                                    WHERE
                                        tc_id = ".$testcaseId."
                                    ");

            }

        }


        $stmt->execute();

        $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 

        $output = $stmt->fetchAll();

        switch($compute){
            case "json": {
                echo json_encode($output, JSON_NUMERIC_CHECK);              
            }
        }


    }
    catch(PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
    $conn = null;
?>
Link to comment
Share on other sites

switch($operation){
Where is $operation being set?

 

No $operation means no $stmt and so the

$stmt->execute();
will fail (calling a method on a non-object) and PHP will die.

 

If you aren't seeing that error message then your environment is not correctly set up for PHP development. Find your php.ini and change two settings:

error_reporting = -1
display_errors = on
Then restart your webserver and try that first script again.
Link to comment
Share on other sites

switch($operation){
Where is $operation being set?

 

No $operation means no $stmt and so the

$stmt->execute();
will fail (calling a method on a non-object) and PHP will die.

 

If you aren't seeing that error message then your environment is not correctly set up for PHP development. Find your php.ini and change two settings:

error_reporting = -1
display_errors = on
Then restart your webserver and try that first script again.

 

<?php
	

	//check for error output
	if(isset($_GET['error'])){
		if($_GET['error'] == "false"){
			error_reporting(0);
		} else if($_GET['error'] == "true"){
			ini_set('error_reporting', E_ALL);
		} else {
			error_reporting(0);
		}
	} else {
		error_reporting(0);
	}

	//check for timelimit
	if(isset($_GET['timelimit'])){ set_time_limit($_GET['timelimit']); } else {	set_time_limit(0); }

	//check for operation
	if(isset($_GET['operation'])){ $operation = $_GET['operation']; } else { exit; }
	
	//check for compute
	if(isset($_GET['compute'])){ $compute = $_GET['compute']; } else { exit; }
	
	$servername = "localhost";
	$username = "root";
	$password = "";
	$dbname = "regressiontest";
	
	try {
	    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
	    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	    
	    switch($operation){
	    	case "managerFilter": {
	    		$stmt = $conn->prepare("
	    							SELECT 
									    GROUP_CONCAT(DISTINCT (campaign_name) order by campaign_name asc) AS uniqueCampaignName,
									    GROUP_CONCAT(DISTINCT (test_type) order by test_type asc) AS uniqueTestType,
									    GROUP_CONCAT(DISTINCT (testee) order by testee asc) AS uniqueTestee,
									    GROUP_CONCAT(DISTINCT (testee_version) order by testee_version asc) AS uniqueTesteeVersion
									FROM
									    regressiontest.campaign_run;
	    							"); 	    		
	    	} break;
	    	
	    	case "managerList": {
	    		$stmt = $conn->prepare("
                                    SELECT
                                        *
                                    FROM
                                        regressiontest.campaign_run;");
	    	} break;
	    	
	    	case "testcaseStatistics": {
	    		
	    		$testcaseId = $_GET['testcaseId'];
	    		
	    		$stmt = $conn->prepare("
	    							SELECT 
									    terminal,
									    AVG(runtime_ms) AS avg_runtime_ms,
									    COUNT(id) AS count_runs,
									    COUNT(CASE
									        WHEN result = 0 THEN 1
									        ELSE NULL
									    END) AS passed_runs,
									    COUNT(CASE
									        WHEN result = 1 THEN 1
									        ELSE NULL
									    END) AS failed_runs,
									    COUNT(id) - COUNT(CASE
									        WHEN result = 0 THEN 1
									        ELSE NULL
									    END) - COUNT(CASE
									        WHEN result = 1 THEN 1
									        ELSE NULL
									    END) AS aborted_runs,
									    COUNT(CASE
									        WHEN result = 0 THEN 1
									        ELSE NULL
									    END) / COUNT(id) * 100 AS success_rate,
									    COUNT(CASE
									        WHEN result = 1 THEN 1
									        ELSE NULL
									    END) / COUNT(id) * 100 AS fail_rate
									FROM
									    tc_result
									WHERE
									    tc_id = ".$testcaseId."
									GROUP BY terminal
	    							");
	    		 
	    	} break;
	    	
	    	case "testcaseRuns": {
	    		
	    		$testcaseId = $_GET['testcaseId'];
	    		
	    		$stmt = $conn->prepare("
				    				SELECT 
									    tc_result.campaign_id,
									    tc_result.tc_id,
									    tc_result.id,
									    tc_result.terminal,
									    tc_result.createdate,
									    tc_result.runtime_ms,
									    tc_result.trx,
									    tc_result.result,
									    campaign_run.testee
									FROM
									    tc_result
									        INNER JOIN
									    campaign_run ON tc_result.campaign_id = campaign_run.id
									WHERE
									    tc_id = ".$testcaseId."
	    							");
	    		
	    	}
	    	
	    }
	    

	    $stmt->execute();
	    
	    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
	    
		$output = $stmt->fetchAll();

	    switch($compute){
	    	case "json": {
				echo json_encode($output, JSON_NUMERIC_CHECK);	    		
	    	}
	    }
	    
	    
	}
	catch(PDOException $e) {
	    echo "Error: " . $e->getMessage();
	}
	$conn = null;
?>

full script only case managerList wont work

Link to comment
Share on other sites

PHP won't show a white screen without some indication as to what went wrong. Did you do the settings thing I said?

 

Also, you've missed the point of how prepared statements work. Don't put values straight into the queries:

"WHERE
	tc_id = ".$testcaseId
Use a placeholder, like :testcaseId

"WHERE
	tc_id = :testcaseId"
and then use the actual value with either bindParam() or execute(). The former would be easier for your code.

$stmt = "...";
$stmt->bindParam(":testcaseId", $testCaseId, PDO::PARAM_INT);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.