Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query getting no records

Status
Not open for further replies.

andyc209

IS-IT--Management
Dec 7, 2004
98
0
0
GB
Please bear with me as i am trying PHP for the first time

i have the following code and it gives me no errors but no records - am i missing something?

<?php
// Connects to your Database
$dsn="DBNAME";
$username="sa";
$password="xxxxxxx";
$sqlsrv_connect=odbc_connect($dsn, $username, $password);
$strSQL = "SELECT * FROM TBL_INSTRUCTIONS ORDER BY DATEADDED DESC";
// Execute the query (the recordset $rs contains the result)
$rs = sqlsrv_query($strSQL);
// Loop the recordset $rs
while($row = sqlsrv_fetch_array($rs)) {
?>
<tr>
<td class="with-checkbox">
<input type="checkbox" name="check" value="1">
</td>
<td><?php echo $row['FIRSTNAME']?></td>
<td><?php echo $row['SURNAME']?></td>
</tr>
<?php
}
// Close the database connection
mysql_close();
?>
 
No errors may indicate that your PHP is sound.

No records may indicate that your query has a fault. Does "SELECT * FROM TBL_INSTRUCTIONS ORDER BY DATEADDED DESC" render a result when you run it directly on the database (without PHP)?
 
You might also try terminating your echo statements with a semi colon.

Code:
<?php echo $row['FIRSTNAME']?>

...

Code:
<?php echo $row['FIRSTNAME']; ?>
 
No errors usually indicates PHP is not setup to show them.

I question the use of an odbc connection, but then using sqlsrv methods to access data.

If you are going to use sqlsrv methods you need to use an sqlsrv connection.

If you are using an odbc connection, then you would want to use odbc methods.

sqlsrv_query requires a connection resource provided by sqlsrv_connect



Code:
<?php
$serverName = "serverName\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}
?>





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
They query works fine in SQL - brings back records.
 

Which again leads me to believe that your PHP is not setup to show errors, and your call sqlsrv_query without an sqlsrv_connect resource is generating an error you cannot see.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I have found the following code online which i can get an error from


<?php

//connect to a DSN "myDSN"
$conn = odbc_connect('PPORTAL','PHP','PHP1');

if ($conn)
echo "odbc connected";
else echo "odbc not connected";
?>

It comes back withh the ODBC not connected line but if i test the ODBC with this username and password it connects fine - even tried connecting using the same parameters with classic ASP and it connects to the database fine - just wanted to learn PHP but even at this early stage its proving a pain. Any ideas
 

Try this:
Code:
$conn = odbc_connect('PPORTAL','PHP','PHP1'); 
echo odbc_errormsg($conn);

See if you get any useful error message.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top