Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> MySQL
Author Message
CodyG
Life Cycles Becoming CPU Cycles



Joined: Jan 02, 2003
Posts: 714
Location: Vancouver Island

PostPosted: Sat Apr 07, 2007 11:16 pm Reply with quote

I thought this block might work, but it's way too simple, so naturally it's not working. Wink All I'm getting is nothing but blank after the last working block. I checked my error logs and no help there. I'm guessing it's a mysql issue, but I could be wrong.

What I'm trying to do should be pretty obvious.
table nuke_mymod with columns called name and picpath.
ie: name picpath
CodyG 123.jpg

The block should display a random row with name followed by 123.jpg. Simple right? There are less than 200 rows in the table, in case you were thinking I should use something other than RAND().

Any ideas as to why this isn't working would be appreciated. A fix will be rewarded with lunch if you are ever in my neighbourhood.

I've tried various permutations of quoting and non-quoting, this is the latest...


Code:
<?php


if ( !defined('BLOCK_FILE') ) {
    Header('Location: ../index.php');
    die();
}


 $imagepath = 'modules/mymod/images/thumbs/';
 $result = $db->sql_query("SELECT RAND( ) , `name` , `picpath` FROM nuke_mymod WHERE picpath != '' LIMIT 1");
 $row = $db->sql_fetchrow($result);
 $content= "$row['name'] <br /> $imagepath.$row['picpath']";
?>

_________________
"We want to see if life is ubiquitous." D.Goldin 
View user's profile Send private message
evaders99
Former Moderator in Good Standing



Joined: Apr 30, 2004
Posts: 3221

PostPosted: Sun Apr 08, 2007 10:09 pm Reply with quote

Let's just grab the count and seed the random number in PHP
Code:


$imagepath = 'modules/mymod/images/thumbs/';
$numrows = $db->sql_numrows($db->sql_query("SELECT * FROM nuke_mymod WHERE picpath != '' LIMIT 1"));
if ($numrows > 1) {
   $numrows = $numrows-1;
   mt_srand((double)microtime()*1000000);
   $num = mt_rand(0, $numrows);
} else {
   $num = 0;
}
$result = $db->sql_query("SELECT `name` , `picpath` FROM nuke_mymod WHERE picpath != '' LIMIT " . $num . ", 1");
$row = $db->sql_fetchrow($result);
$content= "$row['name'] <br /> $imagepath.$row['picpath']";

_________________
- Star Wars Rebellion Network -

Need help? Nuke Patched Core, Coding Services, Webmaster Services 
View user's profile Send private message Visit poster's website
montego
Site Admin



Joined: Aug 29, 2004
Posts: 9457
Location: Arizona

PostPosted: Tue Apr 10, 2007 6:34 am Reply with quote

CodyG, I hope you do not mind my changing your thread title. If you disagree with it, please do not hesitate to "adjust". Wink

_________________
Where Do YOU Stand?
HTML Newsletter::ShortLinks::Mailer::Downloads and more... 
View user's profile Send private message Visit poster's website
Gremmie
Former Moderator in Good Standing



Joined: Apr 06, 2006
Posts: 2415
Location: Iowa, USA

PostPosted: Tue Apr 10, 2007 7:38 am Reply with quote

I don't understand why you have a LIMIT 1 on the first SQL query evaders? Won't that only return 1 row?

I might suggest changing the query to be "SELECT COUNT(*) FROM ..." as it might be less resource intensive. That will just return a count of the number of rows that matched the query. Maybe try this (untested)

Code:


$sql = 'SELECT COUNT(*) FROM nuke_mymod WHERE picpath != \'\'';
$numrows = 0;
if ($result = $db->sql_query($sql))
{
   $row = $db->sql_fetchrow($result);
   $numrows = $row[0];
}

// no need to call mt_srand() in PHP 4.2.0 or greater
$num = mt_rand(0, $numrows);

// and the rest as per evaders:
$result = $db->sql_query("SELECT `name` , `picpath` FROM nuke_mymod WHERE picpath != '' LIMIT " . $num . ", 1");
$row = $db->sql_fetchrow($result);
$content= "$row['name'] <br /> $imagepath.$row['picpath']";

_________________
GCalendar - An Event Calendar for PHP-Nuke
Member_Map - A Google Maps Nuke Module 
View user's profile Send private message
CodyG







PostPosted: Tue Apr 10, 2007 10:31 am Reply with quote

Thank you my friends. I should remember to write the title after I write the post. Smile


I checked my syntax three times and get the same results with all versions of the code, any further table loading on the page is stopped.
I tried punching up the php error reporting. nada. Maybe a mysql die() function would help.

this is what it looks like so far...

Code:
<?php

error_reporting(E_ALL^E_NOTICE);


if ( !defined('BLOCK_FILE') ) {
    Header('Location: ../index.php');
    die();
}


$imagepath = '/modules/mymod/images/thumbs/';
$numrows = $db->sql_numrows($db->sql_query("SELECT COUNT(*) nuke_mymod WHERE picpath != \'\";
$numrows = 0;

// g

if ($result = $db->sql_query($sql))
{
   $row = $db->sql_fetchrow($result);
   $numrows = $row[0];
}



/* e
if ($numrows > 1) {
   $numrows = $numrows-1;
   mt_srand((double)microtime()*1000000);
   $num = mt_rand(0, $numrows);
} else {
   $num = 0;
}
*/



$result = $db->sql_query("SELECT `name` , `picpath` FROM nuke_mymod WHERE picpath != '' LIMIT " . $num . ", 1");
$row = $db->sql_fetchrow($result);
$content= "$row['name'] <br /> $imagepath.$row['picpath']";



?>


Last edited by CodyG on Tue Apr 10, 2007 10:53 am; edited 1 time in total 
Gremmie







PostPosted: Tue Apr 10, 2007 10:51 am Reply with quote

Run the SQL queries in phpMyAdmin just to see if they are working the way you (and we) expect.
 
CodyG







PostPosted: Tue Apr 10, 2007 10:58 am Reply with quote

The first query produced 76.
The second produced this error.
Quote:


SELECT `name` , `picpath`
FROM nuke_mymod
WHERE picpath != ''
LIMIT " . $num . ", 1
LIMIT 0 , 30

MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '" . $num . " , 1
LIMIT 0, 30' at line 1
 
Gremmie







PostPosted: Tue Apr 10, 2007 12:49 pm Reply with quote

When you run that 2nd one in phpMyAdmin you sort of have to act like PHP and fill in the values first. So what does this return?

SELECT name, picpath FROM nuke_mymod WHERE picpath != '' LIMIT 42, 1

Here, I just picked 42 as the random number.

I also noticed you have the code that computes $num commented out. Put that back in before running the second query (in the actual PHP code, not in phpMyAdmin).
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> MySQL

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©