Solved

MSSQL query to show data in a table

Posted on 2007-11-28
8
389 Views
Last Modified: 2013-12-12
I just finished making my php working with mssql. Now I am using this to connect to the mssql database

<?php
 
$conn = mssql_connect('localhost','user','pass') or die("password error");
 
if($conn)
{
    echo "Connected Successfully!";
}
else
{
    echo "Connection Failed!";
      echo mssql_error();
}

?>

what i want to check now is that if it is connected to the database then show the data in the table called mytable. the fields in the table are FirstName, LastName, Email, Zip

Thanks
0
Comment
Question by:syedasimmeesaq
  • 5
  • 3
8 Comments
 
LVL 21

Expert Comment

by:nizsmo
ID: 20371421
hi syedasimmeesag,

How about something like below?
<?php
 
$conn = mssql_connect('localhost','user','pass') or die("password error");
 
if($conn)
{
    echo "Connected Successfully!";
}
else
{
    echo "Connection Failed!";
      echo mssql_error();
}
 
$query = "SELECT * FROM mytable";
 
$result = mssql_query($query);
 
while($row = mssql_fetch_array($result))
{
	echo $row["FirstName"] . " " . $row["LastName"] . " " . $row["Email"] . " " . $row["Zip"] . "<br>";
}
mssql_close();
?>

Open in new window

0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20371426
I'm sorry, didn't see that you want it in a table, something like this then should work.
<?php
 
$conn = mssql_connect('localhost','user','pass') or die("password error");
 
if($conn)
{
    echo "Connected Successfully!";
}
else
{
    echo "Connection Failed!";
      echo mssql_error();
}
 
$query = "SELECT * FROM mytable";
 
$result = mssql_query($query);
echo "<table>"
while($row = mssql_fetch_array($result))
{
	echo "<tr><td>" . $row["FirstName"] . "</td><td>" . $row["LastName"] . "</td><td>" . $row["Email"] . "</td><td>" . $row["Zip"] . "</td></tr>";
}
echo "</table>"
mssql_close();
?>

Open in new window

0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20371486
Thanks for te response. i have a field in MSSQL table that has the values like this
d9907551-d668-51c5-a5a8-0773d3340ca6

and when I try to get the value for ID it shows me something like this
ÌU.°¸’JªYÊÇ»˜ªx

why is it happening and how do i get the correct response.
thanks
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 21

Expert Comment

by:nizsmo
ID: 20371498
Is it stored properly in the database? What field type is it and does the field contain any foreign characters?
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20371523
This is a uniqueidentifier field. It doesn't contain any foregin characters.
thanks
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20371529
what is your code? and how are you printing out the id field?
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20371560
<?php
 
$conn = mssql_connect('localhost','user','pass') or die("password error");
 $db = mssql_select_db('RTI', $conn) or die("couldn't find db");
if($conn)
{
    $query = "SELECT ID,Password FROM mytable";
$result = mssql_query($query);
while ( $record = mssql_fetch_array($result) )
{
      echo $record["ID"] . ", " . $record["Password"] . "<br />";
}
      //echo "Connected Successfully!";
      //echo mssql_get_last_message();
}
else
{
    echo "Connection Failed!";
      echo mssql_error();
}

?>
thanks
0
 
LVL 21

Accepted Solution

by:
nizsmo earned 500 total points
ID: 20371570
Doesn't look like there is anything wrong with your code, can you see the ID properly through the database? If your password is being echoed out properly, there's no reason why the ID is not.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question