dongqiu9018 2013-12-11 22:24
浏览 40
已采纳

在同一个网页上使用jquery和php

I am building this web application and I am attempting to use Jquery and PHP for the scripting. The issue that I am running into while testing the page on MAMP is displaying PHP variable values. My php code runs successfully when I create a very simple test page with just basic HTML and PHP. You can see it here:

<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>

When I try to use that same code on my webpage the value of the variable $numberOfCars will not display.

<!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>

You can check out the code here: http://pastebin.com/GwDbHwY6

Stack Overflow wouldn't let me post the code here.

  • 写回答

1条回答 默认 最新

  • doubi7306 2013-12-11 23:14
    关注

    Change:

    SELECT COUNT( * ) AS  'Number of Cars' FROM CARS
    

    to:

    SELECT COUNT( * ) AS  'CarCount' FROM CARS
    

    and then change:

    $numberOfCars = $row['Number of Cars'];
    

    to:

    $numberOfCars = $row['CarCount'];
    

    Doing so should fix the problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败