Question

The following seems to produce an endless loop but I can seem to figure out why it's not working. I know that the insert is not working but I can't figure out why. I've tried the query out in phpmyadmin and it works fine.

function generate_cart_id($dbh) {

        //generate cart_id and check against assigned_carts to make sure it's unique
        $ip=$_SERVER['REMOTE_ADDR'];
        $stmt=$dbh->prepare("insert into assigned_carts (cart_id,ip,date) values (:cart_id,:ip,now())");
        $stmt->bindValue(':ip',$ip, PDO::PARAM_STR);
        do {
                $cart_id=mt_rand(100000000,9999999999);
                $stmt->bindValue(':cart_id',$cart_id, PDO::PARAM_INT);
        }
        while ($stmt->execute()==false);
        return $cart_id;
}

Here's what I came up with in the end.

function generate_cart_id($dbh) {   
    $ip=$_SERVER['REMOTE_ADDR'];
    $stmt=$dbh->prepare("INSERT INTO assigned_carts (cart_id,ip,date) Value ((select * from (SELECT FLOOR(100000000 + RAND() * 899999999) as ar From assigned_carts where 'ar' NOT IN (Select cart_id FROM assigned_carts) LIMIT 1) as x) ,:ip,now())");
    $stmt->bindValue(':ip',$ip, PDO::PARAM_STR);

    try {
        $stmt->execute();
        $id=$dbh->lastInsertID(); //You cannot retreive a randomly generated id with last id. Only an autoincremented one so I added that and used a select to get the random one.
        $row=$dbh->query("select cart_id from assigned_carts where id=$id")->fetch();

        return $row['cart_id'];
    }
    catch(PDOException $e){
        // do something
        return -1;
    }
}
Was it helpful?

Solution

This isn't a strictly PDO-based solution, but you could try something like this:

// grab list of all cart_id ids
$qry = 'SELECT cart_id FROM assigned_carts'
      .' WHERE cart_id >= 100000000 && cart_id <= 9999999999';
$all_cart_ids = $dbh->exec($qry);

// generate a random number that's not in that list
while($cart_id === NULL || in_array($cart_id, $all_cart_ids)){
    $cart_id=mt_rand(100000000,9999999999);
}

// insert
$stmt=$dbh->prepare("INSERT INTO assigned_carts (cart_id,ip,date) VALUES (:cart_id,:ip,now())");
$stmt->bindValue(':ip',$ip, PDO::PARAM_STR);
$stmt->bindValue(':cart_id',$cart_id, PDO::PARAM_INT);
try {
    $stmt->execute();
    return $cart_id;
}
catch(PDOException $e){
    // do something
    return -1;
}

OTHER TIPS

try something like

if ($stmt->execute()) {
    // Query succeeded.
} else {
    // Query failed.
    $errorcode = $stmt->errorCode();
}

or

if ($sql->execute === false) {    //false on error
     var_dump($sql->errorinfo();  //array containing information on error
     die()
}

BEST try something like

function getData($db) {
   $stmt = $db->query("SELECT * FROM table");
   return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

//then much later
try {
   getData($db);
} catch(PDOException $ex) {
   //handle me.
}

Good Read

PDO Tutorial for MySQL Developers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top