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.

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?