2

I'm having a little problem with php, basically I want to get a random row from my mysql database, I am really new to php and mysql so please be kind and explain me what's going on. I've already granted all permissions on mysql, now I just have to figure out what's going on, i tried to put some echoes to debug but it seems like anything happens, there's just a blank page with nothing on it, this drives me crazy so I'd like to resolve it. Here's the code

<?php
echo "test";
$host="127.0.0.1"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="mine"; // Database name
$tbl_name="accounts"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Select a random account
$min=1;
$row=mysql_fetch_assoc(mysql_query("SHOW TABLE STATUS LIKE 'mine.accounts';"));
$max=$row["Auto_increment"];
$random_id=rand($min,$max);
$row=mysql_fetch_assoc(mysql_query("SELECT * FROM `mine`.`accounts` WHERE id='$random_id'");
echo $row["username"]. ":" . $row["password"]
?>
// --- UPDATE ---
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$host="127.0.0.1"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="mine"; // Database name
$tbl_name="accounts"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Select a random account
$row = mysql_query("SELECT username AND password FROM accounts order by RAND() LIMIT 1");
WHILE ($data = mysql_fetch_array($row))
ENDWHILE;
echo $row['username'] . " " . $row['password'];
?>
Maxpnl
  • 98
  • 2
  • 10
  • read the logs (error log and maybe acces log of your webserver) – Peter Jan 18 '15 at 11:34
  • add this at the top of your code: error_reporting(E_ALL); ini_set('display_errors', 1); – patrickdamery Jan 18 '15 at 11:35
  • 1
    Please consider reading this: http://stackoverflow.com/questions/12859942/ – lolbas Jan 18 '15 at 11:36
  • Please see my [answer.](http://stackoverflow.com/questions/28009207/php-issue-with-random-mysql-rows/28009461#28009461) I've written an entire function for you to get the data you need. I've tested this and it works with no errors. But I highly suggest you read up on MySQLi and prepared SQL statements. Not everyone will be kind enough to take the time to write a function just for you. – Jason Bassett Jan 18 '15 at 12:24
  • I know, thanks anyway for your help, I really appreciate it! – Maxpnl Jan 18 '15 at 13:14

1 Answers1

1

On this line, you forgot the closing parentheses.

$row=mysql_fetch_assoc(mysql_query("SELECT * FROM `mine`.`accounts` WHERE id='$random_id'");

Hence the single closing parentheses while you open two.

$row=mysql_fetch_assoc(mysql_query("SELECT * FROM `mine`.`accounts` WHERE id='$random_id'"));

And you'll have to use a while loop to make $row output anything, since fetch_assoc returns an associative array:

while($row = mysql_fetch_assoc(<...>){
     $max = $row['Auto_increment'];
}

Also you might wanna look into Prepared Statements or PDO as mysql_* Functions are officially deprecated.

  • However this will still not print `$row` contents since he using `mysqli_fetch_assoc`. A `while` loop is requried – lolbas Jan 18 '15 at 11:39
  • Or you could use mysqli. – piry Jan 18 '15 at 11:39
  • On my server i changed the code, anyway even if I put an echo/ error_reporting(E_ALL); ini_set('display_errors', 1); anything happens. I have some other php applications on my server and they all work, I don't get what's wrong with this file. – Maxpnl Jan 18 '15 at 11:52
  • Use this tool: http://phpcodechecker.com/ . Just paste there your php code and it will hopefully notify about errors in coding. – valicu2000 Jan 18 '15 at 11:53
  • I'm still getting issues, I would really appreciate if anyone could convert my code to a mysqli one, even because I'm really new to php and I don't think I'm gonna do everything correct by myself – Maxpnl Jan 18 '15 at 12:02