...

View Full Version : PHP coding help



ianhaney
04-15-2012, 09:03 PM
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

dan-dan
04-15-2012, 09:58 PM
Dude, get rid of your connection details, this is the internet!

tangoforce
04-15-2012, 10:05 PM
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.

dan-dan
04-15-2012, 10:05 PM
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.



<?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/>";
}

?>

chunky
04-15-2012, 10:08 PM
$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?)

ianhaney
04-15-2012, 10:22 PM
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

dan-dan
04-15-2012, 10:27 PM
Doulble check you've got the correct connection details. Copy and paste it over from its source to be sure it's 100% correct.

ianhaney
04-15-2012, 10:36 PM
Hi

Yeah I did that from the confirmation email that I received

ianhaney
04-15-2012, 10:37 PM
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

tangoforce
04-15-2012, 10:49 PM
Then your host has given you the wrong connection details.

Fou-Lu
04-15-2012, 10:51 PM
$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).

ianhaney
04-15-2012, 11:06 PM
right I have managed to get rid the the error that said couldnt connect to server but now its a blank white page

tangoforce
04-15-2012, 11:09 PM
Turn on error reporting - you should ALWAYS have this at the top of every script as it's often turned off by default:



ini_set('display_errors', 1);
error_reporting(E_ALL);

Fou-Lu
04-15-2012, 11:12 PM
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:


ini_set('display_errors', 1);
error_reporting(E_ALL);


And it should tell you the problem.


bbyyaaahhh I lost this time.

tangoforce
04-15-2012, 11:14 PM
bbyyaaahhh I lost this time.


Hehehehe :cool: :thumbsup:

ianhaney
04-15-2012, 11:18 PM
I put that error code and still had a blank white page, does it take time to update and show the error or errors

ianhaney
04-15-2012, 11:22 PM
I am still getting a blank page after putting in that error code

tangoforce
04-15-2012, 11:24 PM
Please post your current code. If error reporting isn't showing anything then there maybe something in the logs (but on shared hosts you can't always get access to the correct log).

Also, please put your current code inside [php] tags. This makes it so much easier for us to help you.

ianhaney
04-15-2012, 11:27 PM
<?php
//$connection = mysql_connect($host, $user, $password) or die ("Couldnt connect to server");

$db=mysql_connect("$hostl", "$user", "$paddword"); mysql_select_db("2up2downhomes_c", $db);

//mysql_connect($hostl,$user,$password);
/* Program: getdata.php
*Desc: Gets data from a database using a function
?>
<html>
<head>
<title>Properties</title>
</head>
<body>
<?php
$user="$user";
$host="$host";
$password="$password";
$connection = mysql_connect($host,$user,$password) or die ("Couldnt connect to server");
$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");
$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&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>

tangoforce
04-15-2012, 11:38 PM
Of course you will have a blank page. All you are doing is connecting to the database and nothing else. You have commented out everything else - thats why it is all orange. The /* applies all the way through your code up to the closing */

Also if this is your latest code, where is the error reporting code? - Please, apart from your mysql login, don't strip out bits of code that YOU THINK we don't need to see.



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

$db=mysql_connect("$hostl", "$user", "$paddword"); mysql_select_db("2up2downhomes_c", $db);

//mysql_connect($hostl,$user,$password);
/* Program: getdata.php
*Desc: Gets data from a database using a function
?>
<html>
<head>
<title>Properties</title>
</head>
<body>
<?php
$user="$user";
$host="$host";
$password="$password";
$connection = mysql_connect($host,$user,$password) or die ("Couldnt connect to server");
$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");
$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&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>

ianhaney
04-15-2012, 11:45 PM
ok no probs, I have put the error reporting code back in and uncommented everything and have pasted it below again


<?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"); mysql_select_db("$database", $db);

Program: getdata.php
*Desc: Gets data from a database using a function
?>
<html>
<head>
<title>Properties</title>
</head>
<body>
$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");
$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&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>

tangoforce
04-15-2012, 11:52 PM
You're still getting things wrong..





//Comments need to be commented out or they will stall the script
Program: getdata.php
*Desc: Gets data from a database using a function
?>

<!--This looks to be php but with no opening php tag-->

$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";

//Ending tag with no opener for the above code
?>


You need to be using a good editor that has colour highlighting such as notepad++ (free via google). See the way the colour in the [php] text box changes? - Thats the first way of seeing problems.

I also think you're panic-rushing when posting and probably making a bit of a mess of it. Calm down, take your time, check things and make sure you've got it clear so that we have the best opportunity to help you. Otherwise we'll go round in circles before we eventually crack it.

ianhaney
04-15-2012, 11:57 PM
ok sorry, I will download notepad++ now and use that, one sec

ianhaney
04-16-2012, 12:01 AM
I have downloaded notepad++ and pasted the code in there and will now paste it 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"); mysql_select_db("$database", $db);

Program: getdata.php
*Desc: Gets data from a database using a function
?>
<html>
<head>
<title>Properties</title>
</head>
<body>
$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");
$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&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>

Sorry if it dont show properly

tangoforce
04-16-2012, 12:04 AM
Yet you're still not listening to me are you?

STOP PANIC POSTING.

Go back to my last post, look at your php that I commented. Act accordingly.

ianhaney
04-16-2012, 12:07 AM
is this it 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"); mysql_select_db("$database", $db);

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");
$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&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>

tangoforce
04-16-2012, 12:09 AM
Again, you're not reading what I say properly and you're rushing to make your next post instead of checking.



//Comments need to be commented with // otherwise they will cause problems.
Program: getdata.php
*Desc: Gets data from a database using a function


The other bit yes, you at least put in the opening tag so thats a good start :thumbsup:

ianhaney
04-16-2012, 12:12 AM
I can only see two lines that need to be commented out with \\, hope I got the right lines near the top


<?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"); mysql_select_db("$database", $db);

//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");
$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&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>

tangoforce
04-16-2012, 12:52 AM
Yes, finally we're there (and even then you say it's \\ and it's actually // (though at least you've put the correct one in the code!).

Right, what happens when you try to run it now?

Be quick, I'm off to bed shortly!

dan-dan
04-16-2012, 01:04 AM
STOP PANIC POSTING.




Be quick, I'm off to bed shortly!


LOL :p

tangoforce
04-16-2012, 01:07 AM
Heh, the irony eh?

ianhaney
04-16-2012, 07:51 AM
Hi tangoforce

Sorry didnt get back to you last night, I fell asleep

When I run it now, I get the following

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.

ianhaney
04-16-2012, 04:18 PM
I have got a connection now to the database, how do I get the data from the database to display on the webpage

chunky
04-16-2012, 07:15 PM
$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?)

This will display results on the screen, just do the same for each column you have



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum