我正在构建这个Web应用程序,我正在尝试使用Jquery和PHP来编写脚本。我在测试MAMP页面时遇到的问题是显示PHP变量值。当我用基本的HTML和PHP创建一个非常简单的测试页面时,我的php代码运行成功。你可以在这里看到它:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
$db = mysql_connect("localhost", "student", "student")
or die("I cannot connect to the database because: " . mysql_error());
mysql_select_db("Dealership", $db);
$result = mysql_query("SELECT COUNT( * ) AS 'Number of Cars' FROM CARS;") or die("Error in query: '$query'");
while($row = mysql_fetch_array($result))
{
$numberOfCars = $row['Number of Cars'];
}
mysql_close($db);
?>
<?php
print "<p>Number of Vehicles on the Lot: " . $numberOfCars . "Var Dump:"; var_dump($numberOfCars); print "</p>";
?>
</body>
</html>
当我尝试在我的网页上使用相同的代码时,变量$ numberOfCars的值将不会显示。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Car Dealership Portal</title>
<link rel="stylesheet" type="text/css" href="jquery.mobile.flatui.css" />
<script src="jquery-1.10.2.min.js"></script>
<script src="jquery.mobile-1.3.2.min.js"></script>
</head>
<body>
<div data-role="page">
<?php
$db = mysql_connect("localhost", "student", "student")
or die("I cannot connect to the database because: " . mysql_error());
mysql_select_db("Dealership", $db);
$result = mysql_query("SELECT COUNT( * ) AS 'Number of Cars' FROM CARS;") or die("Error in query: '$query'");
while($row = mysql_fetch_array($result))
{
$numberOfCars = $row['Number of Cars'];
}
mysql_close($db);
?>
<div data-role="panel" id="panel" data-position="right" data-theme="a" data-display="push"></div>
<div data-role="header">
<a data-iconpos="notext" data-role="button" data-icon="home" title="Home">Home</a>
<h1>Car Dealership Portal</h1>
<a data-iconpos="notext" href="#panel" data-role="button" data-icon="flat-menu"></a>
</div>
<div data-role="content" role="main">
<input type="text" placeholder="Search for a Vehicle" />
<div>
<?php print "<p>Number of Vehicles on the Lot: " . $numberOfCars . "Var Dump:"; var_dump($numberOfCars); print "</p>"; ?>
</div>
<div data-role="collapsible-set">
<div data-role="collapsible" data-collapsed="false">
<h3>Number of Appointments Today: 3</h3>
<p>Jon Snow 11:00am</p>
<p>Joffrey Baratheon 1:00pm</p>
<p>Eddard Stark 3:00pm</p>
</div>
<div data-role="collapsible">
<h3>Number of Cars in Shop: 5</h3>
<p>Porsche 911 GT3 Arya Stark</p>
<p>Nissan GTR Ygritte</p>
<p>BMW M3 Stannis Baratheon</p>
<p>Audi RS6 Avant Tywin Lannister</p>
<p>Lamborghini Aventador Petyr Baelish</p>
</div>
</div>
</div>
</div>
</body>
</html>
您可以在此处查看代码:http://pastebin.com/GwDbHwY6
Stack Overflow不允许我在这里发布代码。
答案 0 :(得分:1)
变化:
SELECT COUNT( * ) AS 'Number of Cars' FROM CARS
为:
SELECT COUNT( * ) AS 'CarCount' FROM CARS
然后改变:
$numberOfCars = $row['Number of Cars'];
为:
$numberOfCars = $row['CarCount'];
这样做可以解决问题。