database call

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nate
Forum Newbie
Posts: 14
Joined: Tue Feb 12, 2008 6:45 pm

database call

Post by nate »

I have this code that I'm trying to get to work with no success. Can anybody out there help? Basically I'm making a simple call to my database but I'm not very experienced at this. Here is my code:

<?php

$location = $_SERVER['PHP_SELF'];
$domain = $_SERVER['HTTP_HOST'];

$host = "localhost";
$username = "***";
$password = "***";
$database = "***";

$connect = mysql_connect($host, $username, $password);
$select = mysql_select_db($database, $connect);

// DOCTYPE & CHARSET

$query1 = sprintf("SELECT mod_code FROM mod_doctype_charset WHERE url='%s'", mysql_real_escape_string($domain));
$result1 = mysql_query($query1);
$data1 = mysql_fetch_row($result1);
if (!$data1[0]) {
pass;
}
else {
echo $data1[0] . "\n";
}
?>

Thanks,

Nate
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: database call

Post by John Cartwright »

Please do not post your database credentials; I have removed it for you.

Also, please edit your post and add the appropriate

Code: Select all

 
tags around your php.

Considering you did not post any relevant information about the expected output, and the given output, I will assume your query is failing.

It is always advisable (atleast in testing) to include error outputting.

Code: Select all

$result1 = mysql_query($query1);
to

Code: Select all

$result1 = mysql_query($query1) or die(mysql_error());
What does this output?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: database call

Post by John Cartwright »

Someone just posted then deleted reguarding the

Code: Select all

pass;
line (I had missed this until I saw the post). This is not valid, and will generate a notice.
nate
Forum Newbie
Posts: 14
Joined: Tue Feb 12, 2008 6:45 pm

Re: database call

Post by nate »

I got the code to work by changing the "WHERE url" to "WHERE location". But now I'm having trouble using a function to avoid repetition in the code. None of the modules are appearing...

<?php

$url = $_SERVER['PHP_SELF'];

$host = "asdf";
$username = "asdf";
$password = "asdf";
$database = "asdf";

$connect = mysql_connect($host, $username, $password);
$select = mysql_select_db($database, $connect);

$result = mysql_query($query);
$data = mysql_fetch_row($result);

function getModule() {

if (!$data[0]) {
}

else {
echo $data[0] . "\n";
}

}

// DOCTYPE & CHARSET
$query = sprintf("SELECT mod_code FROM mod_doctype_charset WHERE url='%s'", mysql_real_escape_string($url));
getModule();

// CONTENT: TITLE & META
echo "<title>Good Samaritan Network, Inc.</title>\n";
echo "<meta name=\"description\" content=\"\">\n";
echo "<meta name=\"keywords\" content=\"\">\n";

// LINKS
$query = sprintf("SELECT mod_code FROM mod_links WHERE url='%s'", mysql_real_escape_string($url));
getModule();

// BANNER
$query = sprintf("SELECT mod_code FROM mod_banner WHERE url='%s'", mysql_real_escape_string($url));
getModule();

// MENU
$query = sprintf("SELECT mod_code FROM mod_menu WHERE url='%s'", mysql_real_escape_string($url));
getModule();

// CONTENT: SECTION HEADING
echo "<div id=\"section-heading\">Good Samaritan Network</div>\n";

// SUBMENU
$query = sprintf("SELECT mod_code FROM mod_submenu WHERE url='%s'", mysql_real_escape_string($url));
getModule();

?>

<div id="collage"><img src="assets/images/img-collage.gif" height="56" width="700" alt="Our Mission Is Your Hope"></div>

<div id="body" style="clear: both; margin-top: 2px;">

<div id="rail">

<div class="h5"><h5>Donate Volunteer</h5></div>
<p>Your support is always appreciated. Please call:<br /><br />(850) 933-1449<br /><br /><a href="">Donate Volunteer Form</a></p>

<div class="h5"><h5>GSN Missions</h5></div>
<p>The Good Samaritan Network is a faith-based outreach.<br /><br /><a href="/missions/chelseas-house">Chelsea's House</a></p>

<div class="h5"><h5>Local Missions</h5></div>
<p>These missions are here in support of the homeless of Tallahassee.<br /><br /><a href="/missions/haven-of-rest">Haven of Rest</a><br /><br /><a href="/missions/care-tallahassee">Care Tallahassee</a></p>

<div class="h5"><h5>Supporters</h5></div>
<p>These organizations have generously contributed.<br /><br /><a href="/supporters">GSN Supporters</a></p>

</div>

<div id="content">

<h1>Good Samaritan Network</h1>

<p id="description">Homeless women and children in our area face many problems and are in need of help. It is the mission of the Good Samaritan Network to be a part of the solution to this ever-growing problem. Through various support services, we are able to meet the needs of the homeless; thus, offering them encouragement and hope in God. The Good Samaritan Network is a faith-based outreach who networks with like-minded agencies and ministries for the purpose of restoration to individuals who find themselves facing homelessness. It is our goal to reach out to their physical, emotional, and spiritual needs.</p>

<h2>Events</h2>

<h3 class="event">Chelsea's House Christmas Benefit Dinner</h3>

<ul class="event-list">
<li>6:00 pm, Tuesday, December 2nd, 2008</li>
<li>Reservation Required</li>
<li>Call: (850) 933-1449</li>
</ul>

<h2>Donate</h2>

<h3>Donation Options</h3>

<ol>
<li><a href="">Donate Online</a></li>
<li>Email <a href="mailto:bethburns@gsncares.org">bethburns@gsncares.org</a> for more information</li>
<li>Mail to: GSN, P.O. Box 10527, Tallahassee, Florida 32301</li>
</ol>

<p class="bold">* Your tax deductible gift may now be made directly to GSN, which is 501c3 pending.</p>

</div>

</div>

<div id="footer">

<ul id="footer-menu">
<li><a href="/">gsncares.org</a> | </li><li><a href="/missions">Missions</a> | </li><li><a href="/programs">Programs</a> | </li><li><a href="/events">Events</a> | </li><li><a href="/help">How to Help</a> | </li><li><a href="/press">Press Releases</a> | </li><li><a href="/contact">Contact</a></li>
</ul>

<br /><div>Copyright &#169; 2008 - 2009 Good Samaritan Network, Inc.</div>

</div>

</body></html>
nate
Forum Newbie
Posts: 14
Joined: Tue Feb 12, 2008 6:45 pm

Re: database call

Post by nate »

Declaring globals was the answer to the last question...

<?php

$url = $_SERVER['PHP_SELF'];

$host = "asdf";
$username = "asdf";
$password = "asdf";
$database = "asdf";

$connect = mysql_connect($host, $username, $password);
$select = mysql_select_db($database, $connect);

global $url, $host, $username, $password, $database, $result, $data;

function getModule() {

$result = mysql_query($query);
$data = mysql_fetch_row($result);

if (!$data[0]) {
}
else {
echo $data[0] . "\n";
}

// DOCTYPE & CHARSET
$query = sprintf("SELECT mod_code FROM mod_doctype_charset WHERE url='%s'", mysql_real_escape_string($url));
getModule();

Nate
Post Reply