Getting Error on mysql_query() Getting Error on mysql_query()
 

News:

cpg1.5.48 Security release - upgrade mandatory!
The Coppermine development team is releasing a security update for Coppermine in order to counter a recently discovered vulnerability. It is important that all users who run version cpg1.5.46 or older update to this latest version as soon as possible.
[more]

Main Menu

Getting Error on mysql_query()

Started by lambda, October 17, 2012, 12:25:42 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

lambda

I am trying to connect to a separate database outside of Coppermine, so I can display some user information inside my custom header. When I connect to the database, I get the error:

Template error
Failed to find block 'output_buffer' (#<!-- BEGIN output_buffer -->(.*?)<!-- END output_buffer -->#s) in :
                    <div class="{CSS_CLASS}">
                        <h2>{HEADER_TXT}</h2>
                        <span class="cpg_user_message">{MESSAGE}</span>
<!-- BEGIN file_line -->
                        <br />
                        <br />
                        {FILE_TXT}{FILE} - {LINE_TXT}{LINE}
<!-- END file_line -->

                        <br /><br />
                    </div>


I am using the following to try to connect to my database:

$connection = mysql_connect(MMD_LOGHOST, MMD_LOGUSER, MMD_LOGPASS);
@mysql_select_db(MMD_LOGNAME) or die( "Unable to select database");

$res = mysql_query("SELECT * FROM `mmd_users` WHERE `username` = '$mmd_username'");
while($row = mysql_fetch_array($res))
{
if($row['avatar'] != "")
{
$avatar = $url0;
}
else
{
$avatar = $url1;
}

$mmd_unreadpms = $row['unreadpms'];
}


If someone could help me out, I'd really appreciate it. I love this software and am really glad it was released and publicly supported.

Many thanks!

lambda

Sorry, I forgot to say. Whenever I take out the database connect code, the error disappears and the gallery works fine and how it is supposed to. Only when I add the code to connect to the database outside of Coppermine does it give me that error.

Jeff Bailey

Thinking is the hardest work there is, which is probably the reason why so few engage in it. - Henry Ford

lambda

I don't really see why it matters, I am not running the code currently so you can't even see the error. But it's http://www.modmydesktop.com/gallery/

Jeff Bailey

Just a mandatory board rule all users have to follow.

That error comes from

/******************************************************************************
** Section <<<$template_cpg_die>>> - START
******************************************************************************/
// HTML template used by the cpg_die function
$template_cpg_die = <<<EOT
                    <div class="{CSS_CLASS}">
                        <h2>{HEADER_TXT}</h2>
                        <span class="cpg_user_message">{MESSAGE}</span>
<!-- BEGIN file_line -->
                        <br />
                        <br />
                        {FILE_TXT}{FILE} - {LINE_TXT}{LINE}
<!-- END file_line -->
<!-- BEGIN output_buffer -->
                        <br />
                        <br />
                        <div align="left">
                                {OUTPUT_BUFFER}
                        </div>
<!-- END output_buffer -->
                        <br /><br />
                    </div>

EOT;
/******************************************************************************
** Section <<<$template_cpg_die>>> - END
******************************************************************************/


Your code is most likely failing and trying to run the die command and Coppermine can't find the template or you edited it in some way.

Quote from: lambda on October 17, 2012, 12:25:42 PM
MMD_LOGHOST, MMD_LOGUSER, MMD_LOGPASS
Are these suppose to be variables or did you just obfuscate the values?

Is there other code in your custom header? If so can you post it.
Thinking is the hardest work there is, which is probably the reason why so few engage in it. - Henry Ford

lambda

Yeah my entire header looks like this:


<div id="container">

<header>
<section class="header_fix">
<a class="logo" href="http://www.modmydesktop.com"></a><!-- /.logo -->
<section class="contact_detail">Stay Connected to MMD</section><!-- /.contact_detail -->
<section class="social_links">
<a href="http://feeds.feedburner.com/ModMyDesktop-Rss" class="rss"></a>
<a href="http://plus.google.com/u/0/104234050699130600340" class="google"></a>
<a href="http://www.youtube.com/modmydesktop" class="youtube"></a>
<a href="http://www.facebook.com/mod.mydesktop" class="facebook"></a>
<a href="http://www.twitter.com/modmydesktop" class="twitter"></a>
</section><!-- /.social_links -->
</section><!-- /.header_fix -->
</header><!-- /header -->

<nav>
<section class="nav_fix">
<section class="nav_fix_in">

<ul class="main-menu">
<li <?php if($mmd_navcurrent == 1){ echo " class=\"current_page_item\""; } ?>><a href="http://www.modmydesktop.com">Mod My Desktop</a>
  <ul>
  <li><a href="http://www.modmydesktop.com">Home</a></li>
  <li><a href="http://www.modmydesktop.com/latest">Latest Video</a></li>
  </ul>
</li>
<li <?php if($mmd_navcurrent == 2){ echo " class=\"current_page_item\""; } ?>><a href="http://www.modmydesktop.com/tutorials">Computer Tutorials</a>
  <ul>
  <li><a href="http://www.modmydesktop.com/customize">Customize Desktop</a></li>
  <li><a href="http://www.modmydesktop.com/speed">Computer Speed</a></li>
  <li><a href="http://www.modmydesktop.com/tips">Tips & Tweaks</a></li>
  <li><a href="http://www.modmydesktop.com/video">Video Tutorials</a></li>
  </ul>
</li>
<li <?php if($mmd_navcurrent == 3){ echo " class=\"current_page_item\""; } ?>><a href="http://www.modmydesktop.com/gallery/">Gallery</a>
  <ul>
  <li><a href="http://www.modmydesktop.com/gallery/index-3.html">Desktop Wallpapers</a>
  <ul class="secondary">
  <li><a href="http://www.modmydesktop.com/gallery/thumbnails-7.html">Clean & Simple</a></li>
  <li><a href="http://www.modmydesktop.com/gallery/thumbnails-8.html">Abstract</a></li>
  <li><a href="http://www.modmydesktop.com/gallery/thumbnails-9.html">Gaming</a></li>
  <li><a href="http://www.modmydesktop.com/gallery/thumbnails-10.html">Blondes/Brunettes</a></li>
  </ul>
  </li>
  <li><a href="http://www.modmydesktop.com/gallery/thumbnails-2.html">Windows Themes</a></li>
  <li><a href="http://www.modmydesktop.com/gallery/thumbnails-3.html">Rainmeter Skins</a></li>
  <li><a href="http://www.modmydesktop.com/gallery/thumbnails-4.html">Dock Themes</a></li>
  <li><a href="http://www.modmydesktop.com/gallery/thumbnails-5.html">Icon Sets</a></li>
  </ul>
</li>
<li <?php if($mmd_navcurrent == 4){ echo " class=\"current_page_item\""; } ?>><a href="http://www.modmydesktop.com/forums/">Forums</a></li>
<li <?php if($mmd_navcurrent == 5){ echo " class=\"current_page_item\""; } ?>><a href="http://www.modmydesktop.com/contact">Contact</a></li>
<?php

if(!USER_ID)
{
echo '
<li class="buynow">MMD Login</a>
<ul class="mmdlogin">
<div id="menulogin">
<form action="../forums/member.php" method="post">
<fieldset>
<table class="login" width="100%">
<tr>
<td class="login" width="15%" height="35px">
<label for="author">Username</label>
</td>
<td class="login" width="85%" height="35px">
<input type="text" class="inputlogin" id="login_user" name="username" size="25" maxlength="30" /><br />
</td>
</tr>
<tr>
<td class="login" width="15%" height="35px">
<label for="comment">Password</label>
</td>
<td class="login" width="85%" height="35px">
<input type="password" class="inputlogin" id="login_pass" name="password" size="25" />
</td>
</tr>
<input type="hidden" id="action" name="action" value="do_login">
<input type="hidden" id="url" name="url" value="index.php" />
<tr>
<td class="login" width="15%" height="30px">
</td>
<td class="login" width="85%" height="30px">
<input name="submit" type="submit" id="submitlogin" tabindex="5" value="Login" />
</td>
</tr>
</table>
</fieldset>
</form>
Not a member? <a href="http://www.modmydesktop.com/forums/member.php?action=register" class="loginlink">Register Now!</a>
</div>
</ul>
</li>
'
;
}
else
{
global $REFERER;
list($timestamp$form_token) = getFormToken();
$mmd_username USER_NAME;

DEFINE ('MMD_LOGUSER''xxxmyusernamexxx');  
DEFINE ('MMD_LOGPASS''xxxmypasswordxxx');  
DEFINE ('MMD_LOGHOST''localhost');  
DEFINE ('MMD_LOGNAME''xxxmydbxxx');

$connection mysql_connect(MMD_LOGHOSTMMD_LOGUSERMMD_LOGPASS);
@mysql_select_db(MMD_LOGNAME) or die("Unable to select database");

$res mysql_query("SELECT * FROM `mybb_users` WHERE `username` = '$mmd_username'") or die(mysql_error());
while($row mysql_fetch_array($res))
{
if($row['avatar'] != "")
{
$avatar "http://www.modmydesktop.com/forums/{$row['avatar']}";
}
else
{
$avatar "http://www.modmydesktop.com/forums/images/mmdefaultavatar.png";
}

$mmd_unreadpms $row['unreadpms'];
}

echo '
<li class="loggedin"><img src="'
.$avatar.'" id="logavatar" height="30" width="30" />User: '.$mmd_username.'</a>
<ul>
<li class="loggedmenu"><a href="http://www.modmydesktop.com/forums/usercp.php">Control Panel</a>
<li class="loggedmenu"><a href="http://www.modmydesktop.com/forums/private.php">PM\'s [ '
.$mmd_unreadpms.' ]</a></li>
<li class="loggedmenu"><a href="logout.php?form_token='
.$form_token.'&amp;timestamp='.$timestamp.'&amp;referer='.$REFERER.'">Logout</a></li>
</ul>
</li>
'
;

mysql_close($connection);
}

?>

</ul>
</section><!-- /.nav_fix_in -->
</section><!-- /.nav_fix -->
</nav><!-- /nav -->


The divs that are opened in the header and closed properly in the footer. Without the connecting to SQL code, everything works fine. With it, though, I get that error.

Jeff Bailey

First check your custom themes theme.php for $template_cpg_die

Are you using Coppermines Custom Header feature, Anycontent or are you including your file in a different way?

Then try this in place of your connection code:

$MMD = array(
    loguser => 'xxxmyusernamexxx',
    logpass => 'xxxmypasswordxxx',
    loghost => 'localhost',
    logname => 'xxxmydbxxx'
);
function external_db_connect()
{
    global $MMD;

    $result = @mysql_connect($MMD['loghost'], $MMD['loguser'], $MMD['logpass']);

    if (!$result) {
        return false;
    }

    if (!mysql_select_db($MMD['logname'])) {
        return false;
    }

    if (!empty($CONFIG['dbcharset'])) {
        mysql_query("SET NAMES '{$CONFIG['dbcharset']}'", $result);
    }

    return $result;
}

$connection = external_db_connect();

if (!$connection) {
    die('Unable to connect to database!' . mysql_error())
}
Thinking is the hardest work there is, which is probably the reason why so few engage in it. - Henry Ford

lambda

My theme.cfg doesn't have a template_cpg_die in it, I am using the Curve theme slightly modified.

Yes, I am using the Custom Header feature that is inside the config options.

I will try the code and report back :)

