The background is that I’m trying to upgrade my website to make all the php functions object-based as it were, so that I only need to call functions from different files instead of manually writing them into each file that needs them. This also makes updates really easy.
My issue is that I have created a database connection function, and a function which is supposed to get random images from the database and display four of them.
The connection function is in a file at “/library/scripts/php/db_operate.php”:
[code=php]
function belDB($action = 0) {
if($action == 0) {
$server = “localhost”; //server address
$usr = “user”; //database usr name
$pwd = “password”; //database pwd
$database = “images”; //database name
$con = mysql_connect($server, $usr, $pwd) or die(mysql_error());
mysql_select_db($database);
return $con;
}
if($action == 1) {
mysql_close($con);
}
}
The second function is in a file at “/library/scripts/php/getFrontImages.php”:
[code=php]
function getRandomImages() {
//get dependancies
$con = require($_SERVER[‘DOCUMENT_ROOT’] . “/library/scripts/php/db_operate.php”);
//first connect to the database
$con = belDB(0);
//now get the records
$sql = “SELECT * FROM images ORDER BY RAND() LIMIT 4”;
$result = mysql_query($sql, $con);
while($row = mysql_fetch_array($result)) {
//echo the <img> tag
echo “<img class=’home_imgPlace’ alt='” . $row[‘name’] . “‘ src='” . $DIR_pImages_thumbs . $row[‘id’] . “.jpg’ border=’0′ />”;
}
}
Then the second file is called by the home.php page which is in the root directory. When I call the page I get the following MySQL error:
[QUOTE]
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /Applications/xampp/xamppfiles/htdocs/www/library/scripts/php/getFrontImages.php on line 14
I’d be grateful for any help on this, I hope it is just a stupid mistake.