【问题标题】:get value from rewritten url in php for a mysql query从 php 中重写的 url 获取 mysql 查询的值
【发布时间】:2012-05-24 02:05:36
【问题描述】:

我有一个重写的网址,显示为:

http://www.coverforce.com.au/insurance-news/news/start-your-own-business-insurance/800479715/

最后的数字是我需要在 mySQL 查询中使用以允许用户阅读本文的 URL 变量 - 有没有办法使用 $_GET 或爆炸来获得这个数字,所以我可以用它来在我的 mySQL 数据库中调出正确的文章?

这是我尝试过的,但它不起作用:

<?php require_once('../Connections/newsDBconnection.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$totalRows_variablearticles = "-1";
if (isset($_GET['id'])) {
  $totalRows_variablearticles = $_GET['id'];
}
mysql_select_db($database_newsDBconnection, $newsDBconnection);
$query_variablearticles = sprintf("SELECT * FROM NewsArticles, NewsArticleCategories, NewsArticlePhotos, NewsPhotos, NewsCategories WHERE NewsArticles.id = %s AND NewsArticles.id = NewsArticleCategories.newsArticleID AND NewsArticles.id = NewsArticlePhotos.newsArticleID AND NewsArticlePhotos.newsPhotoID = NewsPhotos.id AND NewsArticleCategories.newsCategoryID = NewsCategories.id", GetSQLValueString($totalRows_variablearticles, "int"));
$variablearticles = mysql_query($query_variablearticles, $newsDBconnection) or die(mysql_error());
$row_variablearticles = mysql_fetch_assoc($variablearticles);
$totalRows_variablearticles = mysql_num_rows($variablearticles);
?> 

我的网站目前正在运行,但文章无法正常工作,因此您能提供的任何帮助都非常棒!!!

【问题讨论】:

  • 您是否使用 htaccess/mod_rewrite 来解析您的 URL 并重写它们以使 ID 在查询字符串中?
  • 是的,我正在使用 mod rewrite 重写我的 url,这是我在 htaccess 文件中的内容:Options +FollowSymlinks RewriteEngine On RewriteRule ^/?test\.html$ test.php [L] RewriteRule ^/?index\.html$ index.php [L] RewriteRule news/(.*)/(.*)/$ insurance-news/news.php?$1=$2 rewritecond %{http_host} ^coverforce.com.au [nc] rewriterule ^(.*)$ coverforce.com.au/$1 [r=301,nc]

标签: php mysql url variables


【解决方案1】:
<?php

$Path = 'http://DomainName/news/articles/8008280';

//To get the innermost value  your id '8008280'

$Innermost = basename(rtrim($Path, '/'));

echo $Innermost; //will display '8008280'

// you can use article no for your query

// but make sure that article no is within the range of article no's in DB 

?>

【讨论】:

    猜你喜欢
    • 2018-08-04
    • 1970-01-01
    • 2014-10-05
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    相关资源
    最近更新 更多