Log in

View Full Version : Records from database


Przemoo
07-12-2010, 09:18 AM
Hi.

What code is responsible for displaying all the records from the database / table
Is there any other method than "echo"

Speysider
07-12-2010, 02:06 PM
Just use the SELECT query and use $row['(name of row here)'] in a table tag.

Przemoo
07-12-2010, 02:25 PM
what is wrong in that code ?

<?php

// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################
define('NO_REGISTER_GLOBALS', 1);
define('THIS_SCRIPT', 'test'); // change this depending on your filename

// ################### PRE-CACHE TEMPLATES AND DATA ######################
// get special phrase groups
$phrasegroups = array(

);

// get special data templates from the datastore
$specialtemplates = array(

);

// pre-cache templates used by all actions
$globaltemplates = array(
'screeny',
);

// pre-cache templates used by specific actions
$actiontemplates = array(

);

// ######################### REQUIRE BACK-END ############################
require_once('./global.php');

// ################################################## #####################
// ######################## START MAIN SCRIPT ############################
// ################################################## #####################

$navbits = array();
$navbits[$parent] = 'Testowa Strona';

$sql = $db->query_read ("SELECT * FROM `amx_screenshots` ORDER BY ssid DESC");


while( $row = $db->fetch_array($sql) )
{

$x++;

$gracz = $row["player_username"];
$admin = $row["admin_username"];
$data = $row["screenshot_time"];
$mapa = $row["map"];
$ip = $row["player_authid"];

$data = date("H:i:s d.m.Y",$data);
/*
$template->assign_block_vars('screeny', array(

'ID' => $x+$start,
'GRACZ' => $gracz,
'ADMIN' => $admin,
'DATA' => $data,
'MAPA' => $mapa,
'IP' => $ip)
);*/
}

$navbits = construct_navbits($navbits);
eval('$navbar = "' . fetch_template('navbar') . '";');
eval('print_output("' . fetch_template('screeny') . '");');

?>

it shows me only the last entry in the table instead of all

Speysider
07-12-2010, 02:31 PM
try a do ... while loop, instead of just a while loop

Przemoo
07-12-2010, 02:36 PM
what do u mean exactly ? could u change that for me please ?

BirdOPrey5
07-12-2010, 02:50 PM
what do u mean exactly ? could u change that for me please ?

The do... while loop is below... not sure if it will help personally but this is how you do it.


do
{

$x++;

$gracz = $row["player_username"];
$admin = $row["admin_username"];
$data = $row["screenshot_time"];
$mapa = $row["map"];
$ip = $row["player_authid"];

$data = date("H:i:s d.m.Y",$data);
/*
$template->assign_block_vars('screeny', array(

'ID' => $x+$start,
'GRACZ' => $gracz,
'ADMIN' => $admin,
'DATA' => $data,
'MAPA' => $mapa,
'IP' => $ip)
);*/
} while( $row = $db->fetch_array($sql) ) ;

Przemoo
07-12-2010, 03:11 PM
still shows the last entry

i think the problem is here
$sql = $db->query_read("SELECT * FROM `amx_screenshots` ORDER BY ssid DESC");

but i don't know what exactly ;/

BirdOPrey5
07-12-2010, 03:28 PM
Why don't you echo x; at some point so you can see how many rows there are supposed to be in the loop, that would help pinpoint the issue... if x=1 then the problem is with the sql, otherwise the sql is probably right,

Przemoo
07-12-2010, 09:17 PM
could u write an example code for templates ? what code should be there :>

BirdOPrey5
07-12-2010, 09:24 PM
after your while loop just put the line:

echo "<h1>" . $x . "</h1>" ;

That should output the value of x somewhere in a big font, just for debugging purposes, obviously don't run your forum this way. I don't know if $x is available in a template... just put:
<h1>$x</h1>
somewhere in the template and see if anything shows up.

Przemoo
07-12-2010, 09:38 PM
it show up but on the top only (take a look on a screen)

BirdOPrey5
07-12-2010, 09:54 PM
That's good, that means there are 4,668 rows in your variable- the SQL statement is right (probably) you just need to figure out how to print it. (Which I realize was your first question to start this thread, but at some point you questioned if the SQL statement was right).

Przemoo
07-13-2010, 09:40 AM
its working now, because of $dane

$gracz = $row["player_username"];
$admin = $row["admin_username"];
$data = $row["screenshot_time"];
$mapa = $row["map"];
$ip = $row["player_authid"];
$x++;
$data = date("H:i:s d.m.Y",$data);
$dane = (''.$dane.'
<tr>
<td class="alt2" align="center" width="2%"><span class="screeny2">&nbsp;'.$x.'</span></td>
<td class="alt1" align="center" width="15%"><span class="screeny2">&nbsp;'.$gracz.'</span></td>
<td class="alt2" align="center" width="15%"><span class="screeny2">&nbsp;'.$admin.'&nbsp;</span></td>
<td class="alt1" align="center" width="15%"><span class="screeny2">&nbsp;'.$ip.'&nbsp;</span></td>
<td class="alt2" align="center" width="17%"><span class="screeny2">&nbsp;'.$data.'&nbsp;</span></td>
<td class="alt1" align="center" width="13%"><span class="screeny2">&nbsp;'.$mapa.'&nbsp;</span></td>
</tr>
');
} while( $row = $db->fetch_array($sql) ) ;


but there is another problem with pagination
i used tha article - https://vborg.vbsupport.ru/showthread.php?t=120540&highlight=Paginating

