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
Solved

Connecting to MSSQL and Returning Rows

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 look for a specific file type in a local or remote server directory using PHP.

840 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