Database loading problems - php

I have an issue with the loading of this website - http://decoded.ro/l2tales. The problem is caused by these 2 PHP scripts:
<?php
mysql_connect('example.com','XXXXXXX', 'XXXXXXXX') or die(mysql_error());
mysql_select_db('dbname');
$query = mysql_query("SELECT * FROM `smf_topics` WHERE `id_board` = '2' ORDER BY `id_topic` DESC LIMIT 5");
//echo '<span style="color: white;">DARK' . $query . '</span>';
while($row = mysql_fetch_assoc($query))
{
$id_topic = $row['id_topic'];
$query2 = mysql_query("SELECT * FROM `smf_messages` WHERE `id_topic` = $id_topic");
$row2 = mysql_fetch_assoc($query2);
echo '<div class="page-title">';
echo '<h1>' . htmlspecialchars_decode(substr($row2['subject'], 0 ,200)) . '</h1>';
echo '<p style="color: #eed883;">';
echo 'Author: ' . $row2['poster_name'];
echo '</p>';
echo '<p>';
echo $row2['body'];
echo '</p>';
echo '</div>';
}
?>
<?php
mysql_connect('example.com','XXXXXXX', 'XXXXXXXX') or die(mysql_error());
mysql_select_db('dbname');
$query = mysql_query("SELECT * FROM `smf_topics` WHERE `id_board` = '4' ORDER BY `id_topic` DESC LIMIT 5");
//echo '<span style="color: white;">DARK' . $query . '</span>';
while($row = mysql_fetch_assoc($query))
{
$id_topic = $row['id_topic'];
$query2 = mysql_query("SELECT * FROM `smf_messages` WHERE `id_topic` = $id_topic");
$row2 = mysql_fetch_assoc($query2);
$count = strlen($row2['subject']);
if($count > '59')
$dots = '...';
else
$dots = '';
echo '<li>';
echo '<a href="http://forum.l2tales.com//index.php?topic=' . $row['id_topic'] . '/" title="' . htmlspecialchars_decode(substr($row2['subject'], 0 ,75)) . '" target="_blank">';
echo '<span>' . htmlspecialchars_decode(substr($row2['subject'], 0 ,59)) . $dots . '</span>';
echo '<p>';
echo 'Author: ' . $row2['poster_name'];
echo '</p>';
echo '</a>';
echo '</li>';
}
?>
These PHP scrips are fetching from a SMF database on hosted on server B (will explain later) latest 5 topics.
So i have 2 servers. On server A (which is decoded.ro/l2tales) it works fine, is loading is almost instant for me. On server B, where the forum is hosted, the website loads in years.
Here you can see a adress for the website hosted on server B - http://l2tales.com/dark (At first you will see a blank page but the website is still loading, just wait until it loads completly).
Why on the same server with the forum is so slow but on a different server is instant?
Thanks

Related

MySQL select where $id