paginating shows up but only for few minutes and then disappeared


that how it is

<?php

// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################
define('NO_REGISTER_GLOBALS', 1);
define('THIS_SCRIPT', 'test'); // change this depending on your filename

// ################### PRE-CACHE TEMPLATES AND DATA ######################
// get special phrase groups
$phrasegroups = array(

);

// get special data templates from the datastore
$specialtemplates = array(

);

// pre-cache templates used by all actions
$globaltemplates = array(
'screeny',
);

// pre-cache templates used by specific actions
$actiontemplates = array(

);

// ######################### REQUIRE BACK-END ############################
require_once('./global.php');

// ################################################## #####################
// ######################## START MAIN SCRIPT ############################
// ################################################## #####################

$navbits = array();
$navbits[$parent] = 'Testowa Strona';


$sql_conn = mysql_connect('host', 'login', 'password');
mysql_select_db('database',$sql_conn);

// Default page variables
$perpage = $vbulletin->input->clean_gpc('r', 'perpage', TYPE_UINT);
$pagenumber = $vbulletin->input->clean_gpc('r', 'pagenumber', TYPE_UINT);

// Count all log entries
$bannedcount = $db->query_first("
SELECT COUNT(`player_username`) AS `bannedcount`
FROM `amx_screenshots`
");

sanitize_pageresults($bannedcount['bannedcount'], $pagenumber, $perpage, 100, 25);

// Default lower and upper limit variables
$limitlower = ($pagenumber - 1) * $perpage + 1;
$limitupper = $pagenumber * $perpage;
if ($limitupper > $bannedcount['bannedcount'])
{
// Too many for upper limit
$limitupper = $bannedcount['bannedcount'];
if ($limitlower > $bannedcount['bannedcount'])
{
// Too many for lower limit
$limitlower = $bannedcount['bannedcount'] - $perpage;
}
}
if ($limitlower <= 0)
{
// Can't have negative or null lower limit
$limitlower = 1;
}






$sql = mysql_query("SELECT * FROM amx_screenshots ORDER BY ssid DESC LIMIT " . ($limitlower - 1) . ", $perpage ");
$x=0;
do
{



$gracz = $row["player_username"];
$admin = $row["admin_username"];
$data = $row["screenshot_time"];
$mapa = $row["map"];
$ip = $row["player_authid"];
$x++;
$data = date("H:i:s d.m.Y",$data);
$dane = (''.$dane.'
<tr>
<td class="alt2" align="center" width="2%"><span class="screeny2">&nbsp;'.$x.'</span></td>
<td class="alt1" align="center" width="15%"><span class="screeny2">&nbsp;'.$gracz.'</span></td>
<td class="alt2" align="center" width="15%"><span class="screeny2">&nbsp;'.$admin.'&nbsp;</span></td>
<td class="alt1" align="center" width="15%"><span class="screeny2">&nbsp;'.$ip.'&nbsp;</span></td>
<td class="alt2" align="center" width="17%"><span class="screeny2">&nbsp;'.$data.'&nbsp;</span></td>
<td class="alt1" align="center" width="13%"><span class="screeny2">&nbsp;'.$mapa.'&nbsp;</span></td>
</tr>
');
} while( $row = $db->fetch_array($sql) ) ;

$pagenav = construct_page_nav($pagenumber, $perpage, $bannedcount['bannedcount'], 'screeny.php?' . $vbulletin->session->vars['sessionurl'] . 'do=view');
$navbits = construct_navbits($navbits);
eval('$navbar = "' . fetch_template('navbar') . '";');
eval('print_output("' . fetch_template('screeny') . '");');

?>

kh99
07-13-2010, 12:06 PM
I'm not sure how that's supposed to work. It looks like you're always starting at the first record instead of calculating " $limitlower " like in the article.

Also, I think you might want to go back to using a "while" loop instead of "do...while", because I think the way you have it you're using the "$row" variable the first time before you've set it.

BTW, you probably don't want to post your database connect info. Is there a reason you're opening a database connection instead of using the one vB opens?

Przemoo
07-13-2010, 12:12 PM
I'm not sure how that's supposed to work. It looks like you're always starting at the first record instead of calculating " $limitlower " like in the article.

Also, I think you might want to go back to using a "while" loop instead of "do...while", because I think the way you have it you're using the "$row" variable the first time before you've set it.

BTW, you probably don't want to post your database connect info. Is there a reason you're opening a database connection instead of using the one vB opens?

omg i forgot to hide it :) ;p thx

my hosting is sux, i've got limit in "max_user_connection" and my script doesn't close the connection thats why am using VPS for that.
pagenav was working yesterday i don't know why is not :/

kh99
07-13-2010, 12:24 PM
Oh. Anyway, I see that you *did* calculate $limitlower, you just used 1 in the query, so maybe you were just debugging.

Przemoo
07-13-2010, 12:26 PM
could you show me an example ? am not best in php :/

kh99
07-13-2010, 12:29 PM
No, it was my mistake, what you have is right. :) Sorry, I was looking at the wrong code.

Przemoo
07-13-2010, 12:33 PM
is right but still without pagination :D maybe i will find it somewhere

Przemoo
07-16-2010, 04:44 PM
still not working :/ any solutions for that ?

kh99
07-17-2010, 10:36 PM
Maybe try displaying some values to debug the code? I usually insert them in HTML comments, then view the page source to see the values. You would just have to add them in to your $dane variable.