Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 09-11-2005, 12:19 AM
kjhkjh's Avatar
kjhkjh kjhkjh is offline
 
Join Date: Jul 2005
Posts: 77
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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.
Reply With Quote
  #2  
Old 09-11-2005, 12:27 AM
Andreas's Avatar
Andreas Andreas is offline
 
Join Date: Jan 2004
Location: Germany
Posts: 6,863
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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();
?>
Reply With Quote
  #3  
Old 09-11-2005, 01:06 AM
kjhkjh's Avatar
kjhkjh kjhkjh is offline
 
Join Date: Jul 2005
Posts: 77
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #4  
Old 09-11-2005, 01:45 AM
harmor19 harmor19 is offline
 
Join Date: Apr 2005
Posts: 1,324
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #5  
Old 09-11-2005, 01:49 AM
Andreas's Avatar
Andreas Andreas is offline
 
Join Date: Jan 2004
Location: Germany
Posts: 6,863
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

@kjhkjh
Works fine for me, except there were two syntax errors I hadn't corrected in your Code.
Edited my last post.
Reply With Quote
  #6  
Old 09-11-2005, 02:18 AM
kjhkjh's Avatar
kjhkjh kjhkjh is offline
 
Join Date: Jul 2005
Posts: 77
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:21 PM.


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.04050 seconds
  • Memory Usage 2,234KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (3)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (6)post_thanks_box
  • (6)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (6)post_thanks_postbit_info
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete