[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Connecting to MSSQL and Returning Rows

Posted on 2002-03-06
4
Medium Priority
?
332 Views
Last Modified: 2012-05-04
I am trying to connect to a SQL Database and just return all rows.  My code is below:

<?php

$hostname = "server_Name";
$username = "user_name";
$password = "pass_word";
$dbName = "test";

MSSQL_CONNECT($hostname,$username,$password);
MSSQL_SELECT_DB($dbName);

$query = "select Name from alpha where Name = "Michael Cary"";
print $query;
$result = MSSQL_QUERY($query);
print $result;
//$number = MSSQL_NUM_ROWS($result);
//print $number;
//$i=0;

//if ($number == 0) : print "There is no data to display...";
//     elseif ($number > 0) : print "Data:";
     
//     while ($i < $number) : $name = mssql_result($result,$i,"Data");          
//     print $name; print "";
//     $i++;
//     endwhile;
//endif; ?>
0
Comment
Question by:caryma
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
andriv earned 150 total points
ID: 6844540
The mysql_fetch_array will take the next row and make an array with fields indexed by name and/or number. The example below uses both methods.  

$hostname = "server_Name";
$username = "user_name";
$password = "pass_word";
$dbName = "test";

MSSQL_CONNECT($hostname,$username,$password);
MSSQL_SELECT_DB($dbName);

$query = "select Name from alpha where Name = "Michael Cary"";
print $query;
$result = MSSQL_QUERY($query);

while($row=mysql_fetch_array($result))
{
print("$row[col1name],$row[col2name]...");
print("$row[0],$row[1]...");
}
0
 
LVL 5

Expert Comment

by:dkjariwala
ID: 6847002
Try following code,

<?php

$hostname = "server_Name";
$username = "user_name";
$password = "pass_word";
$dbName = "test";

MSSQL_CONNECT($hostname,$username,$password) or die('Can not connect to MS SQL');
MSSQL_SELECT_DB($dbName) or die("Can not select database $dbName");

$query = "select Name from alpha where Name = "Michael Cary"";
print $query;
$result = MSSQL_QUERY($query) or die("Can not execute query $query. ")  ;

/// print $result; //No need to print value of result.

while($row = mssql_fetch_array($result))
{
     //mssql fetch array fetches one record in an array whose indices are name of the fields.

     $name = $row['Name'] ; //so you here user name of your field as index, i.e. 'Name
     print "Name is $name";
     //also mssql_fetch_array advances pointer to next record , so this while loop would go through all the results !!
}

?>

Check out my comments.
Also see how I have used the die function.

Hope that solves your problem,
JD

P.S. : Andriv, when dealing with mssql, you use mssql_fetch_array rather than mysql_fetch_array.

0
 
LVL 5

Expert Comment

by:andriv
ID: 6847718
Just a simple mistype. Good answer I seen it somewhere before.
0
 

Expert Comment

by:SpideyMod
ID: 8287206
Force Accepted

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses

830 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