0

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.

7
  • There's no way anyone on here could tell what the issue is with your other page if you do not post the other page's code. Commented Dec 11, 2013 at 22:26
  • 4
    Your question has no relevance to javascript? Commented Dec 11, 2013 at 22:26
  • 2
    Learning to use mysql_* is rather useless since it's deprecated. Check the manual for a big red box, it'll give you better alternatives. Commented Dec 11, 2013 at 22:27
  • What is the problem? What does this mean? "When I try to use that same code on my webpage the value of the variable $numberOfCars will not display." Commented Dec 11, 2013 at 22:27
  • Sorry it wouldn't let me post my code here I added a paste Commented Dec 11, 2013 at 22:27

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.