2012-03-26 82 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]。