/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Trouble with MySQL queries from within functions

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);
}
}
[/code]

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′ />”;
}
}
[/code]

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

[/QUOTE]

I’d be grateful for any help on this, I hope it is just a stupid mistake.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@MrCoderDec 21.2007 — [code=php]
//first connect to the database
$con = belDB(0);
echo "<pre>".var_export($con, true)."</pre>";
die();
[/code]


What does that echo?
Copy linkTweet thisAlerts:
@blue-eye-labsauthorDec 21.2007 — it echoes "NULL" which I suppose means that the connection function is not functioning, unfortunately.

I don't know why not though, because it uses exactly the same method that I used to use to connect but it seems that because it is inside a function it is not getting returned properly.
Copy linkTweet thisAlerts:
@blue-eye-labsauthorDec 21.2007 — after some extreme stupidity on my part, I realised that the problem lay in using the name of the table in mysql_select_db() instead of the name of the database...

sorry for wasting anyone's time.
×

Success!

Help @blue-eye-labs spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...