I have a website with this code snippet:
<?php
mysql_connect('localhost','root','password');
mysql_select_db('news');
$id_article = $_GET['newsid'];
$query = mysql_query('SELECT * FROM news WHERE id="$id_article"');
{
echo '<div class="item"><h1>'.$query['subject'].'</h1><br />';
echo $query['full_content'].'<br / >';
echo date('D-M-Y', $query['date']).'<br / >';
echo 'Posted by '.$id_article;
echo '</div>';
}
?>
The $id_article gets the id from an previous request. The $id_article works, but the $query doesn't. $query['***'] still blank space. I don't know why. Please help me! Thanks a lot!
You don't get your query result. Use like below:
<?php
mysql_connect('localhost','root','password');
mysql_select_db('news');
$id_article = intval($_GET['newsid']);
$query = mysql_query('SELECT * FROM news WHERE id=' . $id_article);
if (mysql_num_rows($query) > 0)
{
$row = mysql_fetch_assoc($query);
echo '<div class="item"><h1>'.$row['subject'].'</h1><br />';
echo $row['full_content'].'<br / >';
echo date('D-M-Y', $row['date']).'<br / >';
echo 'Posted by '.$id_article;
echo '</div>';
}
?>
Tip: use PDO or mysqli instead of mysql_ functions! Mysql_ functions are deprecated / not supported in new php versions!
Further, sanatize your input before passing to your query!
Use like this
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
echo $row['id']. " - ". $row['date'];
You have not used mysql_fetch_assoc. Use as below :
$query = mysql_query('SELECT * FROM news WHERE id=' . $id_article);
$row = mysql_fetch_assoc($query);
if(count($row)>0)
{
echo '<div class="item"><h1>'.$row['subject'].'</h1><br />';
echo $row['full_content'].'<br / >';
echo date('D-M-Y', $row['date']).'<br / >';
echo 'Posted by '.$id_article;
echo '</div>';
}
change your this code
$query = mysql_query('SELECT * FROM news WHERE id="$id_article"');
{
echo '<div class="item"><h1>'.$query['subject'].'</h1><br />';
echo $query['full_content'].'<br / >';
echo date('D-M-Y', $query['date']).'<br / >';
echo 'Posted by '.$id_article;
echo '</div>';
}
to
$sql = "SELECT * FROM news WHERE id = $id_article";
$query = mysql_query($sql);
while($result = mysql_fetch_assoc($query));
{
echo '<div class="item"><h1>'.$result['subject'].'</h1><br />';
echo $result['full_content'].'<br / >';
echo date('D-M-Y', $result['date']).'<br / >';
echo 'Posted by '.$id_article;
echo '</div>';
}
There are a lot of errors in your code
mysql_query has been depreciated
Use the following code
and ckeck for the errors
<?php
mysql_connect('localhost', 'root', 'password');
$conn = mysql_select_db('news');
$id_article = $_GET['newsid'];
if($query = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM news WHERE id=$id_article"))){
echo '<div class="item"><h1>' . $query['subject'] . '</h1><br />';
echo $query['full_content'] . '<br / >';
echo date('D-M-Y', $query['date']) . '<br / >';
echo 'Posted by ' . $id_article;
echo '</div>';
}
?>
Try This..
$id_article = $_GET['newsid'];
$query = "SELECT * FROM news WHERE id='$id_article'";
$query1=mysql_query($query);
You should call a function to get the row returned by the query. You are trying to access the $query instead of the row. Your code should look like this:
<?php
mysql_connect('localhost','root','password');
mysql_select_db('news');
$id_article = $_GET['newsid'];
$query = mysql_query('SELECT * FROM news WHERE id="$id_article"');
if ($query) {
$row = mysql_fetch_assoc($query));
echo '<div class="item"><h1>'. $row['subject'].'</h1><br />';
echo $row['full_content'].'<br / >';
echo date('D-M-Y', $row['date']).'<br / >';
echo 'Posted by '.$id_article;
echo '</div>';
} else {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
?>
You can also take a look at the mysql_result(), mysql_fetch_array(), mysql_fetch_row() functions.

Showing six latest PhpBB posts instead of six copies of latest post

Trying to display six of the latest posts from my PhpBB installation. I'm happy with how it's all working, however it's showing six copies of the same (most recent) post, and not size unique latest posts.
Just to confirm, I have seven total posts on the forums.
<?php
$con = mysqli_connect("localhost", "dbuser", "dbpass", "dbname");
$users = mysqli_query($con, "SELECT * FROM phpbb_user_group WHERE group_id='8'");
while($row = mysqli_fetch_array($users)) {
$developers[] = $row["user_id"];
}
$post = mysqli_query($con, "SELECT * FROM phpbb_posts");
while($row = mysqli_fetch_array($post)) {
$topic_id = $row["topic_id"];
$forum_id = $row["forum_id"];
$post_id = $row["post_id"];
$post_text = $row["post_text"];
$post_time = $row["post_time"];
}
$username = mysqli_query($con, "SELECT * FROM phpbb_users WHERE user_id='2'");
while($row = mysqli_fetch_array($username)) {
$postauthor = $row["username"];
if (strlen($post_text) > 10)
$post_text = wordwrap($post_text, 120);
$post_text = explode("\n", $post_text);
$post_text = $post_text[0] . '...';
$result = mysqli_query($con, "SELECT * FROM phpbb_posts WHERE poster_id='2' LIMIT 6");
while($row = mysqli_fetch_array($result)) {
$content = '<div onclick="location.href=\'http://test.mythros.net/forum/viewtopic.php?f=' . $forum_id . '&p=' . $topic_id . '#p' . $post_id . '\';" class="forum-latest-box">';
$content .= '<div class="forum-latest-userbar">';
$content .= '<div class="forum-latest-avatar">';
$content .= '<img src="https://minotar.net/helm/' . $postauthor . '/40.png">';
$content .= '</div>';
$content .= '<h1>' . $postauthor . '</h1>';
$content .= '</div>';
$content .= '<div class="forum-latest-content">';
$content .= '<div class="forum-latest-text">';
$content .= '"' . $post_text . '"';
$content .= '</div>';
$content .= '<div class="forum-latest-meta">';
$content .= gmdate("F j, Y, g:i a", $post_time);
$content .= '</div>';
$content .= '</div>';
$content .= '</div>';
echo $content;
}
?>
You can solve this problem by using a single loop and getting your post data and user information at the same time using combined query to the phpbb_posts table and phpbb_users table:
## Line break added to the query for legibility
$result = mysqli_query($con,
"SELECT
p.post_id AS post_id,
p.topic_id AS topic_id,
p.forum_id AS forum_id,
p.post_time AS post_time,
p.post_subject AS subject,
p.post_text AS post_text
IFNULL(m.username, 'Guest') AS username,
FROM phpbb_posts AS p
LEFT JOIN phpbb_users AS m ON (m.user_id = p.poster_id)
ORDER BY phpbb_posts.post_time DESC LIMIT 6");
while($row = mysqli_fetch_array($result)) {
# $row now contains the post information and the user info, so you can grab all your data,
# process the post text, and print it out at the same time.
$post_text = $row["post_text"];
# do your text transformation
if (strlen($post_text) > 10)
... (etc.)
# now set up your content
$content = '<div onclick="location.href=\'http://test.mythros.net/forum/viewtopic.php?f=' . $row["forum_id"] .'&p=' . $row["topic_id"] . '#p' . $row["post_id"] . '\';" class="forum-latest-box">';
... (etc.)
}

PHP: CMS Problems

Hey guys im writting a CMS but im having problems and need a fresh pair of eyes at this. I am getting this error: Parse error: syntax error, unexpected '}' on the line just above the last else statement but there is no reason for it.
The idea is to have display a substr of the articles and then click them to get the full article. Can someone have a look at my code and tell me where im going wrong or if this will even work.
class MyCMS
{
function get_content($id = "", $excerpt = FALSE)
{
if ($id != ""):
$id = mysql_real_escape_string($id);
$sql = "SELECT * FROM content WHERE blog_id = '$id'";
$return = '<p> Go Back To Content Page</p>';
else:
$sql = "SELECT blog_id, title, date, body FROM content ORDER BY blog_id DESC LIMIT 0, 3";
endif;
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) !=0):
while($row = mysql_fetch_assoc($res))
{
echo '<div id="roundedbox"><h2>' . $row['title'] . ' </h2>';
echo '<div id="date"><h5><p>' . $row['date'] . '</p></h5></div>';
if ($excerpt):
echo substr('<p>' . $row['body'] . '</p>',0, 90)." .... "." read more </div>";
else:
echo '<p>' . $row['body'] . '</p>';
}
else:
echo '<p> UH OOH! THERE IS NO SUCH PAGE IT DOES\'T EXIST </p>';
echo $return;
endif;
}
}
Then in my main page i am calling the code as follows:
<?php
include 'cms.php';
$obj = new MyCMS();
?>
<?php
if(isset($_GET['id'])):
echo $obj->get_content($_GET['id'], TRUE);
else:
echo $obj->get_content($_GET['id']);
endif;
?
You forgot to add endif(for the if statement inside loop) and } (for closing class). Please check this code.
class MyCMS
{
function get_content($id = "", $excerpt = FALSE)
{
if ($id != ""):
$id = mysql_real_escape_string($id);
$sql = "SELECT * FROM content WHERE blog_id = '$id'";
$return = '<p> Go Back To Content Page</p>';
else:
$sql = "SELECT blog_id, title, date, body FROM content ORDER BY blog_id DESC LIMIT 0, 3";
endif;
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) !=0):
while($row = mysql_fetch_assoc($res))
{
echo '<div id="roundedbox"><h2>' . $row['title'] . ' </h2>';
echo '<div id="date"><h5><p>' . $row['date'] . '</p></h5></div>';
if ($excerpt):
echo substr('<p>' . $row['body'] . '</p>',0, 90)." .... "." read more </div>";
else:
echo '<p>' . $row['body'] . '</p>';
endif;
}
else:
echo '<p> UH OOH! THERE IS NO SUCH PAGE IT DOES\'T EXIST </p>';
echo $return;
endif;
}
}
Also please notice that second code logic is absolutely wrong..
<?php
include 'cms.php';
$obj = new MyCMS();
?>
<?php
if(isset($_GET['id'])):
echo $obj->get_content($_GET['id'], TRUE);
else:
// echo $obj->get_content($_GET['id']); // This line is executing when $_GET['id'] is undefined.So dont use $_['id'] here.
endif;
?>

