Solved

MSSQL query to show data in a table

Posted on 2007-11-28
8
388 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP populating an array. 4 23
paypal ipn url 5 56
php string detection problem 7 33
000webhost.com default error log 1 28
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 …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
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 …

776 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