Author |
Message |
Dibas
New Member


Joined: Apr 04, 2005
Posts: 16
|
Posted:
Fri May 19, 2006 5:06 am |
|
Hi there, im trying to implement a code to show up a random image, using php/mysql...
this is what i'm using:
Code:global $prefix, $db;
$result = $db->sql_query("SELECT pic, id, banda, FROM ".$prefix."_bandas RAND() 0,1");
while ($row = $db->sql_fetchrow($result)) {
$id = intval($row['id']);
$banda = stripslashes($row['banda']);
$pic = stripslashes(check_html($row['pic'], "nohtml"));
echo "<img src=\"/images/bandas/$pic\" alt=\"$banda\"><br><strong><a href=\"banda$id.html\">$banda</a></strong>";
|
Though... for some reason i can't understand i can't get the $pic to show up the image source... all i get is a blank image with the address /images/bandas/ <-- no picture file (whish is defined on the table under "pic". |
|
|
|
 |
montego
Site Admin

Joined: Aug 29, 2004
Posts: 9457
Location: Arizona
|
Posted:
Fri May 19, 2006 5:29 am |
|
I have never tried the RAND() function, so cannot comment on whether the query is working or not. Have you tried something like this in your while to make sure the query is returning the data properly?
Code:
$result = $db->sql_query("SELECT pic, id, banda, FROM ".$prefix."_bandas RAND() 0,1");
while ($row = $db->sql_fetchrow($result)) {
echo "id = ".$row['id']." :: ";
echo "banda = ".$row['banda']." :: ";
echo "pic = ".$row['pic']."<br>";
|
I am just not seeing anything immediately obvious that is wrong with your code. The $pic variable is not getting valued for some reason. |
_________________ Only registered users can see links on this board! Get registered or login!
Only registered users can see links on this board! Get registered or login! |
|
|
 |
Dibas

|
Posted:
Fri May 19, 2006 6:22 am |
|
ok i had some errors on the code but i tryied yours with my fixes on it... i only get this:
id = 21 :: banda = :: pic =
he's only catching the ID... not the correspondent pic and banda from that ID... |
|
|
|
 |
montego

|
Posted:
Fri May 19, 2006 6:31 am |
|
And you have validated using something like phpMyAdmin that ALL of your rows of data have these fields valued properly? If so, please post the table schema here so I can take a look at it. |
|
|
|
 |
Dibas

|
Posted:
Fri May 19, 2006 6:45 am |
|
ok... first here's the correction to that code:
Code:$result = $db->sql_query("SELECT pic, id, banda FROM ".$prefix."_bandas ORDER BY RAND() LIMIT 0,1");
while ($row = $db->sql_fetchrow($result)) {
echo "id = ".$row['id']." :: ";
echo "banda = ".$row['banda']." :: ";
echo "pic = ".$row['pic']."<br>";
|
here's the table structure:
#
# Table structure for table nuke_bandas
#
DROP TABLE IF EXISTS `nuke_bandas`;
CREATE TABLE `nuke_bandas` (
`Id` int(11) NOT NULL auto_increment,
`banda` varchar(60) NOT NULL default '',
`bio` text NOT NULL,
`email` varchar(60) default NULL,
`url` varchar(100) NOT NULL default '',
`pic` varchar(100) default NULL,
PRIMARY KEY (`Id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1; |
|
|
|
 |
montego

|
Posted:
Fri May 19, 2006 6:53 am |
|
You changed the code on the SQL. Is this working now? |
|
|
|
 |
Dibas

|
Posted:
Fri May 19, 2006 6:54 am |
|
no it.. gives that result i showed u.. only id has a result. |
|
|
|
 |
montego

|
Posted:
Fri May 19, 2006 7:42 am |
|
Quote: |
And you have validated using something like phpMyAdmin that ALL of your rows of data have these fields valued properly?
|
Ok, need to know the answer to this original question then. If you are seeing the data in the database, then take out the ORDER BY RAND() LIMIT 0,1 and at least make sure your code is working.
Like I said, haven't used the RAND function in SQL so just trying to rule that out. |
|
|
|
 |
Dibas

|
Posted:
Fri May 19, 2006 8:00 am |
|
yes the rows have data... i did a order by id... i still can't get data from the other fields... only ID:
id = 1 :: banda = :: pic =
theres definatly something wrong in the code, but no idea what. |
|
|
|
 |
CodyG
Life Cycles Becoming CPU Cycles

Joined: Jan 02, 2003
Posts: 714
Location: Vancouver Island
|
Posted:
Fri May 19, 2006 8:14 am |
|
I'm no expert, but ... might it have something to do with using a mixed case for the id? Shouldn't it be Id all the way through the script as the db table is Id.
Just for reference, here is another random picture script that works for me.
Code:
<?php
if (eregi("block-randomphoto.php",$PHP_SELF)) {
Header("Location: index.php");
die();
}
global $user,$user_prefix,$dbi,$prefix;
$total = sql_fetch_array(sql_query("SELECT COUNT(id) AS total FROM ".$user_prefix."_user_profile1 where photo='1' and approved='1'",$dbi),$dbi);
$p = mt_rand(0,($total[total] - 1));
$pic = sql_fetch_array(sql_query("SELECT ffusername,photopath FROM ".$user_prefix."_user_profile1 where photo='1' and approved='1' LIMIT $p,1",$dbi),$dbi);
$content = "<center><a href=\"modules.php?name=FriendFinder&op=userinfo&ffusername=$pic[ffusername]\"><img src=\"modules/FriendFinder/images/members/thumbs/$pic[photopath]\" border=\"0\" alt=\"$pic[ffusername]\"><br><font size=\"1\">$pic[ffusername]</font></a><br><a href=\"modules.php?name=FriendFinder\">Go To Friendfinder</a></center>";
?>
|
|
_________________ "We want to see if life is ubiquitous." D.Goldin |
|
|
 |
montego

|
Posted:
Sat May 20, 2006 12:42 am |
|
Well, the interesting thing is Dibas is getting the id to be valued from the database.
I have been looking at this from a million angles and the code looks like, only that I don't know what is after this code. I didn't put a closing } on my while statement because it was meant to show where to INSERT a few lines of code and not replace. But I am just not seeing anything else wrong with it. |
|
|
|
 |
Dibas

|
Posted:
Sat May 20, 2006 6:58 am |
|
Yeah i can't understand why is only getting the results from the ID and not the correspondent results of banda and pic of that ID. |
|
|
|
 |
Dibas

|
Posted:
Sat May 20, 2006 7:24 am |
|
i noticed some weird thing on your code montego..
Code:echo "id = ".$row['id']." :: ";
echo "banda = ".$row['banda']." :: ";
echo "pic = ".$row['pic']."<br>";
|
if i change for instance the order... first the banda echo.. i get result on that one and none on Id... lol... pretty weird. the first one echoing is the only one with results.. no matter what column is. |
|
|
|
 |
Dibas

|
Posted:
Sat May 20, 2006 7:56 am |
|
Ok... after a though battle i finally figured out a way to make it work... here's my final code working
Code:<?php
global $prefix, $db;
$result = $db->sql_query("SELECT id FROM ".$prefix."_bandas ORDER BY RAND() LIMIT 0,1");
while ($row = $db->sql_fetchrow($result)) {
$id = intval($row['id']);
$result2 = $db->sql_query("SELECT banda FROM ".$prefix."_bandas where id='$id'");
while ($row = $db->sql_fetchrow($result2)) {
$banda = stripslashes($row['banda']);
$result3 = $db->sql_query("SELECT pic FROM ".$prefix."_bandas where id='$id'");
while ($row = $db->sql_fetchrow($result3)) {
$pic = stripslashes(check_html($row['pic'], "nohtml"));
echo "<img src=\"/images/bandas/$pic\" alt=\"$banda\"><br><strong><a href=\"banda$id.html\">$banda</a></strong>";
}
}
}
?>
|
There's probably an easier way to do this but well this one works lol thanks for the tips. |
|
|
|
 |
montego

|
Posted:
Sat May 20, 2006 2:30 pm |
|
You should not have to do that! Wish I could see the issue! That is frustrating.
 |
|
|
|
 |
Dibas

|
Posted:
Sat May 20, 2006 2:32 pm |
|
you bet it is lol... i had to make a query for each row... puting banda and pic on the second one wont show me one of them lol... only puting the tree separated works... weird |
|
|
|
 |
montego

|
Posted:
Sat May 20, 2006 2:53 pm |
|
We're missing something "easy" but I have been staring at it too long... |
|
|
|
 |
|