PDO - > if(表存在)

时间:2018-06-05 12:59:30

标签: pdo

try {
    $query = $pdo->query("SELECT 1 FROM `classes` LIMIT 1");
} catch (Exception $e) {
    $query = $pdo->prepare("CREATE TABLE `classes`(
    `ID_class` int(11) AUTO_INCREMENT,
    `name` varchar(255),
    PRIMARY KEY(`ID_class`))");

    $query->execute();
}

您好。

如果表格不存在,它就无法捕获。

2 个答案:

答案 0 :(得分:0)

根据PHP documentation (php.net)

,这不起作用
  

返回值

     

PDO :: query()会在失败时返回PDOStatement对象,或 FALSE

因此,您需要测试#!/usr/bin/python import tensorflow as tf import numpy as np x_ = tf.placeholder(np.float32, [None, 1], 'input') y_ = tf.placeholder(np.float32, [None, 1], 'label') #layer1 w1 = tf.Variable(tf.random_normal([1,3])) b1 = tf.Variable(tf.random_normal([3]) ) a1 = tf.add(b1,tf.matmul(x_,w1)) #layer2 w2 = tf.Variable(tf.random_normal([3,1])) b2 = tf.Variable(tf.random_normal([1]) ) a2 = tf.add(b2,tf.matmul(a1,w2),name="output") #global steps steps = 5000 x = [] y = [] for i in range(1,200,5): temp = (1.0 * i)/10 x.append([temp]) y.append([3. + 2. * temp]) x = np.array(x) y = np.array(y) #loss function loss = tf.reduce_mean(tf.reduce_sum(tf.square(a2-y_))) #optimizer optimizer = tf.train.GradientDescentOptimizer(0.00001).minimize(loss) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) length = len(x) #training... for i in range(steps): sess.run(optimizer,feed_dict={x_:x,y_:y}) result = sess.run(loss,feed_dict={x_:x,y_:y}) if i % 50 == 0: print("loss: ",result,"\tstep: ",i) saver = tf.train.Saver() saver.save(sess,"./model/model.ckpt") tf.train.write_graph(sess.graph.as_graph_def(), "./model/", "graph.pbtxt") print("predict...") pre = sess.run(a2,feed_dict={x_:[[0]]}) print("x = 2 pre: ",pre) 是否返回#include <fstream> #include <sstream> #include <iostream> #include <opencv2/dnn.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> using namespace std; using namespace cv; using namespace dnn; std::vector<std::string> classes; int main(int argc,char**argv) { if(argc != 2) { cout<<"Usage: ./main [tensorflow modle path(.pb)]"<<endl; return -1; } String model = argv[1]; Net net = cv::dnn::readNetFromTensorflow(model,argv[2]); cout<<"load Net OK!!"<<endl; float inp[1*1] = {2}; Mat Matrix(1,1,CV_32FC1,inp); cout<<"Matrix:\n"<<Matrix<<endl; net.setInput(Matrix); Mat output = net.forward(); cout<<"output: " << output <<endl; return 0; } 还是替代checking if a table exists without using 'select from' (stackoverflow.com)

答案 1 :(得分:0)

对我来说,PDO ATT_ERRMODE设置为ERRMODE_EXCEPTION。

$user = 'test';
$pass = 'test';
$opt  = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];

$db   = new PDO('mysql:host=localhost;dbname=test;charset=utf8' ,
                                    $user ,
                                    $pass ,
                                    $opt
                                    );


try {
    $query = $db->query("SELECT 1 FROM `classes` LIMIT 1");
} catch (Exception $e) {
    $query = $db->prepare("CREATE TABLE `classes`(
    `ID_class` int(11) AUTO_INCREMENT,
    `name` varchar(255),
    PRIMARY KEY(`ID_class`))");

    $query->execute();
}