调用未定义的方法PDO :: fetchColumn()

时间:2016-03-28 10:41:30

标签: php mysql pdo

我正在尝试生成一个随机数,如果它不存在于同一列中,则将其插入其他信息中。全部使用AJAX,所以它在另一个php文件中 我正在使用这个:

$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';
}

但我得到了: -

  

“调用未定义的方法PDO :: fetchColumn()”。

重要的是要说我是前端开发人员,所以...:D

帮助; - ;

4 个答案:

答案 0 :(得分:1)

我在PDO手册belongs to PDOStatement中找到的唯一fetchColumn方法:

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

您正尝试从PDO

的实例中调用它
$db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
if (!$db->fetchColumn())

答案 1 :(得分:1)

所以你开始使用两个数据库API,让我们继续使用现代的API。既然你正在这样做,你也可以正确使用它并使用准备好的陈述。

$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';

答案 2 :(得分:-1)

你应该写。

$query->fetchColumn()

For Detail

答案 3 :(得分:-1)

解决!

我使用了这个帖子中的提示来做到这一点:

$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';
}

我无法选择一个答案,所以我提出了所有提示。谢谢大家!