Jump to content

Query mysql from url parameter


kevin66

Recommended Posts

Hi, I have the following mqsql query which works fine on my page called property.php

 

<?php

 

$result = mysql_query("SELECT * FROM properties WHERE Property_Name='Test Hotel'") or die(mysql_error()); 

 

$row = mysql_fetch_array( $result );

 

echo "Property Name: ".$row['Property_Name'];

echo "<br>Property Rating: ".$row['Property_Rating'];

echo "<br>Property Address: ".$row['Property_Address'];

?>

 

Where I use the value of 'Test Hotel' for Property Name in above, how can I insert this value from a url parameter like /property.php?propertyname=Test Hotel?

 

Thanks so much.

Link to comment
https://forums.phpfreaks.com/topic/233470-query-mysql-from-url-parameter/
Share on other sites

if(!empty($_GET['propertyname'])) {
  $propertyname = mysql_real_escape_string($_GET['propertyname']);
} else {
  //set default value for $propertyname or maybe output an error because propertyname isn't set
}
$result = mysql_query("SELECT * FROM properties WHERE Property_Name='$propertyname'") or die(mysql_error());

Wow, so fast - thank you - works perfectly. As you probably gathered I am very new to this.

 

Can I please ask another question.

 

How can avoid using say /Test%20Hotel/ or /Test%20Hotel%20Name/ in the url as the db entry is Test Hotel or Test Hotel Name. I would like to use Test-Hotel or Test-Hotel-Name in the url.

 

Many thanks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.