Forum Moderators: coopster

Message Too Old, No Replies

Extracting random MySQL row

Need help with mysql function

         

SoulMaster

6:35 pm on Jan 17, 2008 (gmt 0)

10+ Year Member



I have code like this:

function getRow($table, $column) {
if ($sql = mysql_query("SELECT * FROM $table
WHERE $column='something'
ORDER BY RAND() LIMIT 1")){
return($sql);}
else{
$yks=mysql_error();
return($yks);
}
}

but what i nid is a code where i input table and column and then he'll give me a random row in this table and column.

Thank you.

physics

1:08 am on Jan 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you want to change

if ($sql = mysql_query("SELECT * FROM $table

to

if ($sql = mysql_query("SELECT $column FROM $table

phranque

2:59 am on Jan 18, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], SoulMaster!

you question wasn't absolutely clear, but after physics' change you would get the value of the given $column from a random row from the given $table where $column='something'.
which means you'll get either literally 'something' or actually nothing.

i'm not sure this is really what you wanted.
i'm guessing the query you want might be:
SELECT $column FROM $table
WHERE $column IS NOT NULL
ORDER BY RAND() LIMIT 1

SoulMaster

2:59 pm on Jan 18, 2008 (gmt 0)

10+ Year Member



Ty i got it now...but what if i want to get a row. And seperate the parts from every column...So I can use all 5 parts in different places...but they have to be in same row.

Atm.. i tried to change it like that:

function getRow($table, $column) {
if ($sql = mysql_query("SELECT $column FROM $table
WHERE $column IS NOT NULLORDER BY RAND() LIMIT 1 ")){
while($row=mysql_fetch_array($sql)) {
return($row);}

return($row);}
else{
$yks=mysql_error();
return($yks);
}
}
and now i get smthing like that:
Array ( [0] => Kas vastus on a? [kys] => Kas vastus on a? )

its double

phranque

3:46 pm on Jan 18, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



this will return the entire row:
SELECT * FROM $table
WHERE $column IS NOT NULL
ORDER BY RAND() LIMIT 1

SoulMaster

5:20 pm on Jan 18, 2008 (gmt 0)

10+ Year Member



if i wont do it with array... i get it with something else too like name of column n so on... but how can i get only the thing that i have entered to the mysql...cause without row, i get resource id 4 or 3?

SoulMaster

5:59 pm on Jan 18, 2008 (gmt 0)

10+ Year Member



Hey Thank You guys...i got it with 2 functions...thx to your help.

function get_row($table, $row, $column) {
if ($sql = mysql_query("SELECT $column FROM $table
WHERE kys ='$row'
ORDER BY RAND() LIMIT 1 ")){
while($row=mysql_fetch_array($sql)) {
return($row[0]);}

return($row);}
else{
$yks=mysql_error();
return($yks);
}
}

function getRow($table, $column) {
if ($sql = mysql_query("SELECT $column FROM $table
WHERE $column IS NOT NULL
ORDER BY RAND() LIMIT 1 ")){
while($row=mysql_fetch_array($sql)) {
return($row[0]);}

return($row);}
else{
$yks=mysql_error();
return($yks);
}
}

The lower one gives me a row from the first column n then the upper one gives me the other columns on same row:D

eelixduppy

7:22 pm on Jan 18, 2008 (gmt 0)



I'm glad you found your solution. I have just one suggestion to your queries so that it never causes any problems down the road. Since you are defining the column and table names in the query through the function arguments, you should make sure that you aren't using a reserved word in mysql. Just in case you accidentally (or purposely) used a reserved word for either of these two values, we can get around this by escaping the names of each in your query. Your functions would then look like the following:

function get_row($table, $row, $column) {
if ($sql = mysql_query("SELECT `$column` FROM `$table`
WHERE kys ='$row'
ORDER BY RAND() LIMIT 1 ")){
while($row=mysql_fetch_array($sql)) {
return($row[0]);}
#
return($row);}
else{
$yks=mysql_error();
return($yks);
}
}
#
function getRow($table, $column) {
if ($sql = mysql_query("SELECT `$column` FROM `$table`
WHERE `$column` IS NOT NULL
ORDER BY RAND() LIMIT 1 ")){
while($row=mysql_fetch_array($sql)) {
return($row[0]);}
#
return($row);}
else{
$yks=mysql_error();
return($yks);
}
}

For a list of reserved words in mysql, refer to the following: [dev.mysql.com...]

Also, a warm welcome to WebmasterWorld! :)