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'];
?>
5
  • read the logs (error log and maybe acces log of your webserver) Commented Jan 18, 2015 at 11:34
  • add this at the top of your code: error_reporting(E_ALL); ini_set('display_errors', 1); Commented Jan 18, 2015 at 11:35
  • 1
    Please consider reading this: stackoverflow.com/questions/12859942 Commented Jan 18, 2015 at 11:36
  • Please see my answer. 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. Commented Jan 18, 2015 at 12:24
  • I know, thanks anyway for your help, I really appreciate it! Commented Jan 18, 2015 at 13:14

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

However this will still not print $row contents since he using mysqli_fetch_assoc. A while loop is requried
Or you could use mysqli.
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.
Use this tool: phpcodechecker.com . Just paste there your php code and it will hopefully notify about errors in coding.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.