繁体   English   中英

我应该如何重写此方法以获取更多参数

[英]How should I rewrite this method to take more parameters

我需要重写此方法以允许更多参数,以便执行更大的查询,例如这样

$thing= connecting::query('SELECT * FROM accounts WHERE thing1=:thingOne AND random!=: ran_dom AND thing2=:thingTwo AND thing3=:thingThree AND thing4=:thingFour AND thing5=:thingFive AND thing6=:thingSix',                   array(':thingOne'=>$thingOne,':ran_dom'=>$ran_dom,':thingTwo'=>$thingTwo,':thingThree'=>$thingThree,':thingFour'=>$thingFour,':thingFive'=>$thingFive,':thingSix'=>$thingSix));

现在,该方法仅需要两个数组,因此该语句不会产生错误,但不会返回任何内容。 但是此查询工作正常。

$thing= connecting::query('SELECT * FROM accounts WHERE thing1=:thingOne AND random!=:ran_dom',                 array(':thingOne'=>$thingOne,':ran_dom'=>$ran_dom));

方法如下:

public static function query($query,$params = array())
    {


        $statement = self :: db()->prepare($query);
        $statement->execute($params);
        if(explode(' ',$query)[0] == 'SELECT')
        {
            $data = $statement->fetchAll();
            return $data;
        }

    }

我试图像这样重写函数定义,但是没有用:

public static function queried($query,$params array(array(),array(),array(),array(),array(),array()))

您将在函数中传递两个参数,一个是查询,另一个是数组。 您可以在数组中添加多个元素。 它不限于两个。 我猜你可能有查询错误或传递参数问题。 因此,请通过检查错误进行调试。

public static function query($query,$params = array())
{


    $statement = self :: db()->prepare($query);
    $statement->execute($params);
    if($statement->errorCode() === '00000'){
        if(explode(' ',$query)[0] == 'SELECT')
        {
            $data = $statement->fetchAll();
            return $data;
        }
    }
    else {
        $errorInfo = $statement->errorInfo();
        print_r($errorInfo);
        exit;
    }

}

完整的例子

Class connecting{
    public static function query($query,$params = array())
    {


        $statement = self :: db()->prepare($query);
        $statement->execute($params);
        if($statement->errorCode() === '00000'){
            if(explode(' ',$query)[0] == 'SELECT')
            {
                $data = $statement->fetchAll();
                return $data;
            }
        }
        else {
            $errorInfo = $statement->errorInfo();
            print_r($errorInfo);
            exit;
        }

    }
    public static function db(){
        return $db = new PDO('mysql:host=localhost;dbname=test', 'root', '');
    }

}
$ran_dom = "R";
$thingOne = "A";
$thingTwo = "B";
$thingThree = "C";
$thingFour = "D";
$thingFive = "E";
$thingSix = "F";

$result = connecting::query('SELECT * FROM accounts WHERE thing1=:thingOne AND random!=:ran_dom AND thing2=:thingTwo AND thing3=:thingThree AND thing4=:thingFour AND thing5=:thingFive AND thing6=:thingSix',  array(':thingOne'=>$thingOne,':ran_dom'=>$ran_dom,':thingTwo'=>$thingTwo,':thingThree'=>$thingThree,':thingFour'=>$thingFour,':thingFive'=>$thingFive,':thingSix'=>$thingSix));
print_r($result);

表结构

    -- phpMyAdmin SQL Dump
-- version 4.7.0
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jul 15, 2019 at 02:40 PM
-- Server version: 10.1.26-MariaDB
-- PHP Version: 7.1.8

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `test`
--

-- --------------------------------------------------------

--
-- Table structure for table `accounts`
--

CREATE TABLE `accounts` (
  `thing1` varchar(100) NOT NULL,
  `random` varchar(100) NOT NULL,
  `thing2` varchar(100) NOT NULL,
  `thing3` varchar(100) NOT NULL,
  `thing4` varchar(100) NOT NULL,
  `thing5` varchar(100) NOT NULL,
  `thing6` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `accounts`
--

INSERT INTO `accounts` (`thing1`, `random`, `thing2`, `thing3`, `thing4`, `thing5`, `thing6`) VALUES
('A', 'G', 'B', 'C', 'D', 'E', 'F');
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM