SELECT * FROM videos WHERE url = substring($ url,0,5)

时间:2015-04-06 13:31:38

标签: php mysql get

通常,视频网址与site.com/watch?v=abcde.flv相似 我希望新网址为site.com/watch?v=abcde

通常是:     $url = $_GET['v']; 然后

$query = mysql_query("SELECT * FROM videos WHERE url = '$url'");

我怎么才能只使用前5个字符?

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

$newurl = substr($url, 0, 11);

然后

$query("SELECT FROM videos WHERE substring(url, 0,11) = '$newurl'");

1 个答案:

答案 0 :(得分:0)

SQL中用于字符串解析的正确语法是:

LEFT ( character_expression , integer_expression )

有关详情,请参阅: https://msdn.microsoft.com/en-ca/library/ms177601.aspx

在你的情况下:

//assuming your new url of watch?v=abcde
$url = $_GET['v']; 

//assuming the video in your databse is stored as abcde.flv
$query = mysql_query("SELECT FROM videos WHERE LEFT(url,5) = '$url'");