News:

Wondering if this will always be free?  See why free is better.

Main Menu

PHP Coding Conflict

Started by eRott, November 25, 2007, 12:45:34 AM

Previous topic - Next topic

eRott

Hello,

I am having a bit of trouble with some PHP code involving the forum. This is the error I receive on the page in question (index.php).. my main page.

Warning: mysql_query(): 9 is not a valid MySQL-Link resource in /home/REMOVED/public_html/forums/Sources/Subs.php on line 321

Warning: mysql_error(): 9 is not a valid MySQL-Link resource in /home/REMOVED/public_html/forums/Sources/Errors.php on line 123

Warning: mysql_errno(): 9 is not a valid MySQL-Link resource in /home/REMOVED/public_html/forums/Sources/Errors.php on line 124

Warning: mysql_query(): 9 is not a valid MySQL-Link resource in /home/REMOVED/public_html/forums/Sources/Subs.php on line 321
Database Error:
File: /home/REMOVED/public_html/forums/SSI.php
Line: 1288
Warning: mysql_query(): 9 is not a valid MySQL-Link resource in /home/REMOVED/public_html/forums/Sources/Subs.php on line 321

Warning: mysql_error(): 9 is not a valid MySQL-Link resource in /home/REMOVED/public_html/forums/Sources/Errors.php on line 123

Warning: mysql_errno(): 9 is not a valid MySQL-Link resource in /home/REMOVED/public_html/forums/Sources/Errors.php on line 124

Warning: mysql_query(): 9 is not a valid MySQL-Link resource in /home/REMOVED/public_html/forums/Sources/Subs.php on line 321
Database Error:
File: /home/REMOVED/public_html/forums/Sources/Load.php
Line: 2005


This error only seems to happen when I add this code to the web page:


... other code ...

<?php
include("members/global/db-config.php");
include(
"members/global/db-open.php");

$sql "SELECT * FROM news ORDER BY id DESC LIMIT 0,5";
$result mysql_query($sql) or die(mysql_error());

while (
$list mysql_fetch_array($result)) {
echo 
"<a href='news.php?nid={$list['id']}'><small>{$list['title']}</small></a><br />";
}

include(
"members/global/db-close.php");
?>


... other code ....

<?php
include("members/global/db-config.php");
include(
"members/global/db-open.php");

$sql "SELECT * FROM news ORDER BY id DESC LIMIT 0,1";
$result mysql_query($sql) or die(mysql_error());

while (
$list mysql_fetch_array($result)) {
$position 250//how many characters to display
$message $list['content'];
$post substr($message0$position);
echo 
"<small>$post</small>";
echo 
"<small>...<a href='news.php?nid={$list['id']}'>more</a></small>";
}

include(
"members/global/db-close.php");
?>


... other code ...


And here is the complete page code:

