ag3nt42 Posted May 20, 2008 Share Posted May 20, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/106483-solved-mssql-wont-give-up-the-info/ Share on other sites More sharing options...
MadTechie Posted May 20, 2008 Share Posted May 20, 2008 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'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/106483-solved-mssql-wont-give-up-the-info/#findComment-545806 Share on other sites More sharing options...
ag3nt42 Posted May 20, 2008 Author Share Posted May 20, 2008 no its not working either pretty much getting the same problem... only now it throws up a 500 error Quote Link to comment https://forums.phpfreaks.com/topic/106483-solved-mssql-wont-give-up-the-info/#findComment-545851 Share on other sites More sharing options...
MatthewJ Posted May 20, 2008 Share Posted May 20, 2008 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/106483-solved-mssql-wont-give-up-the-info/#findComment-545872 Share on other sites More sharing options...
ag3nt42 Posted May 21, 2008 Author Share Posted May 21, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/106483-solved-mssql-wont-give-up-the-info/#findComment-546467 Share on other sites More sharing options...
MatthewJ Posted May 21, 2008 Share Posted May 21, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/106483-solved-mssql-wont-give-up-the-info/#findComment-546470 Share on other sites More sharing options...
ag3nt42 Posted May 21, 2008 Author Share Posted May 21, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/106483-solved-mssql-wont-give-up-the-info/#findComment-546473 Share on other sites More sharing options...
ag3nt42 Posted May 21, 2008 Author Share Posted May 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/106483-solved-mssql-wont-give-up-the-info/#findComment-546511 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.