MySQL Question

get post id from long url (php)

hello i have post url like that

http://localhost/pa/news.php?post=2/test-title-test

news.php? = news template

2 in (post =2 ) = post id in mysql
and test-title-test is article title

i want to get only post id to use it on mysql order and load content order by id

http://localhost/pa/news.php?post=2/test-title-test

when i trying to use $id = $_GET['post'];
it echos 2/test-title-test
i need to echo only id 2

are there any other ways to get post id from url tu use it for mysql order?
thanks

Answer

You need to split the post-param.

Pre PHP 5.5:

$postParts = explode('/', $_GET['post']);
$postId    = $postParts[0];

PHP 5.5+:

$postId = explode('/', $_GET['post'])[0];
Comments