Solved

MSSQL query to show data in a table

Posted on 2007-11-28
8
385 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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…
The viewer will learn how to count occurrences of each item in an array.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now