Saturday, 15 February 2014

mysql - PHP check if ID already is in use -



mysql - PHP check if ID already is in use -

i busy creating own 6 player game based on php/mysql. want check if id players_online in use, if yes need maintain searching until found 1 (till max 6) else need homecoming 0;

$con connection database , can find settings on .mysql_config.php

this got:

function find_local_id() { include'./mysql_config.php'; $i=1; $select_id=mysqli_fetch_assoc(mysqli_query($con, "select `id` `players_online` id = ".$i."")); if ($select_id["id"] == $i) { $i++; $select_id=mysqli_fetch_assoc(mysqli_query($con, "select `id` `players_online` id = ".$i."")); if ($select_id["id"] == $i) { $i++; $select_id=mysqli_fetch_assoc(mysqli_query($con, "select `id` `players_online` id = ".$i."")); if ($select_id["id"] == $i) { $i++; $select_id=mysqli_fetch_assoc(mysqli_query($con, "select `id` `players_online` id = ".$i."")); if ($select_id["id"] == $i) { $i++; $select_id=mysqli_fetch_assoc(mysqli_query($con, "select `id` `players_online` id = ".$i."")); if ($select_id["id"] == $i) { $i++; $select_id=mysqli_fetch_assoc(mysqli_query($con, "select `id` `players_online` id = ".$i."")); if ($select_id["id"] == $i) { homecoming 0; } else { homecoming 6; } } else { homecoming 5; } } else { homecoming 4; } } else { homecoming 3; } } else { homecoming 2; } } else { homecoming 1; }

is there way have smaller/compact? because think pretty big little function. :)

i think work:

function find_local_id() { include'./mysql_config.php'; $result = mysqli_query($con, "select `id` `players_online`"); while($row = mysqli_fetch_assoc($result)) { $ids[] = $row['id']; } if($available = array_diff(range(0, 6), $ids)) { homecoming min($available); } homecoming 0; }

if have mysqli_fetch_all utilize that.

get of ids in table see ones (1-6) not in results if available homecoming lowest one if not homecoming 0

php mysql

No comments:

Post a Comment