...

View Full Version : SQL like statements



moos3
08-17-2007, 11:59 AM
I'm getting the following error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/richard/labs/blog.php on line 28

I has something to do with my sql statements


$id = $_GET['id'];
$tag = $_GET['tag'];
if(empty($id) && empty($tag)){
$sql = 'SELECT `b_id`,`title`,`body`,`author`,`tags`, UNIX_TIMESTAMP(`date`) as blogdate FROM `blog`';
}
elseif(!empty($tag)){
$sql = "SELECT `b_id`,`title`,`body`,`author`,`tags`, UNIX_TIMESTAMP(`date`) as blogdate FROM blog WHERE tags LIKE '% $tag %'";
}
else{
$sql = "SELECT `b_id`,`title`,`body`,`author`,`tags`, UNIX_TIMESTAMP(`date`) as blogdate FROM `blog` WHERE id='$id' ORDER BY blogdate DESC";
}
$results = mysql_query($sql);
while($rows = mysql_fetch_array($results)){
$title = $rows['title'];
$body = $rows['body'];
$author = $rows['author'];
$b_date = date("M d Y",$rows['blogdate']);
$tags = explode(" ",$rows['tags']);
echo '<div class="blog">';
echo '<h1>'.$title.' <span class="blogDate">'.$b_date.'</span></h1>';
echo '<p>'.$body.'</p>';
echo '<div id="author">'.$author.'</div> filed under: ';
foreach($tags as $tag){
echo '<a href="?tag='.$tag.'">'.$tag.'</a>, ';
}
echo '</div>';
}

StupidRalph
08-17-2007, 01:12 PM
You should add some error checking to find out why your query is not being executed.

$results = mysql_query($sql) or die(mysql_error() . "SQL statement: " . $sql);
You have an if statement with conditions, under which condition is it not being executed?

Also, I think your wildcards need to be on your tag variable %$tag% unless you're looking for 'anything here, space, variable value, space, anything here'.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum