检索查询的最后两个字符串

retrieve last two string of a query

我希望能够检索查询的最后两个字符串。从某种意义上说,我有一个名为 course_tax 的项目,它被检索为 1.05 或 1.10 或 1.11 等。我只想检索 05,10 或 11.

这基本上就是我现在检索它的方式:

$course_id = $_GET['crs_id'];
$get_crs = "select * from courses where course_id='$course_id'";
$run_crs = mysqli_query($con, $get_crs);

while($row_crs=mysqli_fetch_array($run_crs)){
    $course_tax = $row_crs['course_tax'];
}

您只需要获取数字的小数部分。尝试像这样使用 explode

while($row_crs=mysqli_fetch_array($run_crs)){
    $dig = explode(".", $row_crs['course_tax']);
    $course_tax = $dig[1];
}

这个问题的解决取决于两件事:

  1. 如果你期望从数据库中得到的值总是在这个 格式:x.xx 其中 x 是一个整数。

  2. 如果您不确定结果的精确度 从数据库中出来。

如果是这种情况,这个简单的 sql 查询应该可以解决它:

"SELECT *, right(course_tax, 2) as 'course_tax' FROM courses where course_id = '$course_id'";

否则,如果是两个,则必须使用 php 中提供的功能之一。 最推荐使用的是 explode($delimiter, $haystack) 函数。所以这绝对有效:

$exploded = explode(".", $row_crs['course_tax']); $course_tax = $exploded[1];