vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   whats wrong with this php code? (https://vborg.vbsupport.ru/showthread.php?t=96020)

kjhkjh 09-11-2005 12:19 AM

whats wrong with this php code?
 
The basics is that I have a table on the database that has a single
field (i have been trying some other stuff which includes an ID number
field but I wondered if anyone can help me fix the stuff below so I
don't need it).

The single field has about 100 entries so far but this will always
change and at some times there may be no entries (see the if / else
clause).

Otherwise I need to display a random entry from the table.

Here goes...

<?php
$username="dbusername";
$password="dbpassword";
$database="dbname";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM tablename";
$results=mysql_query($query);
$numrows=mysql_numrows($results);

$randget="SELECT * FROM tablename ORDER BY RAND() LIMIT 1";
$randgetresults=mysql_query($randget);

if ($numrows=0) {echo "Sorry no entries"

} else {

echo "$randgetresults";
mysql_close();
?>

If you can see anything wrong please let me know.

Andreas 09-11-2005 12:27 AM

Id try smth. like
PHP Code:

<?php
$username
="dbusername";
$password="dbpassword";
$database="dbname";

mysql_connect('localhost',$username,$password);
@
mysql_select_db($database) or die( "Unable to select database");

$randgetq=mysql_query("SELECT * FROM tablename ORDER BY RAND() LIMIT 1");

if (!
$randgetresults=mysql_fetch_array($randgetqMYSQL_ASSOC))
{
    echo 
"Sorry no entries";
}
else
{
    
print_r($randgetresults);
}
mysql_close();
?>


kjhkjh 09-11-2005 01:06 AM

thanks for your help kirby.

I'm still getting a blank page.

This is so strange. I even tried it adding the following to the top to see if that made a difference:


chdir('/home/mywebname/public_html/forums/');
define('NO_REGISTER_GLOBALS', 1);
require_once('./global.php');


I think that I must be missing something obvious.

Should the code should go into a regular .php file?
Is there anything else I need to add to it?

Even if I try and take out the random function I'm still getting blank pages. This has been 4 days I've been working on it and still no joy :(

harmor19 09-11-2005 01:45 AM

Try

save the code below as "random.php" and upload to your forums index.
PHP Code:

<?php

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

// #################### DEFINE IMPORTANT CONSTANTS #######################
define('NO_REGISTER_GLOBALS'1);
define('THIS_SCRIPT''random'); // 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(
    
'RANDOM',
);

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

);

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

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

$navbits = array();
$navbits[$parent] = 'Random';

$randgetq$DB_site->query("SELECT * FROM tablename ORDER BY RAND() LIMIT 1");

if (!
$randgetresults=$DB_site->fetch_array($randgetqMYSQL_ASSOC))
{
    
$entries "Sorry no entries";
}
else
{
    
$entries $randgetresults;
}
mysql_close(); 

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

?>


Now go to your acp and make a new template named "random"
Place the below code in the textarea
PHP Code:

$stylevar[htmldoctype]
<
html dir="$stylevar[textdirection]lang="$stylevar[languagecode]">
<
head>
<
title>$vboptions[bbtitle]</title>
$headinclude
</head>
<
body>
$header

$navbar

<table class="tborder" cellpadding="$stylevar[cellpadding]cellspacing="$stylevar[cellspacing]border="0" width="100%" align="center">
<
tr>
    <
td class="tcat">Random</td>
</
tr>
<
tr>
    <
td class="alt1">$entries</td>
</
tr>
</
table>

$footer
</body>
</
html

Now just go to "yoursite.com/forums/random.php"

I got this from https://vborg.vbsupport.ru/showthread.php?t=62164

Andreas 09-11-2005 01:49 AM

@kjhkjh
Works fine for me, except there were two syntax errors I hadn't corrected in your Code.
Edited my last post.

kjhkjh 09-11-2005 02:18 AM

OH I almost have it, thanks guys. Kirby and Harmor you've both taught me something tonight.

It is almost perfectly

The small thing is that it is also containing part of the code in the print output - ie/ it prints "array ( [tablefield] => 5456 )" when I just want it to print "5456" or any other random entry from the table)

I am baffled.

----------

NEWS: The answer>

Instead of print_r use:

echo "$randgetresults[tablefield]";

This works! MarcoH64 offered that information which is the last piece but I wouldn't have got there without you and I really appreciate your help. Without it I would have been staring at my screen for another 4 days.

Thank you


All times are GMT. The time now is 01:11 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01070 seconds
  • Memory Usage 1,751KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (6)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete