Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Connecting to MSSQL and Returning Rows

Posted on 2002-03-06
4
Medium Priority
?
325 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
[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
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
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.
Suggested Courses

609 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