Jump to content

Recommended Posts

I'm having some trouble with this... i'm using php to talk with MSSQL 2005 (not express)

I have been able to create and populate tables however I can't seem to retrieve the information.

Its my understanding that the information needs to be placed into an array so I have done so.. yet no matter what variation i try I cannot get it to retrieve and display the info..

 

below is the code i'm using:

<?php
$con = mssql_connect("localhost","MyUsername","Mypassword");


if (!$con)
 {
 die('Could not connect: ' . mssql_error());
 }

mssql_select_db("MyDatabase", $con);

$result = mssql_query("SELECT * FROM Account Numbers");

for ($i = 0; $i < mssql_num_rows( $result ); $i++)
   {
       $row = mssql_fetch_row($result);
   }

Echo ("AccountNumbersID: ".$row[0]."<br />");
Echo ("Account Number: ".$row[1]."<br />");
Echo ("Description: ".$row[2]."<br />");
Echo ("Appropriation Balance: ".$row[3]."<br />");
Echo ("Net Change: ".$row[4]."<br />");
Echo ("Ending Balance: ".$row[5]."<br />");

echo('Cheese');
?>

 

 

thanx in advance everyone..

 

~ag3nt42

Link to comment
https://forums.phpfreaks.com/topic/106483-solved-mssql-wont-give-up-the-info/
Share on other sites

try this

 

<?php
$con = mssql_connect("localhost","MyUsername","Mypassword");


if (!$con)
{
die('Could not connect: ' . mssql_error());
}

mssql_select_db("MyDatabase", $con);

$result = mssql_query("SELECT * FROM Account Numbers")or die(mssql_error());

while($row = mssql_fetch_row($result))
{
Echo ("AccountNumbersID: ".$row[0]."<br />");
Echo ("Account Number: ".$row[1]."<br />");
Echo ("Description: ".$row[2]."<br />");
Echo ("Appropriation Balance: ".$row[3]."<br />");
Echo ("Net Change: ".$row[4]."<br />");
Echo ("Ending Balance: ".$row[5]."<br />");
}

echo('Cheese');
?>

The following works for me with sql 2005, you shouldbe able to plug your values and be okay.

 

Hope it helps

 

<?php

    //Host
    $SERVER = "yourhost";
   //Account to login with
    $ADMIN_NAME = "yourlogin";
    //complete pass with your pass
    $ADMIN_PASS = "yourpass";
    //name of database you want to connect to
    $DATABASE = "yourdatabase";

    $conn = mssql_connect($SERVER, $ADMIN_NAME, $ADMIN_PASS)
        or die ("Can't connect to Microsoft SQL Server");
       
    mssql_select_db($DATABASE, $conn)
        or die ("Can't connect to Database");

$sql = "SELECT *
        FROM yourtable";

$result = mssql_query($sql, $conn) or die("Wrong database name");
$row_rsRes = mssql_fetch_assoc($result);
             $totalRows_rsRes = mssql_num_rows($result);
echo "<table>";
echo "<tr><td><b>Header1</b></td><td><b>Header2</b></td";
do { ?>
          <tr>
            <td width="15%"><?php echo $row_rsRes['field1']; ?></td>
            <td width="15%"><?php echo $row_rsRes['field2']; ?></td>
          </tr>
          <?php } while ($row_rsRes = mssql_fetch_assoc($result)); ?>
	  
<?php          
   echo "</table>";		
?> 

i tried pluggin my info into this but it keeps coming back with "wrong database name"

even tho i'm 100%positive the database name is correct.. I can use the exact same info to populate the tables or create them but I just can't seem to retrieve anything.

 

anyone know if any settings that might need to be changed..?

 

thanx again for the help everyone

Do notice that "Wrong database name" is the error I put in at the mssql_query()... This was specific to the app I was developing.

 

You may want to change that, it is more likely that the sql query is failing. If not, you would get the "Can't connect to Database" message from mssql_select_db().

 

do you have the ability to run the sql through SQL Server management studio or something like that to see if the query alone works?

yes i do.. the queries do work if i run them right on the database through the SQL studio.. but they won't retrieve and display the info when I try to rip them out with php

 

I actually just tested it... for some reason if i place brackets around the data names then it goes as far as to echo out the "HEADER" strings but then i still am not getting ne information displayed

wow thanx for all the help everyone i think i got this problem licked...

 

mathew your code wasn't working for me for somereason but when i went back to the old code i was using and tossed the database names into brackets...

 

WAHLAH!!! info retrieved... thanx alot for the help guys.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.