How to just show few lines from a whole blog-post on a certain page?

I am making a blog in which I want to show just the summary of all posts on the home page rather than whole posts.
Below is the function that I use to get the posts on my home page.
function get_content($id = '')
{
if ($id != ""):
$id = mysql_real_escape_string($id);
$sql = "SELECT * from cms_content WHERE id = '$id'";
$return = '<p>Go back to Home page</p>';
echo $return;
else:
$sql = "select * from cms_content ORDER BY id DESC";
endif;
$res = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($res) != 0):
while ($row = mysql_fetch_assoc($res)) {
echo '<h1>' . $row['title'] . '</h1>';
echo '<p>' . "by: " . $row['author'] . ", posted on: " . $row['date'] . '<p>';
echo '<p>' . $row['body'] . '</p><br />';
}
else:
echo '<p>We are really very sorry, this page does not exist!</p>';
endif;
}
And then simply I use this php code to get the posts:
<?php
if (isset($_GET['id'])) :
$obj->get_content($_GET['id']);
else :
$obj->get_content();
endif;
?>
So how to get the summary only using php function or mysql as I know that we can use mysql query to get limit the words?
function sumarize($your_string, $limit_lines){
$count = 0;
foreach(explode("\n", $your_string) as $line){
$count++;
echo $line."\n";
if ($count == $limit_lines) break;
}
}
sumarize($row['body'], 10);
will show first ten lines
1-lined:
echo '<p>'.implode("\n",array_slice(explode("\n",$row['body']),0,10)).'</p>';
instead of line with $row['body']