Jeff Bailey

Just noticed
$mmd_username

put {} around that.

Forgot to include query code if needed:

function external_db_query($query, $use_link_id = 0)
{
    global $CONFIG, $query_stats, $queries;

    if ($use_link_id) {
        $link_id = $use_link_id;
    } else {
        $link_id = $connection
    }

    $query_start = cpgGetMicroTime();
   
    $result = mysql_query($query, $link_id);
   
    $query_end = cpgGetMicroTime();

    if (!isset($CONFIG['debug_mode']) || $CONFIG['debug_mode'] == 1 || $CONFIG['debug_mode'] == 2) {
        $trace = debug_backtrace();
        $last = $trace[0];
        $localfile = str_replace(realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..') . DIRECTORY_SEPARATOR , '', $last['file']);

        $duration      = round(($query_end - $query_start) * 1000);
        $query_stats[] = $duration;
        $queries[]     = "$query [$localfile:{$last['line']}] ({$duration} ms)";
    }

    if (!$result && !defined('UPDATE_PHP')) {
        $trace = debug_backtrace();
        $last = $trace[0];
        $localfile = str_replace(realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..') . DIRECTORY_SEPARATOR , '', $last['file']);
        cpg_db_error("While executing query '$query' in $localfile on line {$last['line']}", $link_id);
    }

    return $result;
}

$mmd_username = USER_NAME;
$result = external_db_query("SELECT * FROM mybb_users WHERE username = {$mmd_username}");
while ( ($row = mysql_fetch_array($result)) ) {
    if($row['avatar'] != "") {
            $avatar = "http://www.modmydesktop.com/forums/{$row['avatar']}";
        } else {
            $avatar = "http://www.modmydesktop.com/forums/images/mmdefaultavatar.png";
        }
        $mmd_unreadpms = $row['unreadpms'];
    } // while
mysql_free_result($result)
Thinking is the hardest work there is, which is probably the reason why so few engage in it. - Henry Ford

lambda

Sorry, I'm really a basic beginner in PHP.  I have this as my current code:

else
{
global $REFERER;
list($timestamp, $form_token) = getFormToken();

$MMD = array(
loguser => 'xxxmyusernamexxx',
logpass => 'xxxmypasswordxxx',
loghost => 'localhost',
logname => 'xxxmydbxxx'
);
function external_db_connect()
{
global $MMD;

$result = @mysql_connect($MMD['loghost'], $MMD['loguser'], $MMD['logpass']);

if (!$result) {
return false;
}

if (!mysql_select_db($MMD['logname'])) {
return false;
}

if (!empty($CONFIG['dbcharset'])) {
mysql_query("SET NAMES '{$CONFIG['dbcharset']}'", $result);
}

return $result;
}

$connection = external_db_connect();

if (!$connection) {
die('Unable to connect to database!' . mysql_error());
}

function external_db_query($query, $use_link_id = 0)
{
global $CONFIG, $query_stats, $queries;

if ($use_link_id) {
$link_id = $use_link_id;
} else {
$link_id = $connection;
}

$query_start = cpgGetMicroTime();

$result = mysql_query($query, $link_id);

$query_end = cpgGetMicroTime();

if (!isset($CONFIG['debug_mode']) || $CONFIG['debug_mode'] == 1 || $CONFIG['debug_mode'] == 2) {
$trace = debug_backtrace();
$last = $trace[0];
$localfile = str_replace(realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..') . DIRECTORY_SEPARATOR , '', $last['file']);

$duration      = round(($query_end - $query_start) * 1000);
$query_stats[] = $duration;
$queries[]     = "$query [$localfile:{$last['line']}] ({$duration} ms)";
}

if (!$result && !defined('UPDATE_PHP')) {
$trace = debug_backtrace();
$last = $trace[0];
$localfile = str_replace(realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..') . DIRECTORY_SEPARATOR , '', $last['file']);
cpg_db_error("While executing query '$query' in $localfile on line {$last['line']}", $link_id);
}

return $result;
}

$mmd_username = USER_NAME;
$result = external_db_query("SELECT * FROM mybb_users WHERE username = {$mmd_username}");
while ( ($row = mysql_fetch_array($result)) ) {
if($row['avatar'] != "") {
$avatar = "http://www.modmydesktop.com/forums/{$row['avatar']}";
} else {
$avatar = "http://www.modmydesktop.com/forums/images/mmdefaultavatar.png";
}
$mmd_unreadpms = $row['unreadpms'];
} // while

echo '
<li class="loggedin"><img src="'.$avatar.'" id="logavatar" height="30" width="30" />User: '.$mmd_username.'</a>
<ul>
<li class="loggedmenu"><a href="http://www.modmydesktop.com/forums/usercp.php">Control Panel</a>
<li class="loggedmenu"><a href="http://www.modmydesktop.com/forums/private.php">PM\'s [ '.$mmd_unreadpms.' ]</a></li>
<li class="loggedmenu"><a href="logout.php?form_token='.$form_token.'&amp;timestamp='.$timestamp.'&amp;referer='.$REFERER.'">Logout</a></li>
</ul>
</li>
';

mysql_free_result($result);
}


And with my information replaced with the correct one, it gives me a "cannot connect to database" error. Is this the correct code? I wasn't sure since you posted 2 times. Sorry for misunderstanding :(

Thank you for helping me out though, I really appreciate it!

Jeff Bailey

Quote from: lambda on October 18, 2012, 01:51:32 AM
"cannot connect to database" error.
Is what I wanted to see. No template error now?

That just means that your login information is wrong or we need to update new_link property of mysql_connect()

Edit functions.inc.php
Find
$result = @mysql_connect($CONFIG['dbserver'], $CONFIG['dbuser'], $CONFIG['dbpass']);
Replace with
$result = @mysql_connect($CONFIG['dbserver'], $CONFIG['dbuser'], $CONFIG['dbpass'], true);

Edit the code I posted
Find
$result = @mysql_connect($MMD['loghost'], $MMD['loguser'], $MMD['logpass']);
Replace with
$result = @mysql_connect($MMD['loghost'], $MMD['loguser'], $MMD['logpass'], true);
Thinking is the hardest work there is, which is probably the reason why so few engage in it. - Henry Ford

lambda

Yeah, there doesn't seem to be any template error now, which is good.

I updated the functions.inc.php and the code you posted; quadruple checked the login info to make sure it is correct, and I am still getting the "Unable to connect to database!" error.

lambda

Instead of using the array and declaring it as a global, I tried inputting the login details straight into the connect function.

I got this error:

Fatal error: Cannot redeclare external_db_connect() (previously declared in /home/cclarke/public_html/gallery/header.php:167) in /home/cclarke/public_html/gallery/header.php on line 164

Which leads to line:

function external_db_connect()

I am 100% sure that the info is correct though, I tried a small test on a page in my root directory of my website with the same details and it connected.

Jeff Bailey

I sent you a PM.

Quote from: lambda on October 18, 2012, 02:18:19 AM
Fatal error: Cannot redeclare external_db_connect() (previously declared in /home/cclarke/public_html/gallery/header.php:167) in /home/cclarke/public_html/gallery/header.php on line 164
I don't know how there would be two of these unless you pasted it twice.
Thinking is the hardest work there is, which is probably the reason why so few engage in it. - Henry Ford

lambda

I've replied to your PM, thank you in advanced.

Jeff Bailey

Got rid of the connection and just selected the mybb database and then selected the coppermine database.

global $REFERER;
list($timestamp, $form_token) = getFormToken();

$mmd_username = USER_NAME;

mysql_select_db('databasename');

$result = mysql_query("SELECT * FROM `mybb_users` WHERE `username` = '{$mmd_username}'") or die(mysql_error());
while($row = mysql_fetch_array($result))
{
if($row['avatar'] != "")
{
$avatar = "http://www.modmydesktop.com/forums/{$row['avatar']}";
}
else
{
$avatar = "http://www.modmydesktop.com/forums/images/mmdefaultavatar.png";
}

$mmd_unreadpms = $row['unreadpms'];

} // while

echo '
<li class="loggedin"><img src="'.$avatar.'" id="logavatar" height="30" width="30" />User: '.$mmd_username.'</a>
<ul>
<li class="loggedmenu"><a href="http://www.modmydesktop.com/forums/usercp.php">Control Panel</a>
<li class="loggedmenu"><a href="http://www.modmydesktop.com/forums/private.php">PM\'s [ '.$mmd_unreadpms.' ]</a></li>
<li class="loggedmenu"><a href="logout.php?form_token='.$form_token.'&amp;timestamp='.$timestamp.'&amp;referer='.$REFERER.'">Logout</a></li>
</ul>
</li>
';

mysql_free_result($result);
mysql_select_db($CONFIG['dbname']);
Thinking is the hardest work there is, which is probably the reason why so few engage in it. - Henry Ford

lambda

Yeah, perfect. Thanks for your time on this and I think you might have saved me from going a bit more mentally insane than I already am  ::)

You can mark this as solved, thanks again!