我是新的当谈到的PHP,SQL和仍在学习,我想获得我的列的最后4个字符串值,其中值是电话号码:(7258787) 我我试图显示最后4个字符串,即使搜索查询已满7字符串(8787)基于我已阅读的SUBSTRING(column_name,-4)将导致右边的最后4个字符串。 我的代码返回undefined,你能用这个来启发我吗?结果UNDEFINED在SQL查询和php
if (isset($_GET['telephone'])) {
$data = "%".$_GET['telephone']."%";
$sql = 'SELECT telephone, SUBSTRING(telephone,-4)FROM employee';
使用此:
$sql = 'SELECT * FROM employee WHERE telephone like ?';
将导致的7258787正确的值,但它会导致整个字符串(电话号码),我在搜索框
输入预先感谢您
这是整个代码:
这不是应答b UT斯达康整个脚本,(学分以色列巴拉甘) 在我的数据库中,我有员工为表和列“ID”,“姓名”,“电话”和“电子邮件”
<?php
header('Content-Type: application/json');
require_once 'Connectiondb.php';
$conn = dbConnect();
$OK = true; // We use this to verify the status of the update.
if (isset($_GET['telephone'])) {
// Create the query
$data = "%".$_GET['telephone']."%";
$sql = 'SELECT * FROM employee WHERE telephone like ?';
// we have to tell the PDO that we are going to send values to the query
$stmt = $conn->prepare($sql);
// Now we execute the query passing an array toe execute();
$results = $stmt->execute(array($data));
// Extract the values from $result
$rows = $stmt->fetchAll();
$error = $stmt->errorInfo();
//echo $error[2];
}
// If there are no records.
if(empty($rows)) {
echo json_encode(array('error'=>'There were not records','0'=> 'There were not records'));
}
else {
echo json_encode($rows);
}
?>
对不起我新增至计算器,
你可以张贴一些代码?你如何选择第二个查询?在进入下一页之前,您是否尝试打印结果? – bksi
这是什么数据库?尝试在'SUBSTRING(telephone,-4)FROM'中放置一个列别名和一个空格,使它看起来像'SUBSTRING(telephone,-4)as phoneLastFour FROM' –
Okey抱歉让我把整个代码放在一起,谢谢 – Leiagh