无法在页面上显示数据

时间:2013-08-11 22:25:01

标签: php html mysql

所以,我最近将一个数据库从一个主机移动到另一个主机,并且还必须移动用于显示数据的HTML和php页面,并且由于某种原因它根本不会在页面上显示数据。我知道我的php页面正在连接到数据库,因为当我在if页面中使用简单的php语句时,它会显示正确的数据。
该网站在以前的服务器和数据库主机上正常工作。

我的HTML代码是:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>Olympic Scores</title>
    <link href="../favicon.ico" rel="shortcut icon" />
    <link href="css/tasks.css" rel="stylesheet" type="text/css" />
    <link href="css/style2.css" rel="stylesheet" type="text/css" />
    <link href="css/table.css" rel="stylesheet" type="text/css" /><script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
    <style type="text/css">label.error { 
            float: none; color: red; padding-left: 0.5em; padding-right: 1em; vertical-align: top; 
        }
    </style>
    <script>
            $(document).ready(function(){
                $("#medalValues").validate();
            });
        </script>
    <style type="text/css">
    </style>
</head>
<body>
<div class="container" id="wrapper">
<div class="header_style">
<h1><a href="link"><strong>Back to </strong>Portfolio</a></h1>
<div class="clear"></div>
</div>

<header>
<h1>Olympics<span> - London 2012</span></h1>

<h2>Medals Table</h2>

<h2></h2>
</header>

<div class="image_body">
<div class="image_effect" id="image_click">
<p>Copyright free image of a 2004 Olympics Gold Medal</p>

<p>2004 Olympics - Athens</p>
</div>
<span class="image_note">Keep clicked to show image</span></div>

<div class="content" id="home_page">
<p class="titles">Please assign the weight of the Gold Medals, Silver Medals and Bronze Medals:</p>

<p>(Only numbers are allowed)</p>

<nav>
<div>
<form id="medalValues" method="get"><label class="gold_label" for="gold_value">Gold Medals:</label> <input id="gold_value" name="gold_value" required="required" type="number" value="3" /> <label class="silver_label" for="silver_value">Silver Medals:</label> <input id="silver_value" name="silver_value" required="required" type="number" value="2" /> <label class="bronze_label" for="bronze_value">Bronze Medals:</label> <input id="bronze_value" name="bronze_value" required="required" type="number" value="1" /><br />
<p>Calculate Score Based on</p>
<br />
<label for="medals_only">Medals Only</label> <input id="medals_only" name="radio_button" onclick="no_medals_formula();" required="required" type="radio" value="opt1" /> <!--OnClick runs the function to display the formula used--> <label for="population">Population</label> <input id="population" name="radio_button" onclick="population_formula();" required="required" type="radio" value="opt2" /> <!--OnClick runs the function to display the formula used--> <label for="gdp">GDP</label> <input id="gdp" name="radio_button" onclick="gdp_formula();" required="required" type="radio" value="opt3" /> <!--OnClick runs the function to display the formula used--> <input type="submit" value="Submit" /><br />
<p id="formula_used"></p>


<table id="score_results">
</table>
</form>
</div>
<script>
                            //When document is ready
                            $(function() {

                                //Handles form submission
                                $('#medalValues').submit(function() {

                                    var gold_value, silver_value, bronze_value, medals_only, population, gdp;

                                    //Gets the values from the weight inputs, checks which radio is checked
                                    gold_value = $("#gold_value").val();
                                    silver_value = $("#silver_value").val();
                                    bronze_value = $("#bronze_value").val();
                                    medals_only = $("#medals_only").is(':checked');
                                    gdp = $("#gdp").is(':checked');
                                    population = $("#population").is(':checked');

                                    getResults( gold_value, silver_value, bronze_value, medals_only, population, gdp );

                                    //Stops the form submitting
                                    return false;
                                });

                            });

                            function getResults ( gold_value, silver_value, bronze_value, medals_only, population, gdp ) {

                                $.get('view.php', {gold_value: gold_value, silver_value: silver_value, bronze_value: bronze_value, medals_only: medals_only, population: population, gdp: gdp} ,  function(data) {
                                    postResults(data);
                                });
                            }

                            function postResults (data) {

                                //Turns the string into json
                                var json = $.parseJSON(data);

                                //Inputs the table headings for the html table
                                var output = "<colgroup><col class=oce-first /><col class=oce-second /><col class=oce-third /><col class=oce-fourth /><col class=oce-fifth /></colgroup><tr><th class=rank_header scope=col>Rank</th><th class=rank_country scope=col>Country Name</th><th class=score_header scope=col>Score</th><th class=gdp_header scope=col>GDP</th><th class=population_header scope=col>Population</th>";

                                for (i = 0; i < json.length; i++) {

                                    //Loops through and posts them into a html table
                                    output+= "<tr class=spacing>"
                                    output+= "<td id=selected_rank>" + (i+1) + "</td>"
                                    output+= "<td>" + json[i].country + "</td>"
                                    output+= "<td id=selected_score>" + json[i].score + "</td>"
                                    output+= "<td>" + json[i].gdp + "</td>"
                                    output+= "<td>" + json[i].population + "</td>"
                                    output+= "</tr>"
                                }

                            // Push the output html into the results table
                            $("#score_results").html(output);
                            }
                        </script></nav>
