Untested, but hopefully it will get you using prepared statements, and uses ORDER BY RAND() that was suggested by Royal Bg:
$query = 'SELECT id FROM datacards WHERE id <> ? ORDER BY RAND() LIMIT 2';
$stmt = mysqli_prepare($con,$query) or die( mysqli_error($con) );
mysqli_stmt_bind_param($stmt, 's', $dcard);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while($row = mysqli_fetch_array($result))
{
$ids[] = $row['id'];
}
$Myarray = implode("," , $ids);
Note that ORDER BY RAND() is very slow with large data due to having to reorder all the rows matched. It's better to first count the rows in the table, then generate a number between 0 and the number of rows minus one. This number will represent the index of the record in the database. Then generate another random number between 0 and the number of rows minus two. This will represent another record index in the database. If the second index is equal to or greater than the first index, then add one to its value. With this algorithm, you'll have two completely random non-repeating indexes from the database. Then you'd ORDER BY field(id, ?, ?) LIMIT 2 with the two question marks being the ids you want to select, and the order you want to select them. You can find the FIELD function of MySQL in the manual here:
https://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_field
$dcarduser-supplied data? (from GET / POST)