<?php require_once("forums/SSI.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>REMOVED</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="default.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header">
</div>
<div id="menu">
<ul>
<li class="active"><a href="index.php" accesskey="1" title="">Home</a></li>
<li><a href="forums/index.php" accesskey="2" title="">Forums</a></li>
<li><a href="raidninja/index.php" accesskey="3" title="">Guild Events</a></li>
<li><a href="roster.php" accesskey="4" title="">Roster</a></li>
<li><a href="media.php" accesskey="5" title="">Media</a></li>
</ul>
</div>
<div id="content">

<div id="colOne">
<h2>Navigation</h2>
<?php include("lib/nav2.php"); ?>

<h2>Members</h2>
<?php include("_inc/login.php"); ?>

<h2>Latest Guild Events</h2>
<?php include("_inc/events.php"); ?>

<h2>Recruitment Status</h2>
<?php include("_inc/status.php"); ?>
</div>

<div id="colFull">

<div id="boxes">
<div class="boxed">
<h2 class="title">latest member news</h2>
<div class="content" style="height: 100px;">
<table width="100%">
<tr>
<td style="text-align: left; width: 40%; vertical-align: top;">Latest Headlines</td>
<td style="text-align: right; width: 60%; vertical-align: top;">Latest Story</td>
</tr>
<tr>
<td style="text-align: left; width: 40%; vertical-align: top; padding-right: 10px;">

<?php
include("members/global/db-config.php");
include(
"members/global/db-open.php");

$sql "SELECT * FROM news ORDER BY id DESC LIMIT 0,5";
$result mysql_query($sql) or die(mysql_error());

while (
$list mysql_fetch_array($result)) {
echo 
"<a href='news.php?nid={$list['id']}'><small>{$list['title']}</small></a><br />";
}

include(
"members/global/db-close.php");
?>


</td>
<td style="text-align: justify; width: 60%; vertical-align: top;">

<?php
include("members/global/db-config.php");
include(
"members/global/db-open.php");

$sql "SELECT * FROM news ORDER BY id DESC LIMIT 0,1";
$result mysql_query($sql) or die(mysql_error());

while (
$list mysql_fetch_array($result)) {
$position 250//how many characters to display
$message $list['content'];
$post substr($message0$position);
echo 
"<small>$post</small>";
echo 
"<small>...<a href='news.php?nid={$list['id']}'>more</a></small>";
}

include(
"members/global/db-close.php");
?>


</td>
</tr>
</table>
</div>
<div id="rightmorePan"></div>
</div>
</div>

<?php
$array 
ssi_boardNews(3.05null0'array');
foreach (
$array as $news)
{
echo 
'
<div id="boxes"><div class="boxed"><h2 class="title">'
$news['icon'], ' '$news['title'], '</h2><div class="content">'$news['time'], ' '$txt[525], ' '$news['poster']['link'], '<br /><br />'$news['body'], '</div>
<div id="rightmorePan"><!-- <p class="textposition">'
$news['new_comment'], '</p> --><p class="link">'$news['more'], '</p></div>
</div>
</div>
<br />'
;
}
?>


</div>

<div style="clear: both;">&nbsp;</div>
</div>
<div>

<div id="footer">
<p align="center">Copyright &copy; 2007 REMOVED.</p>
</div>

</div>
</body>
</html>


Does anyone have any idea what the problem is here and how I would go about fixing it?

Thanks.
Take Care

Leipe Po

try replacing mysql_query() with db_query() and mysql_fetch_array with db_fetch_array...
There is only one thing more importend to me then coding:
My Girlfriend

Microsoft - "You've got questions.  We've got dancing paperclips."

eRott

#2
You know, it was very strange. I solved it by not opening and closing, then opening and closing the databases again. Like this:


<div id="boxes">
<div class="boxed">
<h2 class="title">latest member news</h2>
<div class="content" style="height: 100px;">
<table width="100%">
<tr>
<td style="text-align: left; width: 40%; vertical-align: top;">Latest Headlines</td>
<td style="text-align: right; width: 60%; vertical-align: top;">Latest Story</td>
</tr>
<tr>
<td style="text-align: left; width: 40%; vertical-align: top; padding-right: 10px;">

<?php
include("members/global/db-config.php");
include(
"members/global/db-open.php");

$sql "SELECT * FROM news ORDER BY id DESC LIMIT 0,5";
$result mysql_query($sql) or die(mysql_error());

while (
$list mysql_fetch_array($result)) {
echo 
"<a href='news.php?nid={$list['id']}'><small>{$list['title']}</small></a><br />";
}
?>


</td>
<td style="text-align: justify; width: 60%; vertical-align: top;">

<?php
$sql 
"SELECT * FROM news ORDER BY id DESC LIMIT 0,1";
$result mysql_query($sql) or die(mysql_error());

while (
$list mysql_fetch_array($result)) {

$news_position 250;
$news_message $list['content'];
$news_post substr($news_message0$news_position);

echo 
"<small>$news_post</small>";
echo 
"<small>...<a href='news.php?nid={$list['id']}'>more</a></small>";
}

include(
"members/global/db-close.php");
?>


</td>
</tr>
</table>
</div>
<div id="rightmorePan"></div>
</div>
</div>


Which really doesn't make sense. I mean, maybe I could understand if there was something wrong with my includes, but they look fine to me:

db-config.php

<?php
$dbhost 
'localhost';
$dbuser 'REMOVED';
$dbpass 'REMOVED';
$dbname 'REMOVED';
?>



db-open.php

<?php
$conn 
mysql_connect($dbhost$dbuser$dbpass) or die(mysql_error());
$db mysql_select_db($dbname$conn) or die(mysql_error());
?>




db-close.php

<?php
mysql_close
($conn);
?>


Dragooon

You really didn't opened a connection
in db-open.php
Use <?php
$conn 
mysql_connect($dbhost$dbuser$dbpass) or die(mysql_error());
$db mysql_select_db($dbname$conn) or die(mysql_error());
if(!
$conn || !$db)
die(
'Connection failure');
?>

eRott

If that does not open the connection, what does?

As for the code you suggested I should use, I do not follow the purpose of it. The only thing you added as far as I can tell, was some simple error checking, which I really am not too concerned about.

Basically, what you just added:


if(!$conn || !$db)
die('Connection failure');


Simply checks to see if the connection has been established, and if it hasn't, then return some error.

I will admit, I am slightly confused by your suggestion. If I am misunderstanding something, please feel free to explain.

Thank you for your feedback,
Best Regards.
Take care.

Dragooon

You just defined them in variables and never called them.
By using that "if" command you make then run.

Either use
<?php
mysql_connect
($dbhost$dbuser$dbpass) or die(mysql_error());
mysql_select_db($dbname$conn) or die(mysql_error());
?>

Or
<?php
$conn 
mysql_connect($dbhost$dbuser$dbpass) or die(mysql_error());
$db mysql_select_db($dbname$conn) or die(mysql_error());
if(!
$conn || !$db)
die(
'Connection failure');
?>

eRott

Good point, never thought of it that way, however, if that code does not open up a connection, then feel free to explain why code and webpage works as I intended it to. It would appear as though, while your code is, I suppose, a cleaner method of doing it, my code does work.

Dragooon

Hey bro, Don't blame me. I am just trying to help.

Thats how I do my coding.

eRott

To each their own. I am not trying to criticize your code by any means. I am just pointing out that I don't quite understand the use of that extra code as I have always understood it to be 'run' when you include it in the original file.

Dragooon

Does it work the way I said?

Because it seems quite obvious that by just including them in variables will not make them run.

eRott

After looking at your code a bit further, no, I do not think it would work. If you take a look at the second line (mysql_select_db), you are basically saying select the database name (defined from db-config.php) using the connection information (defined previously from the variable $conn). Since you took away that variable, $conn would no longer work, as it no longer exists, therefore causing that code not to work all-together. I think that is how it would work out is it not?


<?php
mysql_connect
($dbhost$dbuser$dbpass) or die(mysql_error());
mysql_select_db($dbname$conn) or die(mysql_error());
?>


Dragooon

Just realized something.
In
mysql_select_db($dbname, $conn);
It should be
mysql_select_db($dbname);
So The overall code can be either
<?php
global $dbhost$dbuser$dbpass$dbname;
mysql_connect($dbhost$dbuser$dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
?>

Or
<?php
global $dbhost$dbuser$dbpass$dbname;
$conn mysql_connect($dbhost$dbuser$dbpass) or die(mysql_error());
$db mysql_select_db($dbname) or die(mysql_error());
if(!
$conn || !$db)
die(
'Connection failure');
?>

I also added the globals as they weren't here.

Leipe Po

if you use SSI.php then the db connection is opened with smf....
There is only one thing more importend to me then coding:
My Girlfriend

Microsoft - "You've got questions.  We've got dancing paperclips."

Rudolf

Quote from: Dragooon on November 26, 2007, 05:33:33 AM
You just defined them in variables and never called them.
By using that "if" command you make then run.

Either use
<?php
mysql_connect
($dbhost$dbuser$dbpass) or die(mysql_error());
mysql_select_db($dbname$conn) or die(mysql_error());
?>

Or
<?php
$conn 
mysql_connect($dbhost$dbuser$dbpass) or die(mysql_error());
$db mysql_select_db($dbname$conn) or die(mysql_error());
if(!
$conn || !$db)
die(
'Connection failure');
?>


I know this is a week old topic, but there are some serious mistakes said here. Dragooon, you do some monumental mistakes there. If you are not a programmer then document yourself before saying something programming related.

if doesn't makes anything to run. The mysql_connect function actually creates AND opens a connection. It does. Really. Similarly the mysql_select_db really selects a database, if the database exists.
And thy don't "including them in variables", but return the result of their action. The first returns the mysql link indentifier created or false in case of failure, The second returns true or false.
As a side note mysql_select_db($dbname$conn); is perfectly valid. The second parameter is the connection and it's optional. Maybe that's why you haven't seen it before.
Also your if command is completely useless, because the condition will never evaluate to true. If either the connection or the database selection fails, the script dies before it reaches that point. If the script manages to get to the if, then it means that both of them were successful, therefore the condition will turn out to be false.

I'd suggest you take up the php documentation and read about the basic syntax of php, to understand variable handling, control structures, functions. Start with the basic stuff. You should also read about variable scoping. Using global where you did is "inappropriate". It's not an error, but it's not needed and not a good practice.
I will update all my mods in the next few weeks. Thanks for your patience.

SVG-Collapse (you need an SVG compliant browser)

Dragooon

I see :)
Thanks Rudolf :), I still am quite new to PHP/MySQL.

