Home > front end >  How to get some part from url in php
How to get some part from url in php

Time:08-10

Here is my url

&tend_num=B&R/HO/51071/STP/NIT/ROURKELA/01

I would like to get tend_num. When printing the tend_num, it only prints B.

Here is my code

$tend_num = mysqli_real_escape_string($con, $_GET['tend_num']);

Here is query

$result = mysqli_query($con,"SELECT x.*, y.`fullname`, x.`id` as t_id FROM `tend_testing` x, `emp` y  WHERE x.`testfeng`=y.`id` AND x.`testfeng`='$_SESSION[ERP_SESS_ID]' AND x.tend_num ='$tend_num' AND x.`status`='1' ORDER BY x.`created_on` DESC limit $start,$limit");

As I can't get tend_num from url properly so it's difficult to get data from table.

CodePudding user response:

In order to know how to resolve this problem, you need to understand how GET parameters work in a url.

In the current request, you have actually 2 arguments:

tend_num=B
R/HO/51071/STP/NIT/ROURKELA/01 not defined

This is because the & character is used to divide the different arguments in the url.

The way to do this is to encode the & character using & which is the encoded version of &.

CodePudding user response:

If you can encode your url following code will work

$parts = parse_url($url);
parse_str($parts['query'], $query);
echo $query['tend_num'];

if not possible then you may try this

  $url = 'http://localhost/xerp/basic/tender/tsupply.php?id=1&tend_num=B&R/HO/51071/STP/NIT/ROURKELA/01';
        preg_match('/tend_num=(.*)/', $url, $matches);
        print_r($matches);
  •  Tags:  
  • php
  • Related