mastubbs Posted February 23, 2009 Share Posted February 23, 2009 Hi there, This is my first time using mySQL and i've managed to get a bit stuck. What i want to do is redirect to a different page depending on the value of the attribute in the URL of a link. What i mean is: If the url i type into the browser is http://www.mysite.com/product_info.php?products_id=1234 divert to http://www.mysite.com/some_page.php if the url is http://www.mysite.com/product_info.php?products_id=2345 divert to http://www.mysite.com/some_other_page.php I will be forwarding to a large number of pages (2000 or so) so i thought best to use a mySQL database rather than lots of if statements. I can't seem to get it to work though, could anyone tell me what im doing wrong? FIRST I make the table: <?php // Make a MySQL Connection mysql_connect("localhost", "jasperss_redirect", "password") or die(mysql_error()); mysql_select_db("jasperss_redirect") or die(mysql_error()); // Create a MySQL table in the selected database mysql_query("CREATE TABLE redirect_table( id INT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), page_list VARCHAR(50), file_name VARCHAR(150))") or die(mysql_error()); echo "Table Created!"; ?> then, <?php // Make a MySQL Connection mysql_connect("localhost", "jasperss_redirect", "password") or die(mysql_error()); mysql_select_db("jasperss_redirect") or die(mysql_error()); // Insert a row of information into the table "redirect_table" mysql_query("INSERT INTO redirect_table (file_name, page_list) VALUES('some_page.php', '1234' ) ") or die(mysql_error()); mysql_query("INSERT INTO redirect_table (file_name, page_list) VALUES('some_other_page.php', '2345' ) ") or die(mysql_error()); echo "Data Inserted!"; ?> and on the redirect page itself (product_info.php) <?php $pageid = (isset($_GET['products_id'])) ? (int) $_GET['products_id'] : null; if(!empty($pageid)) { if(mysql_connect('localhost', 'jasperss_redirect', 'password')) { if(mysql_select_db('jasperss_redirect')) { $result = mysql_query("SELECT file_name FROM redirect_table WHERE page_list = $pageid"); if($result != false and mysql_num_rows($result)) { $page = mysql_result($result, 0); header('Location: $page'); exit; } } } $error = "Database error."; } else { $error = "No page specified."; } ?> <html><head><title>Error</title></head><body> <p class="error"><?php echo $error; ?></p> </body></html> When i type "http://www.mysite.com/product_info.php?products_id=1234" into the browser I get the error: "The requested URL /$page was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request." It is redirecting me to $page by the looks of it, rather than to the actual value of $page from the database. Can anyone help? Thanks in advance, Matt Link to comment https://forums.phpfreaks.com/topic/146552-divert-page-by-phpidattribute/ Share on other sites More sharing options...
mastubbs Posted March 2, 2009 Author Share Posted March 2, 2009 Really? Know one knows? Link to comment https://forums.phpfreaks.com/topic/146552-divert-page-by-phpidattribute/#findComment-774959 Share on other sites More sharing options...
jack_ Posted March 2, 2009 Share Posted March 2, 2009 If you are doing massive grouping I would suggest looking into mod_rewrite. It is far far easier than php'ing everything through statements. But it can be somewhat intensive. So to clearify, if you wanted to group say, id's 1-500 to somepage1.html, then 501-1000 to somepage2.html then 1001-1500 to somepage3.html etc, it would probably be your best bet. Your pages will be read by whatever attribs you have, apache just redirects them to the pages you specify. In the above example you could do it all in 3 lines in your htaccess versus whatever you were looking for via php. Link to comment https://forums.phpfreaks.com/topic/146552-divert-page-by-phpidattribute/#findComment-774974 Share on other sites More sharing options...
br0ken Posted March 2, 2009 Share Posted March 2, 2009 You have this code: header('Location: $page'); But you actually need: header("Location: $page"); When using single speech marks, variables are not parsed and are treated as a string. Link to comment https://forums.phpfreaks.com/topic/146552-divert-page-by-phpidattribute/#findComment-774984 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.