Solved

MSSQL query to show data in a table

Posted on 2007-11-28
8
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

733 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