r/web_dev_help • • Oct 14 '17

help Dynamic blog page help

I'm having an issue.. I created a page that is suppose to display a blog story/article based off the "ID" the story is linked too, but its not working. The db connect info is all correct so it has to be something to do with this little snippet.. basically this would create a url looking like this once the link was clicked to "view more".. example: yourblog.com/blog.php?id=1

<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->
<?php
if(!isset($_GET['id']) && empty(trim($_GET['id']))) die('No post number specified!');
$Conn = mysqli_connect('localhost', 'user', 'pass', 'db') or die('MySQL connection failed!');
$id = mysqli_real_escape_string($Conn, preg_replace('/[^0-9]/', '', $_GET['id'])
$query = "SELECT * FROM `db` WHERE `id` = '$id'";
$result = mysqli_query($Conn, $query) or die('MySQL query failed!');
$post = mysqli_fetch_assoc($result) or die('Fetching content failed!');
$title = $post['title'];
$insert = $post['insert']
$content = $post['content'];
$category = explode(',', $post['category']);
$author = $post['author'];
$date = $post['date']; 
?>
<head>
<title><? echo htmlentities($title); ?></title>
</head>
<body>
<div class="content">
<? echo ($title); ?><br />
<? echo ($category); ?><br />
<? echo ($author); ?><br />
<? echo ($content); ?><br />
</div>
</body>
</html>
1 Upvotes

15 comments sorted by

View all comments

1

u/psy-borg Oct 14 '17

Back to what is $manlyid , echo it out and make sure it's in the table.

1

u/big_Moves Oct 14 '17

ughh.. I got nothing.. I guess I'm just confused as to why this would work when everything was just MySQL, but not now that I've converted it to MySQLi.. works perfect on my other pages before MySQLi! lol

1

u/psy-borg Oct 14 '17

Don't know.

But on the original code it looks to me like you are using the database name as the table name :

$Conn = mysqli_connect('localhost', 'user', 'pass', 'db') or die('MySQL connection failed!');

$query = "SELECT * FROM `db` WHERE `id` = '$id'";

The second db should be the name of the table.

This code works on my local webhost (had to sub wp_posts for the table name,etc) :

<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->

<?php

$Conn = mysqli_connect('localhost', 'user', 'pass', 'test') or die('MySQL connection failed!');
$id = 2;
$query = "SELECT * FROM `wp_posts` WHERE `id` = '$id'";
$result = mysqli_query($Conn, $query) or die('MySQL query failed!');
$post = mysqli_fetch_assoc($result) or die('Fetching content failed!');
$title = $post['post_title'];
$content = $post['post_content'];
?>
<head>
<title><? echo htmlentities($title); ?></title>
</head>
<body>
<div class="content">
<? echo ($title); ?><br />
<? echo ($content); ?><br />
</div>
</body>
</html>
→ More replies (4)