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.

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

报告相同问题?

悬赏问题

  • ¥15 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页