</div>

<div class="footer_style">
<h1><strong>Part of my </strong>Portfolio</h1>
<div class="clear"></div>
</div>
<!--Functions to update the element wihth id=formula_used to change the text depending on which radio button the user selected--><script>
                    function no_medals_formula() {
                        document.getElementById("formula_used").innerHTML="The Formula used was: (gold * $gold_value) + (silver * $silver_value) + (bronze * $bronze_value) <br/>Rounded to 2 d.p";
                    }
                    function population_formula() {
                        document.getElementById("formula_used").innerHTML="The Formula used was: ((gold * $gold_value) + (silver * $silver_value) + (bronze * $bronze_value)) / (population / Million) <br/>Rounded to 2 d.p";
                    }
                    function gdp_formula() {
                        document.getElementById("formula_used").innerHTML="The Formula used was: ((gold * $gold_value) + (silver * $silver_value) + (bronze * $bronze_value)) / (gdp / 10 Billion) <br/>Rounded to 2 d.p";
                    }
                </script></div>
</body>
</html>

我的php代码是:

<?php

require_once 'MDB2.php'

$hostname="hostname.hostedresource.com";
$username="username";
$password="password";
$dbname="dbname";
$usertable="usertable";
$yourfield = "ISO_id";

mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");
mysql_select_db($dbname);

# Check If Record Exists
$dsn = "mysql://$username:$password@$hostname/$dbname"; 

$db =& MDB2::connect($dsn); 
if(PEAR::isError($db)){ 
    die($db->getMessage());
}

$db->setFetchMode(MDB2_FETCHMODE_ASSOC);

//Gets the values the user assigned to Gold, Silver and Bronze
    $gold_value = $_GET["gold_value"];
    $silver_value = $_GET["silver_value"];
    $bronze_value = $_GET["bronze_value"];
    $medals_only = $_GET["medals_only"];
    $population = $_GET["population"];
    $gdp = $_GET["gdp"];

//If statements that choose which sql statement to run.
//sql statement that gets run when user selects to calculate score without using population or gdp, orders it by score, and stops after reaching 10.

if ($medals_only == 'true') {
    $sql="SELECT gold, silver, bronze, gdp, population, country_name, ROUND(((gold * $gold_value) + (silver * $silver_value) + (bronze * $bronze_value)),2) as score
    FROM $usertable
    ORDER BY score DESC LIMIT 0,10";
//sql statement that gets run when user selects to calculate score witH population, orders it by score, and stops after reaching 10.
}
else if($population == 'true'){ 
    $sql="SELECT gold, silver, bronze, gdp, population, country_name, ROUND(((gold * $gold_value) + (silver * $silver_value) + (bronze * $bronze_value)) / (population / 1000000),2) as score 
    FROM $usertable
    ORDER BY score DESC LIMIT 0,10";
//sql statement that gets run when user selects to calculate score with gdp, orders it by score, and stops after reaching 10.
}
else if($gdp == 'true'){ 
    $sql="SELECT gold, silver, bronze, gdp, population, country_name, ROUND(((gold * $gold_value) + (silver * $silver_value) + (bronze * $bronze_value)) / (gdp / 10000000000),2) as score 
    FROM $usertable
    ORDER BY score DESC LIMIT 0,10";
}   

$res =& $db->query($sql);
if(PEAR::isError($res)){
        die($res->getMessage());
}

echo json_encode($res->fetchAll() );    

?>

当我直接进入php page-view.php时,我收到了错误

  

MDB2错误:未知错误

1 个答案:

答案 0 :(得分:0)

require_once 'MDB2.php'

应该是:

require_once('MDB2.php');

没有“;”而且函数语法错误。这是我到目前为止注意到的,MDB2.php所在的位置可能存在另一个问题,因为您需要包含MDB2.php所在的父目录。