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 3 123 LastLast
Results 1 to 15 of 34

Thread: PHP coding help

  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP coding help

    Hi

    I am in the middle of designing a website and am trying to get a webpage to connect and display results from the database to my webpage when a customer does a search but is not working

    The coding is below

    Any ideas how to get it to work, I have been trying for hours today

    <?php

    $db=mysql_connect("hostl", "user", "pass");
    mysql_select_db("", $db);

    $result = mysql_query("SELECT * FROM properties");

    while($row = mysql_fetch_assoc($result)){
    echo "ID: ".$row['id'].", Type of Property:".$row['typeProperty']
    .", bedrooms:".$row['bedrooms'].", garden:.$row['garden'].", description:.$row['description'].", price:.$row['price'].", location:.$row['location']."<br/>";
    }

    ?>

    <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"]; ?>

    </body>
    </html>

    Thank you in advance, appreciate it

    Ian
    Last edited by ianhaney; 04-15-2012 at 10:20 PM.

  • #2
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    Dude, get rid of your connection details, this is the internet!

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,341
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Quote Originally Posted by dan-dan View Post
    Dude, get rid of your connection details, this is the internet!
    Indeed, I (and everyone else) can now see your mysql login details. Not smart.

    Instead you should have changed it to this:
    $db=mysql_connect("hostl", "user", "pass");

    That way we can see that you're doing your connection right without showing your login. As it stands now anyone with the right skills could wipe out your tables.

    As for why your code isn't working, I'm afraid you're going to need to be more specific. In what way is it not working? Is it showing an error message? Is it just showing a blank white page? Is the mysql query returning the wrong results? - What is wrong?!?!

    As for a search, your query is selecting * (everything) from the table so there is no actual search going on anyway.

    Also you need to read the signature link about quotes. Your misuse of double quotes needs to be tidied up or you're going to run into quotation hell at some point.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #4
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    Is it that you're not connecting to the database/table, or are you just not producing any results?

    If the former, add the highlighted code below to find the problem.

    Code:
    <?php
    
    $db=mysql_connect("$host", "$user", "$pass");
    
    if (!$db)
    	die("Unable to connect to mysql: " . mysql_error());
    
    mysql_select_db("2up2downhomes_c", $db);
              or die("Unable to select database: " . mysql_error());
    
    $result = mysql_query("SELECT * FROM properties");
    
    while($row = mysql_fetch_assoc($result)){
    echo "ID: ".$row['id'].", Type of Property:".$row['typeProperty']
    .", bedrooms:".$row['bedrooms'].", garden:.$row['garden'].", description:.$row['description'].", price:.$row['price'].", location:.$row['location']."<br/>";
    }
    
    ?>
    Last edited by dan-dan; 04-15-2012 at 10:12 PM.

  • #5
    New Coder
    Join Date
    Apr 2012
    Posts
    23
    Thanks
    3
    Thanked 1 Time in 1 Post
    PHP Code:
    $result mysql_query("SELECT * FROM properties"); 

    while(
    $row mysql_fetch_assoc($result)){
        
    $property $row['typeProperty'];
        
    $bedrooms $row['bedrooms'];
    }

    echo 
    "You have searched for $property and $bedrooms"
    POST is mainly used for getting values from html forms

    (BTW, where does the search feature come into this?)

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

    Stupid me in a rush forgot to hide the database info, I have done it now

    on the page that I want to display the results from the database, I am now getting couldnt connect to server

  • #7
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    Doulble check you've got the correct connection details. Copy and paste it over from its source to be sure it's 100% correct.

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

    Yeah I did that from the confirmation email that I received

  • #9
    New Coder
    Join Date
    Apr 2012
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this is what I have now in the coding

    <?php
    $connection = mysql_connect($host, $user, $password) or die ("Couldnt connect to server");

    and I am getting that error message of couldnt connect to server

  • #10
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,341
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Then your host has given you the wrong connection details.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    $host, $user, and $password have been declared as well?
    Use or die(mysql_error());. It should give more information on the cause of the failure (unknown server, bad username / password, etc).

  • #12
    New Coder
    Join Date
    Apr 2012
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    right I have managed to get rid the the error that said couldnt connect to server but now its a blank white page

  • #13
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,341
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Turn on error reporting - you should ALWAYS have this at the top of every script as it's often turned off by default:

    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by ianhaney View Post
    right I have managed to get rid the the error that said couldnt connect to server but now its a blank white page
    Assuming it should have output, this is typical of a fatal error. Enable your error reporting:
    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    And it should tell you the problem.

    Edit:
    bbyyaaahhh I lost this time.

  • #15
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,341
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Quote Originally Posted by Fou-Lu View Post
    Edit:
    bbyyaaahhh I lost this time.
    Hehehehe
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  
    Page 1 of 3 123 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
    •