PHP - MYSQL Concat无法正常工作[关闭]

I used the below query in mysql and it works SELECT concat(userid, '-', text) FROM grades1.

When i embed this into php, it doesnt work.

<?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'];
}
?>

Any idea why this is happening?

first alias the field in result set like this:

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

and then use:

$row["user_text"]

At first glance, you're using mysql_fetch_assoc, and pulling the text column.

The query will actually create a column named "concat(userid, '-', text)". The column 'text' is never pulled.

I would recommend using mysql_fetch_array and echo $row[0].