kevin66 Posted April 12, 2011 Share Posted April 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233470-query-mysql-from-url-parameter/ Share on other sites More sharing options...
dcro2 Posted April 12, 2011 Share Posted April 12, 2011 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()); Quote Link to comment https://forums.phpfreaks.com/topic/233470-query-mysql-from-url-parameter/#findComment-1200484 Share on other sites More sharing options...
kevin66 Posted April 12, 2011 Author Share Posted April 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233470-query-mysql-from-url-parameter/#findComment-1200517 Share on other sites More sharing options...
dcro2 Posted April 12, 2011 Share Posted April 12, 2011 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'])); Quote Link to comment https://forums.phpfreaks.com/topic/233470-query-mysql-from-url-parameter/#findComment-1200525 Share on other sites More sharing options...
kevin66 Posted April 12, 2011 Author Share Posted April 12, 2011 Thanks. You are the man. Quote Link to comment https://forums.phpfreaks.com/topic/233470-query-mysql-from-url-parameter/#findComment-1200538 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.