Solved

Connecting to MSSQL and Returning Rows

Posted on 2002-03-06
4
304 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 50 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
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 …

747 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

12 Experts available now in Live!

Get 1:1 Help Now