Closed. This question is off-topic。它当前不接受答案。
想改善这个问题吗? Update the question,因此它是on-topic,用于堆栈溢出。
已关闭8年。
Improve this question
我做了这个功能,因为我在整个站点上都使用了它,所以我想知道它是否是一种优化它,使其运行更快的方法?我也想知道我对功能的看法是否正确?谢谢。
// function to get the seo friendly url of a video based on it's id
function video_seo_urls($video_id)
{
global $site_url;
$sql = dbquery('SELECT `post_name`, `new_post_name`, `category` FROM `videos` WHERE `id` ="'.$video_id.'" LIMIT 0,1;');
while($row = mysql_fetch_array($sql))
{
$post_name = $row["post_name"];
$new_post_name = $row["new_post_name"];
$category_id = $row["category"];
}
$csql = dbquery('SELECT `category_slug` FROM `categories` WHERE `category_id` = "'.$category_id.'" LIMIT 0,1;');
while($row = mysql_fetch_array($csql))
{
$category = $row["category_slug"];
}
if ($new_post_name !== "")
{
$video_slug = $new_post_name;
}
else if ($new_post_name == "" && $post_name !== "")
{
$video_slug = $post_name;
}
$video_url = $site_url.'video/'.$category.'/'.$video_slug.'/';
return $video_url;
}
echo video_seo_urls(14775);
参考方案
可以通过使用JOIN将这两个查询合并为一个。语法如下所示:
$sql = dbquery('SELECT v.`post_name`, v.`new_post_name`, v.`category` FROM `videos` AS v JOIN 'category_slug' AS cs ON cs.category=v.category WHERE v.`id` ="'. mysql_real_escape_string($video_id));
与速度问题无关,由于将字符串数据连接到SQL查询中,因此还存在巨大的安全漏洞。如果有人能够使用video_id为“; DROP TABLE videos”来调用此函数,则您将丢失整个表。更糟糕的事情可能发生,这被称为SQL Injection,您需要阅读有关它的信息,因为这是网站被黑客入侵的最常见方式。我使用了一个可以保护您免受黑客攻击的功能,但这仍然不是解决此问题的最佳方法,它仅被视为创可贴。
PHP:将数组值加在一起 - php我相信这比标题听起来要难一些,但我可能完全错了。我有一个像这样的数组:[["londrina",15],["cascavel",34],["londrina",23],['tiradentes',34],['tiradentes',21]] 我希望能够采用通用…
PHP JQuery复选框 - php我有以下片段。 var myData = { video: $("input[name='video[]']:checked").serialize(), sinopse: $("#sinopse").val(), dia: $("#dia").val(), quem: $(…
PHP strtotime困境 - php有人可以解释为什么这在我的服务器上输出为true吗?date_default_timezone_set('Europe/Bucharest'); var_dump( strtotime('29.03.2015 03:00', time()) === strtotime('29.03.2015 04:00…
PHP-全局变量的性能和内存问题 - php假设情况:我在php中运行一个复杂的站点,并且我使用了很多全局变量。我可以将变量存储在现有的全局范围内,例如$_REQUEST['userInfo'],$_REQUEST['foo']和$_REQUEST['bar']等,然后将许多不同的内容放入请求范围内(这将是适当的用法,因为这些数据指的是要求自…
php-casperjs获取内部文本 - php我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…