I'm attempting to migrate to PHP from ASP and am having real problems duplicating the structures that I'm used to. (Yes, I realize that old structures may not work in this environment and I'm willing to change.)
My problem is that I'm trying to remove my database access code to a separate .inc file and run my DB access from a central location.
I'm getting a "Warning: mysql_fetch_array(): supplied resource is not a valid MySQL result resource in C:\Inetpub\sites\PHP\default.php on line 29" error and can't work out what the problem is.
The calling code is:
<?php
include("inc/db.inc");
include("inc/Nav.inc");
error_reporting(2); // 1 = no messages, 2 = show warnings
fnMain("PHP Test");
function fnMain($title) {
PageHeader($title);
DisplayMenu();
switch ($_REQUEST["action"]) {
case "Get DB Records":
DisplayRecords();
break;
case "Do Nothing":
echo "Page - <b>$title</b> - did nothing!";
break;
default:
break;
}
PageFooter();
}
function DisplayRecords() {
$rs = new DB;
// if (function_exists("DB_Read")) {
$rsTest = $rs->DB_Read("Select Item from Excess");
while($row = mysql_fetch_array($rsTest) or die("Error reading data file.")); {
echo $row("Item");
}
// } else {
// echo "Function not available.";
// }
}
function DisplayMenu() { ?>
<p>Select an action from the list below:</p>
<form method="post" action="<? echo $_SERVER['PHP_SELF'] ?>">
<p><input type="submit" name="action" value="Get DB Records"></p>
<p><input type="submit" name="action" value="Do Nothing"></p>
<hr>
</form>
<?php
} ?>
The db.inc file is:
<?php
Class DB {
var $rs;
function DB_Read($query) {
$Conn = mssql_connect ("DELL7000", "php", "php") or die ("Unable to connect to database.");
$db = "Axisold";
mssql_select_db($db, $Conn) or die ("Unable to select database.");
$rsX = mssql_query($query) or die("Error retrieving data from database.");
$this -> rs = $rsX;
return $this->rs;
}
}
?>
The nav.inc is:
<?php
function PageHeader($title) { ?>
<html>
<head>
<title><?php echo $title ?></title>
<style>
p {
font-family: sans-serif;
font-size: 11pt;
}
h1 {
font-family : sans-serif;
font-size : 14pt;
color : #03008B;
text-align: center;
}
</style>
</head>
<body bgcolor="black">
<table width="80%" align="center" cellpadding="20">
<tr bgcolor="#ffffff">
<td>
<h1><?php echo $title . ": " . $_SERVER['PHP_SELF'] ?></h1>
<?php }
function PageFooter() {?>
</td>
</tr>
</table>
</body>
</html>
<?php } ?>
This is all the code that is in the test environment.
Obviously the db connection string and query would need to change on other systems.
Any suggestions would be great.
Brett
PS: I've been programming in PHP for about 4 days now so please be gently on me.🙂