gpt4 book ai didi

php - 根据数据库中的表检查 url

转载 作者:行者123 更新时间:2023-11-29 20:50:54 25 4
gpt4 key购买 nike

我目前正在通过 IIS 使用 url 重写,该功能正在运行,并且我正在使用以下变量捕获 url

$compiled_url= $_GET["params"];

但是我需要检查该 url 中的内容与 MySQL 中的页表中的内容。该表如下所示

tbl_pages
page_id | page_slug | page_parent
--------+-----------+------------
1 | posts | 0
2 | daily | 2

http://www.domain.com/posts/daily

有哪些可能的方法可以检查上述域并根据数据库传递参数,并确保它们按该顺序存在,因此如果每天向后输入 url/posts ,则会失败,因为它们没有反射(reflect)这一点数据库中的方式

我已经开始使用这种方法,但作为一个例子,我的最终结果我希望成为一个类或更简洁的选项

$compiled_url = explode("/",$compiled_url); 
$compiled_parent=0;
foreach($compiled_url as $url_decompiled)
{
$url_results = mysqli_query($webapp_db,"SELECT * FROM tbl_pages WHERE page_slug='" . $url_decompiled . "' AND page_parent ='" . $compiled_parent . "'");
if(mysqli_num_rows($url_results) > 0)
{
echo "page found <br>";
$result = mysqli_fetch_array($url_results);
$compiled_parent=$result['page_id'];
}
else
{
echo "page not found <br>";
break;
}

}

谁以前做过类似的事情?不使用框架有哪些方法可用?

最佳答案

你可以使用这样的东西(还没有测试过),但你明白了它的要点......

<?php
$compiled_url = explode("/",$compiled_url);

// using alphabet characters for table aliases
$alphabet = explode("","abcdefghiklmnopqrstuvwxyz");

$sql_query = "SELECT * FROM tbl_pages ".$alphabet[0];

// loop to build all the JOINs
for($i=0; $i<count($compiled_url); $i++)
{
$sql_query .= " INNER JOIN tbl_pages ".$alphabet[$i+1]." ON ".$alphabet[$i+1].".page_id = ".$alphabet[$i].".page_parent";
}


// loop to build all the WHERE filters
$where = array();
for($i=0; $i<count($compiled_url); $i++)
{
$where[] = $alphabet[$i].".page_slug = '".$compiled_url[$i]."'";
}

$sql_query .= " WHERE ".implode(" AND ",$where);

// if results are found print "page found" else print "page not found"
if(mysqli_num_rows($url_results) > 0)
{
echo "page found <br>";
$result = mysqli_fetch_array($url_results);
$compiled_parent=$result['page_id'];
}
else
{
echo "page not found <br>";
break;
}

关于php - 根据数据库中的表检查 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38070638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com