Jump to content

php, my sql and database help


ianhaney

Recommended Posts

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 ;)

Link to comment
Share on other sites

1 - not sure what this has to do with MS SQL Server? :shrug:

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());   

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.