我试图从PHP中的字符串中删除最后7个字符,然后将其作为结果写入表中。当我在Navicat中创建Mysql查询时,我得到了所需的字符串,但我无法将其移动到PHP中并使其工作。这是工作的Mysql查询(在Navicat中):
SELECT
SUBSTRING(ssp_subjects.uid, 1, LENGTH(ssp_subjects.uid) -7),
ssp_subjects.namefirst,
ssp_subjects.namelast,
ssp_subjects.imageactual,
ssp_subjects.accesscode,
ssp_subjects.correction,
ssp_subjects.correctiondate
FROM
ssp_subjects
WHERE
ssp_subjects.correction = 1
返回:
CCU-SOC-14-05-31-A KYLE O'CONNOR 29 GFKDPFLM3F 1 2017-06-20 09:14:39
(这是正确的,因为它修剪了UID末尾的最后7个字符。)
在PHP中我试图构建一个类似的表,但我似乎无法正确编写SUBSTRING语句。这就是我在PHP中构建表的方法:
$sql = "SELECT
ssp_subjects.correctiondate,
ssp_subjects.namelast,
ssp_subjects.namefirst,
ssp_subjects.imageactual,
ssp_subjects.accesscode,
ssp_subjects.uid
FROM
ssp_subjects
WHERE
ssp_subjects.correction = 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>Date</th><th>Job Name</th><th>Team Number</th><th>Type</th><th>Last Name</th><th>First Name</th><th>Access Code</th><th>Sequence</th><th>Card</th><th>Image</th><th>UID</th><th>Order</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>".$row["correctiondate"]."</td>
<td><p>Corrections</p></td>
<td><p>10</p></td>
<td><p>Player</p></td>
<td>".$row["namelast"]." </td>
<td>".$row["namefirst"]."</td>
<td>".$row["accesscode"]." </td>
<td><p>1000</p></td>
<td>".$row["imageactual"]." </td>
<td> ".$row["uid"]." </td>
<td><p>1x1</p></td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
当我尝试在PHP中添加SUBSTR时,它会返回“Undefined Index:uid”错误,而不是修剪后的字符串。
$sql = "SELECT
ssp_subjects.correctiondate,
ssp_subjects.namelast,
ssp_subjects.namefirst,
ssp_subjects.imageactual,
ssp_subjects.accesscode,
SUBSTRING(ssp_subjects.uid, 1, LENGTH(ssp_subjects.uid) -7)
FROM
ssp_subjects
WHERE
ssp_subjects.correction = 1";
$result = $conn->query($sql);
提前致谢!
答案 0 :(得分:2)
从PHP中的字符串中删除最后7个字符的代码是
$string = substr($string, 0, -7);