/    Sign up×
Community /Pin to ProfileBookmark

A modular php connection to myqsl db

Hi everyone,

Quick question. I have used and know that the following code works

[code=php]
<?php

$con = mysql_connect(“localhost”,”root”,”password”);

if(!$con)

{

die(‘Could not connect: ‘ . mysql_error());

}

else

{

mysql_select_db(“bits”, $con);

$ans = mysql_query(“SELECT * FROM `snippets` ORDER BY `Date`, `Time` DESC LIMIT 0, 15”);

while($result = mysql_fetch_array($ans))

{

?>

<h2><a title=”<?php echo $result[“Title”]; ?>” href=”<?php echo $result[“Link”]; ?>”><?php echo $result[“Title”]; ?></a></h2>

<p><?php echo $result[“Text”] ?></p>

<?php

}

mysql_close($con);

}

?>
[/code]

But, it’s a pain and a increases filesize to keep rewriting it everytime I need to access the db. So I tried to write the bulk of the connection and mysql query code in one file and then call that code from another.

My code now looks like this

dbConnect.php

[code=php]
<?php

function connect()
{

$con = mysql_connect(“localhost”,”root”,”password”);

if(!$con)

{

die(‘Could not connect: ‘ . mysql_error());

}

else

{

mysql_select_db(“bits”, $con);
return true;

}

}

function get($db, $title)

{

$request = mysql_query(“SELECT * FROM `bits`.” . $db . “` WHERE `Title` = ‘” . $title . “‘”);

$ans = mysql_fetch_array($request);

return $ans;

}

function getAll($db, $dbcon)

{

$request = mysql_query(“SELECT * FROM `bits`.” . $db . “` ORDER BY `Date`, `Time` DESC LIMIT 0, 15”);

$ans = mysql_fetch_array($request);

return $ans;

}

?>
[/code]

snippet from other file

[code=php]
<div class=”ubuntu”>

<?php

include($_SERVER[DOCUMENT_ROOT] . “/dbConnect.php”);

if(connect())
{
$articles = getAll(“ubuntu”);

while($articles)

{

?>

<h2><a title=”<?php echo $articles[“Title”]; ?>” href=”<?php echo $articles[“Link”]; ?>”><?php echo $articles[“Title”]; ?></a></h2>

<?php

}

echo $articles[“Title”];

?>

</div>
[/code]

So far I’ve not been able to get it to work. Does anyone know how I could alter it so it’s all good?

Thanks a million

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Sep 08.2007 — I don't know, but this works
[code=php]
<?php // db.php

$dbhost = 'server';
$dbuser = 'username';
$dbpass = 'password';

function dbConnect($db='') {
global $dbhost, $dbuser, $dbpass;

$dbcnx = @mysql_connect($dbhost, $dbuser, $dbpass)
or die('The site database appears to be down.');

if ($db!='' and !@mysql_select_db($db,$dbcnx))
die('The site database is unavailable.');

return $dbcnx;
}
?>[/code]
Copy linkTweet thisAlerts:
@DJRobThaManauthorSep 08.2007 — Okay Declan,

Will give it a try.

Thanks
×

Success!

Help @DJRobThaMan 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.20,
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,
)...