Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts

    displaying data from mysql database to a webpage

    I have got a connection now to the mysql database, how do I get the data from the database to display on the webpage

    Thank you

    Ian

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Show us what you have so far, in regards to the PHP script.

    And give us the name of the MySQL table and some column names while you're at it.


    .

  • #3
    New Coder
    Join Date
    Apr 2012
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi mlseim

    I have managed to display results now to the webpage but I need it to be like the following


    I want the results to be displayed by what the customer searches as its a property website

    so for example, in my search form I have the following

    Property Type
    Location
    Number of bedrooms
    Number of Bathrooms
    Min Price
    Max Price

    for example, I put in the following

    House
    Benfleet
    4
    2
    £100,000
    £300,000

    but when I click on search, it returns all the results that are in mysql database and not just what I am looking for

    Please help

    I have included the coding below

    Code:
    <html>
    <body>
    You have searched for a&nbsp;<?php echo $_POST["propertytype"]; ?>
    <br>
    You have searched in the location of <?php echo $_POST["location"]; ?>
    <br />
    You have searched for <?php echo $_POST["bedrooms"]; ?>&nbsp;bedrooms
    <br />
    You have searched for <?php echo $_POST["bathrooms"]; ?>&nbsp;bathrooms
    <br />
    You have searched for £<?php echo $_POST["minprice"]; ?>
    <br />
    You have searched for £<?php echo $_POST["maxprice"]; ?>
    <br><br>
    Please find your results below
    </body>
    </html>
    <br><br>
    <?php
    
    $db=mysql_connect("host", "user", "password");
    mysql_select_db("database", $db);
    
    ?>
    
    <?php
    
    $db = mysql_select_db("database") or die ("Couldnt select database");
    ?>
    
    <?php
    
    // Query database
    $result = mysql_query("SELECT * FROM properties");
    
    if (!$result)
    {
        echo "Error running query:<br>";
        trigger_error(mysql_error());
    }
    elseif(!mysql_num_rows($result))
    {
        // no records found by query.
        echo "No records found";
    }
    else
    {
        $i = 0;
        echo '<div class="container" style="float:left;">';
      while($row = mysql_fetch_array($result)) {     // Loop through results
            $i++;
    	echo '</div>';		
    	echo '<div class="textholder" style="font-family:helvetica; font-size:13px; float:left; padding-top:10px;">';
            echo "<span style=\"color:green;\"><b>Displaying record $i<br>\n</b><br></span>";
            echo "<b>" . $row['id'] . "</b><br>\n";      // Where 'id' is the column/field title in the database
    		echo "Property Type: ". $row['typeProperty'] . "<br>\n";       // as above
    		echo "Bedrooms: ". $row['bedrooms'] . "<br>\n";  // ..
    		echo "Bathrooms: ". $row['bathrooms'] . "<br>\n";  // ..
    		echo "Garden: ". $row['garden'] . "<br>\n";  // ..
    		echo "Description: ". $row['description'] . "<br>\n";  // ..
    		echo "Price: ". $row['price'] . "<br>\n";         // ..
            echo "Location: ". $row['location'] . "<br>\n";            // Where 'location' is the column/field title in the database
    	echo '</div>';
    	echo '<div style="clear:both"></div>';				
        }
    echo '</div>';	}
    
    ?>
    Kind regards

    Ian

  • #4
    New Coder
    Join Date
    Apr 2012
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    any ideas mlseim

    I am well and truly stuck at the moment

    Ian

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    First, let's see the actual search form.

    It's important to know what they are allowed to search for,
    and pricing searches you allow. Are you searching for prices
    within a range (like you have min/max), and locations exact,
    or within a range (distance)? It could get really complicated.

  • #6
    New Coder
    Join Date
    Apr 2012
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Mlseim

    Thank you for the reply

    The user would be searching for type of property, location, number of bedrooms, number of bathrooms, min price and a max price

    I have included the search form coding below

    Code:
    <form action="/saleresults.php" method="post" name="form1" id="form1" onsubmit="MM_validateForm('maxprice','','NisNum','minprice','','NisNum','section2');return document.MM_returnValue">
    <div id="fieldnames">Property Type</div><input type="text" name="propertytype" size="25" maxlength="40"/>
    <br>
    <div id="fieldnames">Location</div><input type="text" name="location" size="25" maxlength="25"/>
    <br>
    <div id="fieldnames">Number of Bedrooms</div>
    <select name="bedrooms" id="bedrooms">
                    <option value="1" selected="selected" name="bedrooms">1</option>
                    <option value="2" selected="selected" name="bedrooms">2</option>
    		    <option value="3" selected="selected" name="bedrooms">3</option>
    		    <option value="4" selected="selected" name="bedrooms">4</option>
    		    <option value="5" selected="selected" name="bedrooms">5</option>
                  </select>
    <br>
    <div id="fieldnames">Number of Bathrooms</div>
    <select name="bathrooms" id="bathrooms">
                    <option value="1" selected="selected" name="bathrooms">1</option>
                    <option value="2" selected="selected" name="bathrooms">2</option>
    		    <option value="3" selected="selected" name="bathrooms">3</option>
    		    <option value="4" selected="selected" name="bathrooms">4</option>
    		    <option value="5" selected="selected" name="bathrooms">5</option>
                  </select>
    <div id="fieldnames">Min Price</div>
     <select name="minprice" id="minprice">
                    <option value="0" selected="selected" name="minprice">&pound;0</option>
                    <option value="100,000" name="minprice">&pound;100,000</option>
                    <option value="125,000" name="minprice">&pound;125,000</option>
                    <option value="150,000" name="minprice">&pound;150,000</option>
                    <option value="175,000" name="minprice">&pound;175,000</option>
                    <option value="200,000" name="minprice">&pound;200,000</option>
                    <option value="225,000" name="minprice">&pound;225,000</option>
                    <option value="250,000" name="minprice">&pound;250,000</option>
                    <option value="275,000" name="minprice">&pound;275,000</option>
                    <option value="300,000" name="minprice">&pound;300,000</option>
                    <option value="325,000" name="minprice">&pound;325,000</option>
                    <option value="350,000" name="minprice">&pound;350,000</option>
                    <option value="375,000" name="minprice">&pound;375,000</option>
                    <option value="400,000" name="minprice">&pound;400,000</option>
                    <option value="425,000" name="minprice">&pound;425,000</option>
                    <option value="450,000" name="minprice">&pound;450,000</option>
                    <option value="475,000" name="minprice">&pound;475,000</option>
                    <option value="500,000" name="minprice">&pound;500,000</option>
                    <option value="550,000" name="minprice">&pound;550,000</option>
                    <option value="600,000" name="minprice">&pound;600,000</option>
                    <option value="650,000" name="minprice">&pound;650,000</option>
                    <option value="700,000" name="minprice">&pound;700,000</option>
                    <option value="800,000" name="minprice">&pound;800,000</option>
                    <option value="900,000" name="minprice">&pound;900,000</option>
                    <option value="100,000,0" name="minprice">&pound;1,000,000</option>
                    </select>
    
    <div id="fieldnames">Max Price</div>
                  <select name="maxprice" id="maxprice">
                    <option value="100,000" name="maxprice">&pound;100,000</option>
                    <option value="125,000" name="maxprice">&pound;125,000</option>
                    <option value="150,000" name="maxprice">&pound;150,000</option>
                    <option value="175,000" name="maxprice">&pound;175,000</option>
                    <option value="200,000" name="maxprice">&pound;200,000</option>
                    <option value="225,000" name="maxprice">&pound;225,000</option>
                    <option value="250,000" name="maxprice">&pound;250,000</option>
                    <option value="275,000" name="maxprice">&pound;275,000</option>
                    <option value="300,000" name="maxprice">&pound;300,000</option>
                    <option value="325,000" name="maxprice">&pound;325,000</option>
                    <option value="350,000" name="maxprice">&pound;350,000</option>
                    <option value="375,000" name="maxprice">&pound;375,000</option>
                    <option value="400,000" name="maxprice">&pound;400,000</option>
                    <option value="425,000" name="maxprice">&pound;425,000</option>
                    <option value="450,000" name="maxprice">&pound;450,000</option>
                    <option value="475,000" name="maxprice">&pound;475,000</option>
                    <option value="500,000" name="maxprice">&pound;500,000</option>
                    <option value="550,000" name="maxprice">&pound;550,000</option>
                    <option value="600,000" name="maxprice">&pound;600,000</option>
                    <option value="650,000" name="maxprice">&pound;650,000</option>
                    <option value="700,000" name="maxprice">&pound;700,000</option>
                    <option value="800,000" name="maxprice">&pound;800,000</option>
                    <option value="900,000" name="maxprice">&pound;900,000</option>
                    <option value="100,000,0" name="maxprice">&pound;1,000,000</option>
                    <option value="125,000,0" name="maxprice">&pound;1,250,000</option>
                    <option value="150,000,0" name="maxprice">&pound;1,500,000</option>
                    <option value="200,000,0" name="maxprice">&pound;2,000,000</option>
                    <option value="300,000,0" name="maxprice">&pound;3,000,000</option>
                  </select>
    <br><br>
               <input type="submit" value="Submit" />
               </form>

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    For all values that get sent to the script ...

    value="100,000"

    Remove the commas .... you can keep the commas on the values that people can see.

    <option value="100000" name="minprice">&pound;100,000</option>

    and remove the names from <options> ... only <select> has a name.

    <option value="100000">&pound;100,000</option>

    ===============

    When you process the form using PHP, you'll be doing a query to match the
    form variables to various columns in the MySQL table.

    The query will look sort of like this ...

    PHP Code:
    <?php

    $bedrooms
    =mysql_real_escape_string($_POST['bedrooms'];
    $bathrooms=mysql_real_escape_string($_POST['bathrooms'];
    $minprice=mysql_real_escape_string($_POST['minprice'];
    $maxprice=mysql_real_escape_string($_POST['maxprice'];

    $query="SELECT * FROM properties WHERE bedrooms='$bedrooms' AND bathrooms='$bathrooms' AND price >= '$minprice' AND price <= '$maxprice' ORDER BY price DESC";

    ?>

  • #8
    New Coder
    Join Date
    Apr 2012
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi mlseim

    I have removed the commas from the values and the names as well

    So now I need to put in below into the saleresults.php page, is that right

    <?php

    $bedrooms=mysql_real_escape_string($_POST['bedrooms'];
    $bathrooms=mysql_real_escape_string($_POST['bathrooms'];
    $minprice=mysql_real_escape_string($_POST['minprice'];
    $maxprice=mysql_real_escape_string($_POST['maxprice'];

    $query="SELECT * FROM properties WHERE bedrooms='$bedrooms' AND bathrooms='$bathrooms' AND price >= '$minprice' AND price <= '$maxprice' ORDER BY price DESC";

    ?>

  • #9
    New Coder
    Join Date
    Apr 2012
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have gone wrong somwhere as now am getting server error page

    The code I have got is below

    PHP Code:
    <html>
    <body>
    You have searched for a&nbsp;<?php echo $_POST["propertytype"]; ?>
    <br>
    You have searched in the location of <?php echo $_POST["location"]; ?>
    <br />
    You have searched for <?php echo $_POST["bedrooms"]; ?>&nbsp;bedrooms
    <br />
    You have searched for <?php echo $_POST["bathrooms"]; ?>&nbsp;bathrooms
    <br />
    You have searched for £<?php echo $_POST["minprice"]; ?>
    <br />
    You have searched for £<?php echo $_POST["maxprice"]; ?>
    <br><br>
    Please find your results below
    </body>
    </html>
    <br><br>
    <?php

    $db
    =mysql_connect("host""user""password");
    mysql_select_db("database"$db);

    $db mysql_select_db("database") or die ("Couldnt select database");

    ?>

    <?php
    // Query database
    $bedrooms=mysql_real_escape_string($_POST['bedrooms'];
    $bathrooms=mysql_real_escape_string($_POST['bathrooms'];
    $minprice=mysql_real_escape_string($_POST['minprice'];
    $maxprice=mysql_real_escape_string($_POST['maxprice'];

    $query="SELECT * FROM properties WHERE bedrooms='$bedrooms' AND bathrooms='$bathrooms' AND price >= '$minprice' AND price <= '$maxprice' ORDER BY price DESC";

    /*$selectsql = "SELECT * FROM properties where typeProperty='".$_POST['typeProperty']."' AND location='".$_POST['location']."' AND bedrooms='".$_POST['bedrooms']."' AND bathrooms='".$_POST['bathrooms']."' AND (price  BETWEEN ".$_POST['minprice']." AND ".$_POST['maxprice'].")";
    $result = mysql_query($selectsql);

    if (!$result)
    {
        echo "Error running query:<br>";
        trigger_error(mysql_error());
    }
    elseif(!mysql_num_rows($result))
    {
        // no records found by query.
        echo "No records found";
    }
    else
    {*/
        
    $i 0;
        echo 
    '<div class="container" style="float:left;">';
      while(
    $row mysql_fetch_array($result)) {     // Loop through results
            
    $i++;
        echo 
    '</div>';        
        echo 
    '<div class="textholder" style="font-family:helvetica; font-size:13px; float:left; padding-top:10px;">';
            echo 
    "<span style=\"color:green;\"><b>Displaying record $i<br>\n</b><br></span>";
            echo 
    "<b>" $row['id'] . "</b><br>\n";      // Where 'id' is the column/field title in the database
            
    echo "Property Type: "$row['typeProperty'] . "<br>\n";       // as above
            
    echo "Bedrooms: "$row['bedrooms'] . "<br>\n";  // ..
            
    echo "Bathrooms: "$row['bathrooms'] . "<br>\n";  // ..
            
    echo "Garden: "$row['garden'] . "<br>\n";  // ..
            
    echo "Description: "$row['description'] . "<br>\n";  // ..
            
    echo "Price: "$row['price'] . "<br>\n";         // ..
            
    echo "Location: "$row['location'] . "<br>\n";            // Where 'location' is the column/field title in the database
        
    echo '</div>';
        echo 
    '<div style="clear:both"></div>';                
        }
    echo 
    '</div>';    }

    ?>
    Sorry to be a pain

  • #10
    New Coder
    Join Date
    Apr 2012
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi mlseim

    See in the code below where you have put

    $minprice=mysql_real_escape_string($_POST['minprice'];
    $maxprice=mysql_real_escape_string($_POST['maxprice'];

    I have not got minprice and maxprice in my database, only price, could that be why I have got server error

    if I added it into the database, could I leave it blank or do I need to add in the vales for example from 100,000 to 3000,000,0

    Kind regards

    Ian

  • #11
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    minprice and maxprice are variables from your form.

    you will compare them to the values in column named 'price' ... so what's there is correct.

    This line is commented-out:
    $result = mysql_query($selectsql);

    Uncomment that line and change it to:
    $result = mysql_query($query);

    You didn't perform the query ... that's why you got an error.

  • #12
    New Coder
    Join Date
    Apr 2012
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi mlseim

    I have still got the error after changing the line of code

    The coding is below again

    Code:
    <html>
    <body>
    You have searched for a&nbsp;<?php echo $_POST["propertytype"]; ?>
    <br>
    You have searched in the location of <?php echo $_POST["location"]; ?>
    <br />
    You have searched for <?php echo $_POST["bedrooms"]; ?>&nbsp;bedrooms
    <br />
    You have searched for <?php echo $_POST["bathrooms"]; ?>&nbsp;bathrooms
    <br />
    You have searched for £<?php echo $_POST["minprice"]; ?>
    <br />
    You have searched for £<?php echo $_POST["maxprice"]; ?>
    <br><br>
    Please find your results below
    </body>
    </html>
    <br><br>
    <?php
    
    $db=mysql_connect("host", "user", "password");
    mysql_select_db("database", $db);
    
    $db = mysql_select_db("database") or die ("Couldnt select database");
    
    ?>
    
    <?php
    // Query database
    $bedrooms=mysql_real_escape_string($_POST['bedrooms'];
    $bathrooms=mysql_real_escape_string($_POST['bathrooms'];
    $minprice=mysql_real_escape_string($_POST['minprice'];
    $maxprice=mysql_real_escape_string($_POST['maxprice'];
    
    $query="SELECT * FROM properties WHERE bedrooms='$bedrooms' AND bathrooms='$bathrooms' AND price >= '$minprice' AND price <= '$maxprice' ORDER BY price DESC";
    
    /*$selectsql = "SELECT * FROM properties where typeProperty='".$_POST['typeProperty']."' AND location='".$_POST['location']."' AND bedrooms='".$_POST['bedrooms']."' AND bathrooms='".$_POST['bathrooms']."' AND (price  BETWEEN ".$_POST['minprice']." AND ".$_POST['maxprice'].")";*/
    
    //$result = mysql_query($selectsql);
    
    $result = mysql_query($query);
    
    /*
    if (!$result)
    {
        echo "Error running query:<br>";
        trigger_error(mysql_error());
    }
    elseif(!mysql_num_rows($result))
    {
        // no records found by query.
        echo "No records found";
    }
    else
    {*/
        $i = 0;
        echo '<div class="container" style="float:left;">';
      while($row = mysql_fetch_array($result)) {     // Loop through results
            $i++;
    	echo '</div>';		
    	echo '<div class="textholder" style="font-family:helvetica; font-size:13px; float:left; padding-top:10px;">';
            echo "<span style=\"color:green;\"><b>Displaying record $i<br>\n</b><br></span>";
            echo "<b>" . $row['id'] . "</b><br>\n";      // Where 'id' is the column/field title in the database
    		echo "Property Type: ". $row['typeProperty'] . "<br>\n";       // as above
    		echo "Bedrooms: ". $row['bedrooms'] . "<br>\n";  // ..
    		echo "Bathrooms: ". $row['bathrooms'] . "<br>\n";  // ..
    		echo "Garden: ". $row['garden'] . "<br>\n";  // ..
    		echo "Description: ". $row['description'] . "<br>\n";  // ..
    		echo "Price: ". $row['price'] . "<br>\n";         // ..
            echo "Location: ". $row['location'] . "<br>\n";            // Where 'location' is the column/field title in the database
    	echo '</div>';
    	echo '<div style="clear:both"></div>';				
        }
    echo '</div>';	}
    
    ?>

  • #13
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Tell me what the error says ... and if it provides a line number of error.

  • #14
    New Coder
    Join Date
    Apr 2012
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am getting the following error

    Server error
    The website encountered an error while retrieving http://www.2up2downhomes.com/saleresults.php. It may be down for maintenance or configured incorrectly.
    Here are some suggestions:
    Reload this web page later.
    HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfil the request.

  • #15
    New Coder
    Join Date
    Apr 2012
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    any ideas what it could be Mlseim


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •