MySQL_fetch_object error on Wiki.

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
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

MySQL_fetch_object error on Wiki.

Post by ThatPerson »

I have recently been learning C, and I have decided to release it, and the source code. However, I am trying to make a website to house it, and a wiki. I am making the Wiki myself and I am also making the Wiki opensource. Unfortunately, the Wiki has an issue. In the Database there is an entry like:

[text]ID: 1, Name: Intro, Desc: The Homepage, Text: Welcome[/text]

However in the code:

Code: Select all

<?php
include("database.php");
ob_start();
$pages = mysql_fetch_object(mysql_query("select count(ID) as count from swarm_wiki")) -> count;
echo <<<END
	<head>
		<title>Crimson Swarm</title>
	</head>
END;
echo <<<end
	<div style = "overflow:scroll;height:100%;width:200px;">
end;
$p = 0;
for ($i = 1; $i <= $pages; $i++) {
	$p = $p+1;
	${note.$p."title"} = mysql_fetch_object(mysql_query("select Name from swarm_wiki where ID = '$p'")) ->Name;
	${note.$p."desc"} = mysql_fetch_object(mysql_query("select Desc from swarm_wiki where ID = '$p'")) ->Desc;
	${note.$p."text"} = mysql_fetch_object(mysql_query("select Text from swarm_wiki where ID = '$p'")) ->Text;
	$title = ${note.$p."title"};
	$desc = ${note.$p."desc"};
	$text = ${note.$p."text"};
	$pos = 50*($p-1);
	$submit = "Submit".$p;
echo <<<END
	<div style = "height:50px;width:100%;position:inherit;left:0px;top:$pos;background-color:#3399FF;">
		<INPUT style = "position:inherit;height:50%;width:100%;background-color:#3399FF;top:0px;left:0px;" value = "$title" name = "$submit">
		<div style = "position:inherit;height:50%;left:0px;top:50%;width:100%;">
			<p>$desc</p>
		</div>
	</div>
END;
}
echo "</div>";

$p = 0;
for ($i = 1; $i <= $pages; $i++) {
	$p = $p+1;
	$submit = "Submit".$p;
	if (isset($_POST[$submit])) {
		setcookie("page",$p,time()+60*60);
echo <<<end
	<head>
		<meta HTTP-EQUIV="REFRESH" content="0; url='index.php'">
	</head>
end;
}
}

echo "<div style = 'height:100%;left:200px;right:0px;'>";
echo "<h1 style='text-align:center;'>Crimson Wiki</h1>";
echo <<<END
<div style="overflow:scroll;top:150px;left:0px;bottom:0px;position:inherit;">
END;
if (!$_COOKIE['page']) {
$text = $note1text;
$title = $note1title;
$desc = $note1desc;
echo <<<END
<div style="height:150px;width:100%;position:inherit;top:0px;left:0px;">
<h2>$title</h2>
</div>
<div style="height:100px;width:100%;position:inherit;top:150px;left:0px;">
<i>$desc</i>
</div>
<div style="top:150px;left:0px;width:100%;position:inherit;">
<p>$text</p>
</div>
END;
} else {
$p = $_COOKIE['page'];
$text = ${note.$p."text"};
$title = ${note.$p."title"};
$desc = ${note.$p."desc"};
echo <<<END
<div style="height:150px;width:100%;position:inherit;top:0px;left:0px;">
<h2>$title</h2>
</div>
<div style="height:100px;width:100%;position:inherit;top:150px;left:0px;">
<i>$desc</i>
</div>
<div style="top:150px;left:0px;width:100%;position:inherit;">
<p>$text</p>
</div>
END;
}
echo "</div>";
?>
It errors with:

[text]Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/bentatma/public_html/swarm/index.php on line 4[/text]

Which normally means there is nothing going to the fetch object on this:

Code: Select all

$pages = mysql_fetch_object(mysql_query("select count(ID) as count from swarm_wiki")) -> count;
However it should return 1.
User avatar
kasuals
Forum Commoner
Posts: 28
Joined: Thu Aug 11, 2011 12:59 pm

Re: MySQL_fetch_object error on Wiki.

Post by kasuals »

Have you tried to break up the statement a bit to troubleshoot it?

Code: Select all

if($res = mysql_query("select count(ID) as count from swarm_wiki")) {
    $pages = mysql_fetch_object($res);
    print_r($pages);
} else {
    ...
}
Based on the warning, there is no resource available to pull from. Which means the resulting query isn't being returned properly. If the statement works from the command line client then I'd assume the issue was somewhere in the code inside database.php.

I'm no MySQL statement guru, so I can't help with that.
Dorin85
Forum Newbie
Posts: 20
Joined: Tue Aug 16, 2011 3:16 pm

Re: MySQL_fetch_object error on Wiki.

Post by Dorin85 »

"select count(ID) as count from swarm_wiki"

I'm not the best with SQL queries but I believe "as count" is invalid... "count" is a reserved word. Try "select count(ID) as WTFcount from swarm_wiki"
Post Reply