尝试运行此脚本时,我的浏览器出现以下错误:
Parse error: syntax error, unexpected end of file in C:\wamp\www\server2.php on line 35
注意:第35行是脚本中的最后一行。对不起,我是PHP的新手
<?php
// get the command
$command = $_REQUEST['command'];
// determine which command will be run
if($command == "getAnimalList") {
// return a list of animals
echo "bird,dog,cat,cow,sheep";
} else if($command == "getAnimalSound") {
// get the animal parameter and send the right response
$animal = $_REQUEST['cat'];
// fetch the sound of the animal from the database
$username = "root"; $password = ""; $hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("rosslocalhost1",$dbhandle)
or die("Could not select examples");
//execute the SQL query and try to return a record
$result = mysql_query("SELECT sound FROM animalsounds WHERE name='$animal'");
if (!$result) {
echo 'Dont know' .mysql_error();
exit;
}
$row = mysql_fetch_array($result);
echo $row['sound'];
?>
答案 0 :(得分:3)
您已错过elseif
<{1}}声明中的结束括号
if($command == "getAnimalList")
答案 1 :(得分:0)
这是你的代码正确缩进。您没有正确关闭else if ($command == "getAnimalSound")
<?php
// get the command
$command = $_REQUEST['command'];
// determine which command will be run
if ($command == "getAnimalList") {
// return a list of animals
echo "bird,dog,cat,cow,sheep";
} else if ($command == "getAnimalSound") {
// get the animal parameter and send the right response
$animal = $_REQUEST['cat'];
}
// fetch the sound of the animal from the database
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("rosslocalhost1", $dbhandle) or die("Could not select examples");
//execute the SQL query and try to return a record
$result = mysql_query("SELECT sound FROM animalsounds WHERE name='$animal'");
if (!$result) {
echo 'Dont know' . mysql_error();
exit;
}
$row = mysql_fetch_array($result);
echo $row['sound'];
?>