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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Well.. if you're sure "-" will never be in the name of a hotel, you can replace all spaces with - when you output the link, then do the opposite in your property.php code:

$propertyname = str_replace('-', ' ', mysql_real_escape_string($_GET['propertyname']));

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.