It depends on your database interface. I have no idea what fetch_database() does, so I shall give my example using the PDO extension:
// Check that the number is not already in the database.
$stmt = $db->prepare("SELECT COUNT(*) AS id_count FROM numbers WHERE order_id=:id");
$stmt->bindParam(':id', $item_number, PDO::PARAM_INT);
$id_count = 0;
do {
$item_number = mt_rand(100000000, 999999999);
$stmt->execute();
$row = $stmt->fetch();
$id_count = $row['id_count'];
} while ($id_count > 0);
// Insert number.
$stmt = $db->prepare("INSERT INTO numbers (order_id) VALUES (:id)");
$stmt->bindParam(':id', $item_number, PDO::PARAM_INT);
$stmt->execute();
The above is vulnerable to a race condition. A better way would be to make the order_id column UNIQUE, and then attempt to insert:
$stmt = $db->prepare("INSERT INTO numbers (order_id) VALUES (:id)");
$stmt->bindParam(':id', $item_number, PDO::PARAM_INT);
do {
$item_number = mt_rand(100000000, 999999999);
} while (!$stmt->execute());