1

I have been trying to learn php and have been getting better day by day but still fighting cant wait till everything just falls into place and flow. (With other programming languages it worked that way for me fight for a while then everything just works)

but I am trying to make it return a random account and display all there photos along with data.

Here is my example code

 <?php
$config['db'] = array(
'host' => 'DATA',
'username' => 'WOULD',
'password' => 'GO',
'dbname' => 'HERE'
);

$db = new PDO('mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['dbname'], $config['db']['username'], $config['db']['password']);

$query = $db->query("SELECT `content`.`ProfilePic` FROM `content`");

while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
$Hello = $row['ProfilePic'];
echo  '<html><img src="http://www.MYDOMAIN.COM/Content/';
echo $Hello;
echo '"></html>';

}
?>

So what this is doing is returning everyones profile pics on one page Not quite what I want

I have it where in a feild on my mysql data base it has a unique Id for each account I would like it to randomly pic one of those then return all the data for that row

FirstName
LastName
Gender
City
State
FacebookUrl
BirthMonth
BirthDay
BirthYear
AccountNumber - This is the one I want to pull and use to return everything
ProfilePic
ProfilePicCaption

So basically pick a random row and pull all the data instead of displaying all the data for one column

Thank you any and all help is awesome and at least now im using secure code

bush man
  • 147
  • 2
  • 2
  • 8

1 Answers1

2

Use the RAND function

 <?php
$config['db'] = array(
'host' => 'DATA',
'username' => 'WOULD',
'password' => 'GO',
'dbname' => 'HERE'
);

$db = new PDO('mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['dbname'], $config['db']['username'], $config['db']['password']);

$query = $db->query("SELECT * FROM `content` ORDER BY RAND() LIMIT 1");

while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
$Hello = $row['ProfilePic'];
// and so on, so forth
echo  '<html><img src="http://www.MYDOMAIN.COM/Content/';
echo $Hello;
echo '"></html>';

}
?>
hd1
  • 33,938
  • 5
  • 80
  • 91
  • This requires full scan of users table, so is incredibly slow. This solution is much better for this: http://stackoverflow.com/a/19422/1734130 – mvp Nov 25 '12 at 06:53
  • http://stackoverflow.com/questions/10054836/mysql-whats-the-most-efficient-way-to-select-multiple-random-rows?lq=1 also, here, @mvp, looks better to me – hd1 Nov 25 '12 at 06:58