PDA

View Full Version : whats wrong with this php code?


kjhkjh
09-11-2005, 12:19 AM
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
$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($randgetq, MYSQL_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

// ####################### 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($randgetq, MYSQL_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

$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