Help - Search - Members - Calendar
Full Version: Whats wrong with this code?
Weborum Webmaster Forum > Web Page Design > PHP
Joe
I keep getting the following error in my PHP page;

QUOTE
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in c:\program files\easyphp\www\web design\cms\viewtutorial.php on line 18


It works for all my other scripts except for this one. Here's my PHP;

PHP
<?php
session_start
();

if(&
#33;isset($_SESSION['loggedin'])) {
   
header('Location: '.$domain.'index.php?error=1');
   exit();
}

include
'config.php';

$ID = $_GET['view'];

$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);
$requete = "SELECT T.ID, metakeywords, metadescription, tutorialtitle, subjectID, subject, postedby, datetime, tutcontent FROM tutorial T INNER JOIN

subject S ON S.ID = T.subjectID WHERE ID=$ID"
;
$result = mysql_query ($requete,$db);

while(
$row = mysql_fetch_assoc($result)) {

$ID = $row['ID'];
$metakeywords = $row['metakeywords'];
$metadescription = $row['metadescription'];
$tutorialtitle = $row['tutorialtitle'];
$subjectID = $row['subjectID'];
$subject = $row['subject'];
$postedby = $row['postedby'];
$datetime = date("n.j.y / g:ia", $row['datetime']);
$tutcontent = $row['tutcontent'];

$metakeywords = stripslashes($row['metakeywords']);
$metadescription = stripslashes($row['metadescription']);
$tutorialtitle = stripslashes($row['tutorialtitle']);
$subject = stripslashes($row['subject']);
$postedby = stripslashes($row['postedby']);

$tutcontent = str_replace("\n", '', $tutcontent);
$tutcontent = stripslashes($tutcontent);
$tutcontent = str_replace("'", "\'", $tutcontent);

$tutcontent = preg_replace("/\[php\](.+?)\[\/php\]/", "'); highlight_php('$1'); echo('", $tutcontent);
$tutcontent = preg_replace("/\[html\](.+?)\[\/html\]/", "'); htmlchar('$1'); echo('", $tutcontent);
$tutcontent = 'echo(\'' . $tutcontent . '&#092;');';

echo '<div class="indexpagecomment">';
echo
'<div class="indexpagecommentheader"><a href="viewtutorial.php?view='.$ID.'">'.$tutorialtitle.'</a><p>Category: <a

href="subjectlist.php?='
.$subjectID.'">'.$subject.'</a></p><p>Created by '.$postedby.' on '.$datetime.' <acronym title="Greenwich Mean

Time">GMT</acronyn></p></div>'
;

echo
'<div style="padding: 5px;">';
eval(
$tutcontent);
echo
'</div></div>';
}
?>



And as Willy mentioned before it is quite difficult to solve server side problems without seeing the tables here they are also;

SQL
CREATE TABLE `subject` (
`ID` int(11) NOT NULL auto_increment,
`subject` varchar(255) default NULL,
KEY `ID` (`ID`)
);


SQL
CREATE TABLE `tutorial` (
`ID` int(11) NOT NULL auto_increment,
`metakeywords` text,
`metadescription` text,
`tutorialtitle` varchar(255) default NULL,
`subjectID` int(5) default NULL,
`tutcontent` text,
`postedby` varchar(60) default NULL,
`datetime` int(10) unsigned default NULL,
KEY `ID` (`ID`)
);


Can anyone see where I'm going wrong with this code?

TIA
Timo
Hmm

subject S ON S.ID = T.subjectID WHERE ID=$ID";

shouldn't that be...

subject S ON S.ID = T.subjectID WHERE ID='$ID'";

If it's something else (like the query's structure), you better ask Bassrek in the databasing forum... I'm not very knowledgable with inner joins. ;D

:-D

-Tim
Joe
Nope, ID = $ID and ID = '$ID' give the same results.


When I'm grabbing all the data out from the database Tim, how would I do it so I wouldn't need to put it into a while statement?

I know usually I would do SELECT one, two WHERE one = '$one' AND two = '$two' but that's not possible for the amount of fields I'm using is it?
Joe
I found the problem, and it was quite simple laugh.gif

My query was the problem ...

I had this;

SQL
SELECT T.ID, metakeywords, metadescription, tutorialtitle, subjectID, subject, postedby, datetime, tutcontent FROM tutorial T INNER JOIN subject S ON S.ID = T.subjectID WHERE ID='$ID


And I needed this;

SQL
SELECT T.ID, metakeywords, metadescription, tutorialtitle, subjectID, subject, postedby, datetime, tutcontent FROM tutorial T INNER JOIN subject S ON S.ID = T.subjectID WHERE T.ID='$ID


rolleyes.gif 2 letter error smile.gif
Timo
haha I'll have to learn inner joins eventually but I'm content with all the bs they make us learn for some of my classes here... ;D Good job on solving it though.

-Tim biggrin.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.