我是新的當談到的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