-1

I'm trying to generate a random number and insert it among other information if it doesn't exist in the same column. All with AJAX, so it is in another php file.
I'm using this:

$dbhost = "127.0.0.1";
$dbuser = "root";
$dbpass = "";
$dbname = "feely";
$db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);

function func(){
   $success = false;
   while (!$success) {
      $rndInt = rand(0, pow(36, 6) - 1);
      $rndStr = base_convert ($rndInt, 10, 36);
      $rndStr = str_pad($rndStr , 6, "0", STR_PAD_LEFT);
      global $db;

      $query = "SELECT * FROM pedidos WHERE Codigo = {$rndStr} LIMIT 1";
      $db->query($query);
      if (!$db->fetchColumn()) { // value does not exist yet
         // insert new random value
         //$db->query($query);
         $success = true; // will terminate the loop
         return $rndStr;
      } else { // value already exists
         // do nothing - try again in the next loop
      }
   }
}

$code = func();
$device= $_POST['devicename'];
$issue= $_POST['issue'];

$sql = "insert into pedidos (Code, Device, Issue) values (";
$sql .= "'$codigo', '$device', '$issue')";

if(mysqli_query($db, $sql)){
   echo 'success';
}

But I'm getting:-

"Call to undefined method PDO::fetchColumn()".

It is important to say that I'm a front-end developer, so... :D

Help ;-;

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Igor
  • 131
  • 3
  • 10

4 Answers4

1

The only fetchColumn method I could find in the PDO manual belongs to PDOStatement:

public mixed PDOStatement::fetchColumn ([ int $column_number = 0 ] )

You're trying to call it from an instance of PDO:

$db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
if (!$db->fetchColumn())
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
1

So you started out using two database APIs, let's move forward with the modern one. Since you're doing that, you may as well use it properly and use prepared statements.

$dbhost = "127.0.0.1";
$dbuser = "root";
$dbpass = "";
$dbname = "feely";
$db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

function random() {
    $rndInt = rand(0, pow(36, 6) - 1);
    $rndStr = base_convert ($rndInt, 10, 36);
    $rndStr = str_pad($rndStr , 6, "0", STR_PAD_LEFT);
    return $rndStr;
}

function get_unique_code(){
    global $db;
    $rndStr = random();
    $query = "SELECT * FROM pedidos WHERE Codigo = ? LIMIT 1";
    $stmt = $db->prepare($query);
    $stmt->bindParam(1, $rndStr, PDO::PARAM_STR);
    while (true) {
        $rndStr = random();
        if (!$stmt->fetchcolumn()) {
            // value does not exist yet
            return $rndStr;
        }
    }
}

$code = get_unique_code();
$device= $_POST['devicename'];
$issue= $_POST['issue'];

$query = "INSERT INTO pedidos (Code, Device, Issue) VALUES (?, ?, ?)";
$stmt = $db->prepare($query);
$stmt->execute(array($code, $device, $issue));
echo 'success';
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
miken32
  • 42,008
  • 16
  • 111
  • 154
  • Just to clarify, mysqli is no less "modern" than PDO. – Your Common Sense Mar 30 '16 at 04:41
  • Thanks for the edits, I was trying to answer on a tablet. I was under the impression that mysqli, while newer than mysql, was still a PHP 4 holdover. Looks like it debuted in PHP 5 with PDO though. – miken32 Mar 30 '16 at 04:54
-1

you should write.

$query->fetchColumn()

For Detail

SAUMYA
  • 1,508
  • 1
  • 13
  • 20
-2

Solved!

I used the hints in this thread to make this:

$db = mysqli_connect('127.0.0.1', 'root', '', 'feely');

function func(){
   $success = false;
   while (!$success) {
      $rndInt = rand(0, pow(36, 6) - 1);
      $rndStr = base_convert ($rndInt, 10, 36);
      $rndStr = str_pad($rndStr , 6, "0", STR_PAD_LEFT);
      global $db;

      $result = mysqli_query($db, "SELECT Device FROM pedidos WHERE Code = '{$rndStr}'");
      $row_cnt = mysqli_num_rows($result);
      if ($row_cnt == 0) { // value does not exist yet
         $success = true; // will terminate the loop
         return $rndStr;
      } else { // value already exists
         // do nothing - try again in the next loop
      }
   }
}

$code = func();
$device= $_POST['devicename'];
$issue= $_POST['issue'];

$sql = "insert into pedidos (Code, Device, Issue) values (";
$sql .= "'$codigo', '$device', '$issue')";

if(mysqli_query($db, $sql)){
   echo 'success';
}

I couldn't choose just one answer, so I made one with all the hints. Thank you all!

Igor
  • 131
  • 3
  • 10
  • This is a strange answer to a question tagged as PDO since it doesn't use PDO at all. I hope you at least understand the difference. (The DV is not mine anyway, in case you wonder.) – Álvaro González Mar 28 '16 at 14:45
  • This code is just terrible. It is not only using the wrong way to see whether the query returned anything or not, **which would always return true**, but the query itself can result with error, **which is misinterpreted as an empty result.** – Your Common Sense Mar 28 '16 at 14:46
  • @YourCommonSense Better? – Igor Mar 30 '16 at 03:32
  • @YourCommonSense Man, I built the last edited code using this thread: http://stackoverflow.com/questions/2973202/mysql-fetch-array-mysql-fetch-assoc-mysql-fetch-row-expects-parameter-1-to Why is it wrong? As you can check here: http://php.net/manual/en/function.mysql-query.php "For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error." The same for myslqi. – Igor Mar 31 '16 at 05:45
  • @YourCommonSense Hey... Is it corret now? If it is, can you remove the negative? I don't want to be blocked :( – Igor Mar 31 '16 at 07:06