-2

Hey I got some thinking problem with this code here bear with me I'm new to php i use this script and want the comments that i use atm mysq_num_rows just to see if a can get the relevant data out. But I don't really have a clue to get it out so the relevant comment number is in the right ID like blogg id 700 for example i want it to show 34 comments and so on to the next $data of blog. now i just get 0 0 0 0 34 comments on each blogg post showing.

Any tips or hints how I would proccide woud be great I have googled everything i could think of but my lack of experience in MYSQL, PHP is what holding me back i think.

COMMENTS: In comments i got ID and i got BID and the BID is where the blogg ID is fetched.

BLOG:Fetching the ID from it to the function test()

Here is some of my code

function test() 
{ 


$resultblogs = mysql_query("SELECT * FROM blog"); 
while($rowblogs = mysql_fetch_array( $resultblogs )) {
$blogid = $rowblogs['id'];

$blog_comment=mysql_query("Select * from comments WHERE bid='$blogid' AND verify='1' ");
$blog_comment_count=mysql_num_rows($blog_comment);
echo $blog_comment_count;

}                   
} 

Here is the rest of the code for you to inspect

<?php if (!isset($_GET["bid"])) { ?>
<!-- Blog Post -->

<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);

$mysql_host = "localhost";
$mysql_database = "database";
$mysql_user = "root";
$mysql_password = "****";

$link = mysql_connect("$mysql_host", "$mysql_user", "$mysql_password") or die("Database is down, Please try again.");
mysql_select_db($mysql_database) or die("database not found!");

/*
set the pagination settings
$sql - Your sql query
$num_results_per_page - Number of results per page
$num_page_links_per_page - Number of visible page links per page
*/
$sql = "SELECT * FROM blog ORDER BY date DESC";
$num_results_per_page = 3;
$num_page_links_per_page = 5;
$pg_param = "#news"; // Ex: &q=value
pagination($sql, $num_results_per_page, $num_page_links_per_page, $pg_param);
/*
Pass 4 values to the pagination function.
No return values,
instead You can use 3 global variables set by the function.

$pg_result - mysql result id variable
$pagination_output - variable containing html pagination output
$pg_error - display any errors
*/
?>
<?php


if($pg_error == '')
{
if(mysql_num_rows($pg_result) > 0)
{
    while($data = mysql_fetch_assoc($pg_result))
    {
    ?>

    <div class="type-date">                    
    <div class="blog-type"><img src="images/blog-text.png" alt=""></div>
    <div class="blog-date"><h5><?php echo $data["day"]?></h5><h5><?php echo       $data["month"]?></h5></div>                    
    </div>
    <h2><a  href="?bid=<?php echo $data["id"].'#news'?>"><?php echo $data["title"] ?></a></h2>
    <?php

echo test();

$blogcontent = substr
($data["body"],0,600).'....<div style="margin-top:20px"><br><a class="newave-button medium color" href="?bid='.$data["id"].'#news">Läs vidare!</a></div>';



        echo '<div style="margin-left:80px;">'.$blogcontent.'</div>';
        echo '<hr>';


    }

    /*
        display the pagination
        you can echo $pagination_output anywhere you want
    */
    echo $pagination_output; 
}
else
{
    echo 'No results found!';
}
}
else
{
echo $pg_error; //display any errors, you can remove this if you want.  
}
?>
Matthew Daly
  • 9,212
  • 2
  • 42
  • 83
chrjoh88
  • 27
  • 8
  • 1
    dunno what tutorial you used, but it is surely very old. It is not recommended to use mysql_* functions in new projects, because they are vulnerable to injection & other bad stuff. Use PDO. – MightyPork May 25 '14 at 17:37
  • 2
    As you are new to php: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php?rq=1 – jeroen May 25 '14 at 17:37
  • 1
    I'll keep my clothes on, thanks – Strawberry May 25 '14 at 17:37
  • Errors? If any, add error reporting to the top of your file(s) `error_reporting(E_ALL); ini_set('display_errors', 1);` during production and use `var_dump();` to track what is set or not. – Funk Forty Niner May 25 '14 at 17:37
  • use mysqli ** functions. mysql functions getting deprecated – fortune May 25 '14 at 17:39
  • Got no errors so far lucky enough but It's just my lack of experiense how to get the data output right. I've fiddle with this code from time to time but getting close and closer atleast :) – chrjoh88 May 25 '14 at 17:39
  • *"but my lack of experience in MYSQL, PHP is what holding me back i think"* --- So why not learn (functions and all) from the ground up, instead of going for Gold right away? It's best to learn both slowly but surely before grabbing code from the Web and trying to make it work and asking us to debug your code. – Funk Forty Niner May 25 '14 at 17:40
  • @Strawberry exactly what i was about to say :) – andrew May 25 '14 at 17:43
  • Yeah what I'm after hhere just after is tips I've got plenty of time to try it out myself so a solution would't be that fun for me I enjoy the challenge and thinking but atm im stuck for what I want so more like "Have you've consider trying somthing like this" with out give me to mutch clues :) – chrjoh88 May 25 '14 at 17:44
  • Remember [`what I wrote`](http://stackoverflow.com/questions/23832726/cant-get-comments-set-for-each-row-need-some-new-input#comment36670521_23832726) in your [**other and same question**](http://stackoverflow.com/q/23832726/) you asked a couple of days ago? You could've found another script and a better one at that within that time, instead of trying to get this to work. Google `"pagination script php mysqli pdo"` – Funk Forty Niner May 25 '14 at 17:45
  • Thanks @Fred-ii- Will defently do that :) – chrjoh88 May 25 '14 at 17:47

1 Answers1

0

This is not working:

$blog_comment=mysql_query("Select * from comments WHERE bid='$blogid' AND verify='1' ")

use

$blog_comment=mysql_query("Select * from comments WHERE bid=" . $blogid ." AND verify='1' ")
ratmalwer
  • 700
  • 5
  • 14