Rudolf

Quote from: Dragooon on December 09, 2007, 12:22:03 PM
I see :)
Thanks Rudolf :), I still am quite new to PHP/MySQL.
No problem. I meant it as positive criticism. Everyone has to start somewhere. The manual is a good place.

As for the problem, if you are still interested then there's a really simple explanation.
@eRott, you probably use the same host, username, password to connect to the database as SMF does.
IF you call the mysql_connect() function a second (or more times) with the same parameters it will not open a new  connection but will return the existing one.
So here's what actually happens.

1. You include the SSI.php file, which creates a connection to the database and saves that connection in the $db_connection variable.
2. You include your db-open.php which tries to make another connection but with the same information. PHP by default will not create the connection, instead it will return in your $conn variable the existing connection.
3. You call the db-close.php, which closes (tries to close) the $conn connection.
4. You include the second time the db-open.php and thus repeat the process again. This call doesn't really affect the outcome.
5. You close the second time the $conn. The problem is that in this moment both the $conn and $db_connection variables are destroyed. That's why the upcoming queries SMF is trying to do will fail.

You might ask, why not closing and reopening the database works. It is because of PHP's internal mechanism of memory handling and garbage collection. After your first db-open call the $conn and $db_connection  (SMF's variable) are simply two symbols pointing to the same value (same memory block). When the first time you call mysql_close($conn), PHP instead of freeing the memory it will simply store the dereference the $conn variable (in some internal stack, because the variable will still exists). When you call the second time, PHP will dereference the second variable too ($db_connection) and since there are no symbols pointing to the memory, it will finally release it (destroy it).
If you ask me this is an erroneous behavior, because PHP seems to ignore the fact that you issued two destroying calls on the same symbol. Since there are two symbols pointing to that memory and you are freeing it two times he considers it ready to be released.

