Solved

MSSQL query to show data in a table

Posted on 2007-11-28
8
391 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses four methods for overlaying images in a container on a web page
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

690 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