-1

So I want to pull a random record off my DB:

mysql_connect("127.0.0.1","my_shop","xxxxxx");

mysql_select_db("my_shop_db");
error_reporting(E_ALL && ~E_NOTICE);

$random_record = "SELECT * FROM products ORDER BY RAND() LIMIT 1";
$random_product = mysql_query($random_record) or die ($random_record);

echo $random_product['id'];

What I tried so far:

  • DB Connection works 100%
  • Tables exist, have exact wordings and contain data
  • DB User has rights to pull Data

What is wrong with my script?

user980018
  • 133
  • 8
  • 2
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). – Jay Blanchard Dec 12 '14 at 21:09
  • 2
    Please, what does "does not work" mean? What are you seeing? What do you expect to see? What does your data look like? Are there any errors? Have you read the manual? http://php.net/manual/en/function.mysql-query.php – Madbreaks Dec 12 '14 at 21:10
  • Thank you for your answers! It won't be a live project, just a rough protoype. I'll look at your suggestions! – user980018 Dec 12 '14 at 21:27

3 Answers3

2

That can't work. You make a query thats it. You should fetch the data first with mysql_fetch_assoc or another function to get an output.

And the mysql_* functions are deprecated you should start with mysqli or PDO and prepared statements.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
René Höhle
  • 26,716
  • 22
  • 73
  • 82
1

Obligatory comment that mysql_* is deprecated and not safe and should not be used anymore.

mysql_query returns a resource object NOT an array. You need to fetch the data into an array so you can access it.

$random_record = "SELECT * FROM products ORDER BY RAND() LIMIT 1";
$random_product = mysql_query($random_record) or die ($random_record);
//fetch data
$data_array = mysql_fetch_assoc($random_product);

echo $data_array['id'];

print_r the array so you understand the structure and how to access the element you want.

Dan
  • 10,614
  • 5
  • 24
  • 35
0

A lot of folks on here (correctly) point out that mysql should no longer be used, then go on to answer the question using mysql. I think it's worthwhile to show you how easy it is to make the change to mysqli. Using dan08's answer as a jumping off point:

//set up connection, save it into the $link variable
$link = mysqli_connect("127.0.0.1","my_shop","xxxxxx", "my_shop_db");
//your query, same as before
$random_record = "SELECT * FROM products ORDER BY RAND() LIMIT 1";
//almost the same syntax as mysql_*, but with the $link added in to specify the connection
$random_product = mysqli_query($link, $random_record) or die ($random_record);
//fetch data
$data_array = mysqli_fetch_assoc($random_product);

echo $data_array['id'];
jdmcguire
  • 111
  • 3