Hope I'm not too technical or something.
I think that you just found a really nasty bug, still the major problem is not with the PHP engine but with your application. In this case the problem is a design issue.

The easiest solution for you would be to force php to create a new connection, by using $conn mysql_connect($dbhost$dbuser$dbpasstrue); in db-open.php.
However I would really suggest that you use SMF's connection and the db_query() function provided by SMF.
Your code would become:

<?php

$sql 
"SELECT * FROM news ORDER BY id DESC LIMIT 0,5";
$result db_query($sql__FILE____LINE__);

while (
$list mysql_fetch_array($result)) {
echo 
"<a href='news.php?nid={$list['id']}'><small>{$list['title']}</small></a><br />";
}
?>


</td>
<td style="text-align: justify; width: 60%; vertical-align: top;">

<?php
$sql 
"SELECT * FROM news ORDER BY id DESC LIMIT 0,1";
$result db_query($sql__FILE____LINE__);

while (
$list mysql_fetch_array($result)) {
$position 250//how many characters to display
$message $list['content'];
$post substr($message0$position);
echo 
"<small>$post</small> ";
echo 
"<small>...<a href='news.php?nid={$list['id']}'>more</a></small>";
}
?>



Note that including SSI.php doesn't necessarily selects the database. If you get errors saying that the tables don't exist you have to select it. In which case you simply add before your first query:
mysql_select_db('yourdatabase');

Hope it helps you fixing the problem (instead of hiding it).
I will update all my mods in the next few weeks. Thanks for your patience.

SVG-Collapse (you need an SVG compliant browser)

Advertisement: