I have added a link name to database called $linkname. For example “large-red-widget”. However my link is querying the database using a unique product ID but I want to display the $linkname within link.
What I dont understand is the relationship between the htaccess file and the way the link is displayed. How I do I display link with linkname but query the databsae using the product_id ?
Firstly you’d have to change the code in product.php to query your db on the linkname column, rather than the product_id. You’d also need to change the code that generates your links, so that they include the name rather than the id, similar to this:
/products/product/<?php echo $row['linkname']; ?>
Are you using some kind of ecommerce software or anything like that?
The thing is I want to run the query using a unique code which I am currently doing. How do I query the database using one variable but display a different variable in the link.
This is a classic example of what I am trying to do.
Right, yes you would… sorry, I’d assumed that your link name was unique. In the case that it’s not, what you have to do is include the product ID in the url, something like johnlewis.com/p230674902-john-lewis-lasko-chest-of-drawers-oak to use your example. I expect this brings you back to square one, but unfortunately if the link name is not unique then there is no way around it.
Your “link name” is commonly referred to as a slug. You need a unique identifier somewhere in your URL to retrieve the appropriate page. If you have redundant slugs, you will have to have a product ID or some other unique identifier in your URL.
123456 is the product ID in this case which would be used to query the table. The large-red-widget is a dummy slug in this case for the benefit of people or search engines reading the URL and is not used in the code. So you could do something like this:
Its very strange because when I try to run the query with the linkname and change the GET on the following page the following page does not display anything.
/products/product/<?php echo $row['linkname']; ?>
<?php
if (isset($_GET['linkname']))
$linkname = mysql_real_escape_string($_GET['linkname']);
$sql = "SELECT * FROM productdbase WHERE linkname = '$linkname'";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($res); // no need to loop since you are retrieving only one row
$num_rows = mysql_num_rows($res); // check to see if any results were found, just in case someone puts an ID in the url without clicking on your link
?>