How to set permalink of your blog post according to date and title of post?

I am having this website http://www.finalyearondesk.com . My blogs post link are set like this.. http://www.finalyearondesk.com/index.php?id=28 . I want it to set like this ... finalyearondesk.com/2011/09/22/how-to-recover-ubuntu-after-it-is-crashed/ .
I am using the following function to get these posts...
function get_content($id = '') {
if($id != ""):
$id = mysql_real_escape_string($id);
$sql = "SELECT * from cms_content WHERE id = '$id'";
$return = '<p>Go back to Home page</p>';
echo $return;
else:
$sql = "select * from cms_content ORDER BY id DESC";
endif;
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) != 0):
while($row = mysql_fetch_assoc($res)) {
echo '<h1>' . $row['title'] . '</h1>';
echo '<p>' . "By: " . '<font color="orange">' . $row['author'] . '</font>' . ", Posted on: " . $row['date'] . '<p>';
echo '<p>' . $row['body'] . '</p><br />';
}
else:
echo '<p>We are really very sorry, this page does not exist!</p>';
endif;
}
And I am using this code to dispaly it on my index.php page...
<?php
if (isset($_GET['id'])) :
$obj -> get_content($_GET['id']);
else :
$obj -> get_content_summary();
endif;
?>
Any suggestions how to do this? And can we do this by using .htaccess?
The unfortunate thing about using mod_rewrite is that the data you are supplying in the form of a url is not the best way to query a database. But none the less you have year, month, day and title variables so you will need to rewrite your get_content function to query soomething like (depending on how you date is stored in the database.):
select * from cms_content
WHERE date='mktime(0,0,0,$month,$day,$year)'
AND title='$title'
ORDER BY id DESC
.htaccess would be something like:
RewriteRule ^(.*)/(.*)/(.*)/(.*)$ index.php?year=$1&month=$2&day=$3&title=$4

Categories