Page 1 of 1

MSSQL question

Posted: Tue Mar 30, 2004 2:35 am
by ihateevilbill
I am having trouble linking to a database and was wondering if any1 in here knew what i was doing wrong.

Below is my code:

Code: Select all

<?php
$myServer = "blah";
$myUser = "blah";
$myPass = "blah";
$myDB = "blah";

$s = @mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");

$d = @mssql_select_db($myDB, $s)
or die("Couldn't open database $myDB");

$query = "SELECT title,firstname,surname,emailaddress AS Employee FROM enquiries WHERE firstname='Steven'";

$result = mssql_query($query);
$numRows = mssql_num_rows($result);

echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";

while($row = mssql_fetch_array($result))
{
echo "<li>" . $row["Employee"] . "</li>";
}

mssql_free_result($rs);
mssql_close ($link);

?>
Thing is, when I run this code it brings back only the emailaddress in the list. Now this select looks fine to me, but im used to using query analyser etc.

Now, I have been given this code to work with

Code: Select all

<?php
$query = "SELECT title+' '+firstname+' '+surname AS Employee ";
$query .= "FROM enquiries ";
$query .= "WHERE firstname='Steven'";

?>
and it seems to work, but i dont understand why all the + and ' are there...any one shed any light on this as well as the rules for creating a query like the 2nd one.

Regards,

Steven.

Posted: Tue Mar 30, 2004 3:50 am
by twigletmac
In the first query:

Code: Select all

$query = "SELECT title,firstname,surname,emailaddress AS Employee FROM enquiries WHERE firstname='Steven'";
you are selecting 4 separate columns so when you echo $row['Employee'] all you get is the email address (as this is the field you assigned to the alias Employee).

However, in the second query:

Code: Select all

$query = "SELECT title+' '+firstname+' '+surname AS Employee ";
$query .= "FROM enquiries ";
$query .= "WHERE firstname='Steven'";
You are concenating (i.e. joining together) the values of the title, firstname and surname columns as one with an alias of Employee.

Mac

Posted: Tue Mar 30, 2004 4:14 am
by ihateevilbill
ahhhhhhhhhhhhh right, i understand now, just tried it in query analyser (shoulda tried that first i suppose) and it put the emailaddress into Employee, i just automatically thought that it concatenated the 4 fields.

Thanks,

Steven