2012-03-26 39 views
-4

我在mysql中使用了下面的查詢,它的工作原理 SELECT concat(userid,' - ',text)FROM grades1PHP - MYSQL Concat不工作

當我把它嵌入到php中,它不工作。

<?php 
//connect to the db 
$user = 'sproc'; 
$pswd = 'password'; 
$db = 'mydb1'; 
$conn = mysql_connect('localhost', $user, $pswd); 
mysql_select_db($db, $conn); 
//run the query to search for the username and password the match 
$query = "SELECT concat(userid, '-', text) FROM grades1"; 
$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error()); 
//this is where the actual verification happens 
while ($row = mysql_fetch_assoc($result)) { 
    echo $row['text']; 
} 
?> 

任何想法爲什麼會發生這種情況?

+0

你的意思是「它不起作用」。 – SimonMayer 2012-03-26 23:47:58

回答

1

第一別名結果字段設置如下:

$query = "SELECT concat(userid, '-', text) AS user_text FROM grades1"; 

,然後使用:

$row["user_text"] 
0

乍一看,你使用mysql_fetch_assoc,並拉動文本列。

該查詢實際上會創建一個名爲「concat(userid,' - ',text)」的列。 「文本」欄永遠不會被拖動。

我會推薦使用mysql_fetch_array和echo $ row [0]。