ianhaney Posted April 15, 2012 Share Posted April 15, 2012 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("2up2downhomes.com.mysql", "userxxx", "passxxx"); mysql_select_db("2up2downhomes_c", $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 <?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"]; ?> bedrooms <br /> You have searched for <?php echo $_POST["bathrooms"]; ?> 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 EDIT (KP): Removed credz & please use code tags next time Quote Link to comment https://forums.phpfreaks.com/topic/260997-php-my-sql-and-database-help/ Share on other sites More sharing options...
ianhaney Posted April 15, 2012 Author Share Posted April 15, 2012 Hi Thank you so much for removing the data and I will do, I am so sorry, bit new to this forum Quote Link to comment https://forums.phpfreaks.com/topic/260997-php-my-sql-and-database-help/#findComment-1337658 Share on other sites More sharing options...
Muddy_Funster Posted April 16, 2012 Share Posted April 16, 2012 1 - not sure what this has to do with MS SQL Server? 2 - more information than "it's not working" is always appreciated 3 - the following three lines should all have an or die statement at the end to display any errors coming back from the database: $db=mysql_connect("2up2downhomes.com.mysql", "userxxx", "passxxx") or die (mysql_error()); mysql_select_db("2up2downhomes_c", $db) or die (mysql_error()); $result = mysql_query("SELECT * FROM properties") or die (mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/260997-php-my-sql-and-database-help/#findComment-1337750 Share on other sites More sharing options...
ianhaney Posted April 16, 2012 Author Share Posted April 16, 2012 Hi Muddy Funster Thank you for the reply I put in the coding you said and am still 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. The code I am using is below <?php ini_set('display_errors', 1); error_reporting(E_ALL); //$connection = mysql_connect($host, $user, $password) or die ("Couldnt connect to server"); $db=mysql_connect("$host", "$user", "$password") or die (mysql_error()); mysql_select_db("$database", $db) or die (mysql_error()); //Program: getdata.php //Desc: Gets data from a database using a function ?> <html> <head> <title>Properties</title> </head> <body> <?php $typeProperty = gettypeProperty("House"); //call function $f_price = number_format ($typeProperty['price',2); echo "<p><b>{$typeProperty['typeProperty']}</b><br>\n; Description: {$typeProperty['Description']}<br>\n; Price: \${$typeProperty['price']}\n"; ?> </body> </html> <?php function gettypeProperty($typeProperty) { $db = mysql_select_db("properties") or die ("Couldnt select database"); $result = mysql_query("SELECT * FROM properties") or die (mysql_error()); //$query = "SELECT * FROM properties WHERE typeProperty='typeProperty'"; $result = mysql_query($query) or die ("Couldnt execute query."); return mysql_fetch_array($result,MYSQL_ASSOC); } ?> <html> <body> You have searched for a <?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"]; ?> bedrooms <br /> You have searched for <?php echo $_POST["bathrooms"]; ?> bathrooms <br /> You have searched for £<?php echo $_POST["minprice"]; ?> <br /> You have searched for £<?php echo $_POST["maxprice"]; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/260997-php-my-sql-and-database-help/#findComment-1337761 Share on other sites More sharing options...
Muddy_Funster Posted April 16, 2012 Share Posted April 16, 2012 do you have custom error messages set up? Quote Link to comment https://forums.phpfreaks.com/topic/260997-php-my-sql-and-database-help/#findComment-1337795 Share on other sites More sharing options...
ianhaney Posted April 16, 2012 Author Share Posted April 16, 2012 Hi I have this in my coding and have got the php error messages turned on in my control panel with my host but that's it ini_set('display_errors', 1); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/260997-php-my-sql-and-database-help/#findComment-1337797 Share on other sites More sharing options...
PFMaBiSmAd Posted April 16, 2012 Share Posted April 16, 2012 Your code is actually using mysql, so the MS SQL form section where you posted it is not where this belongs at all. In any case, your current problem is the php code, so moving thread to the php help section... Quote Link to comment https://forums.phpfreaks.com/topic/260997-php-my-sql-and-database-help/#findComment-1337807 Share on other sites More sharing options...
PFMaBiSmAd Posted April 16, 2012 Share Posted April 16, 2012 You have a fatal parse (syntax) error in - Parse error: syntax error, unexpected ',', expecting ']' in your_file.php on line 19 For fatal parse errors to be reported in your main file, the error_reporting/display_errors setting must be set before your script is requested (your code never runs, so putting the settings into your script won't ever turn the settings on.) You should have the error_reporting/display_errors setting set in your master php.ini on your development system. You should also not be attempting to develop and debug php code on a live server. Set up a localhost development system, you will save a TON of time. Quote Link to comment https://forums.phpfreaks.com/topic/260997-php-my-sql-and-database-help/#findComment-1337810 Share on other sites More sharing options...
Muddy_Funster Posted April 16, 2012 Share Posted April 16, 2012 this looks to be the offending line from PFM's info: $f_price = number_format ($typeProperty['price',2); Quote Link to comment https://forums.phpfreaks.com/topic/260997-php-my-sql-and-database-help/#findComment-1337813 Share on other sites More sharing options...
ianhaney Posted April 16, 2012 Author Share Posted April 16, 2012 I took that line out Muddy but am getting this now php_network_getaddresses: getaddrinfo failed: Name or service not known This is doing my head in now I have included my coding in below <?php //$connection = mysql_connect($host, $user, $password) or die ("Couldnt connect to server"); $db=mysql_connect("$host", "$user", "$password"); mysql_select_db("$database", $db); $db=mysql_connect("$host", "$user", "$password"); mysql_select_db("$database", $db) or die (mysql_error()); mysql_select_db("$database", $db) or die (mysql_error()) or die (mysql_error()); //Program: getdata.php //Desc: Gets data from a database using a function ?> <html> <head> <title>Properties</title> </head> <body> <?php $typeProperty = gettypeProperty("House"); //call function echo "<p><b>{$typeProperty['typeProperty']}</b><br>\n; Description: {$typeProperty['Description']}<br>\n; Price: \${$typeProperty['price']}\n"; ?> </body> </html> <?php function gettypeProperty($typeProperty) { $db = mysql_select_db("properties") or die ("Couldnt select database"); $query = "SELECT * FROM properties WHERE typeProperty='typeProperty'"; $result = mysql_query($query) or die ("Couldnt execute query."); return mysql_fetch_array($result,MYSQL_ASSOC); } ?> <html> <body> You have searched for a <?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"]; ?> bedrooms <br /> You have searched for <?php echo $_POST["bathrooms"]; ?> bathrooms <br /> You have searched for £<?php echo $_POST["minprice"]; ?> <br /> You have searched for £<?php echo $_POST["maxprice"]; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/260997-php-my-sql-and-database-help/#findComment-1337815 Share on other sites More sharing options...
Muddy_Funster Posted April 16, 2012 Share Posted April 16, 2012 I dont see any code therre that would bear any relevance to that error message Quote Link to comment https://forums.phpfreaks.com/topic/260997-php-my-sql-and-database-help/#findComment-1337824 Share on other sites More sharing options...
ianhaney Posted April 16, 2012 Author Share Posted April 16, 2012 I am thinking of starting from scratch as is going wrong somewhere Quote Link to comment https://forums.phpfreaks.com/topic/260997-php-my-sql-and-database-help/#findComment-1337827 Share on other sites More sharing options...
ianhaney Posted April 16, 2012 Author Share Posted April 16, 2012 Ok I am winning sort of, I have got connection the mysql database, how do I get the data from the database to display on the webpage Quote Link to comment https://forums.phpfreaks.com/topic/260997-php-my-sql-and-database-help/#findComment-1337844 Share on other sites More sharing options...
ianhaney Posted April 16, 2012 Author Share Posted April 16, 2012 Hi I have managed to connect to mysql database and managed to display results but now how I want exactly 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 <html> <body> You have searched for a <?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"]; ?> bedrooms <br /> You have searched for <?php echo $_POST["bathrooms"]; ?> 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 Quote Link to comment https://forums.phpfreaks.com/topic/260997-php-my-sql-and-database-help/#findComment-1337870 Share on other sites More sharing options...
ianhaney Posted April 17, 2012 Author Share Posted April 17, 2012 Any ideas any one please I am well and truly stuck Kind regards Ian Quote Link to comment https://forums.phpfreaks.com/topic/260997-php-my-sql-and-database-help/#findComment-1338058 Share on other sites More sharing options...
Muddy_Funster Posted April 17, 2012 Share Posted April 17, 2012 you need to have a WHERE condition in your query : $result = mysql_query("SELECT * FROM properties WHERE field1='$type' AND field2='$area' AND......"); Quote Link to comment https://forums.phpfreaks.com/topic/260997-php-my-sql-and-database-help/#findComment-1338059 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.