Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\test\blog\resources\func\blog.php on line 43
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\test\blog\index.php on line 25
blog:
PHP Code:
<?php
function add_post($title, $contents, $category) {
$title = mysql_real_escape_string($title);
$contents = mysql_real_escape_string($contents);
$category = (int)$category;
mysql_query("INSERT INTO 'post' SET
'cat_id' = {$category},
'title' = '{$title}',
'contents' = '{$contents}',
'date_posted' = NOW()")
or die(mysql_error());
}
function edit_post($id, $title, $contents, $category) {
}
function add_category($name) {
$name = mysql_real_escape_string($name);
mysql_query("INSERT INTO 'categories' SET 'name' = '{$name}' ");
}
function delete($table, $id) {
$table = mysql_real_escape_string($table);
$id = (int) $id;
mysql_query("DELETE FROM '{$table}' WHERE 'id' = {$id}");
}
function get_posts($id = null, $cat_id = null) {
$post = array();
$query = "SELECT 'posts' . 'id' AS 'post_id', 'categories . 'id' AS 'category_id',
'title', 'contents', 'date_posted', 'categories' . 'name'
FROM 'post'
INNER JOIN 'categories' ON 'categories' . 'id' = 'post' . 'cat_id'
ORDER BY 'id' DESC";
$query = mysql_query($query);
while ( $row = mysql_fetch_assoc($query) ) {
$posts[] = $row;
}
return $posts;
}
function get_categories($id = null) {
$categories = array();
$query = mysql_query("SELECT 'id', 'name' FROM 'categories' ");
while ( $row = mysql_fetch_assoc($query) ) {
$categories[] = $row;
}
return $categories;
}
function category_exists($field, $value) {
$field = mysql_real_escape_string($field);
$value = mysql_real_ecape_string($value);
$query = mysql_query("SELECT COUNT(1) FROM 'categories' WHERE '{$field}' = '{$value}' ");
return ( mysql_result($query, 0) == '0' ) ? false : true;
}
?>
index:
PHP Code:
<?php
include_once('resources/init.php');
$posts = get_posts();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
ul { list-style-type: none; }
li { display: inline; margin-right: 20px; }
</style>
<title> My Blog </title>
<?php
foreach ( $posts as $post) {
}
?>
</head>
<body>
<nav>
<ul>
<li><a href="index.php"> Index </a></li>
<li><a href="add_post.php"> Add a Post </a></li>
<li><a href="add_category.php"> Add a Category </a></li>
<li><a href="category_list.php"> Category List </a></li>
</ul>
</nav>
<h1> Devin's Awesome Blog </h1>
</body>
</html>
Bookmarks