dqrfdl5708 2019-07-13 20:47
浏览 73

PHP函数与mysql不兼容

Well, what am trying to do is create a function that can create a

Database Table and Model class

automatically in pure PHP codes and this is the code I have so far

1. function addTableToDB($pdo, $table, $column = [], $must){
2.  $query = 'CREATE TABLE IF NOT EXISTS ' . $table . '(';
3.  $query .= 'id  int(11) not null, primary key auto_increment, ';
4.  foreach ($column as $key => $value) {
5.      #foreach($type as $t){
6.          #foreach($max as $m){
7.              #echo $key . ' ' . $value . ' ' . ' ' . $must . "<br>";
8.              $query .= "" . $value/* . " " . $t . " " . $must . ","*/ . $must . "," . PHP_EOL;
            #}
        #}*/
    }
    $query = rtrim($query, ',');
    $pdo->exec($query);
    /*$result = 'DESCRIBE ' . $table;

    $join = '<?php' . PHP_EOL . '/**' . PHP_EOL . '* Document your script' . PHP_EOL . '*/' . PHP_EOL;
    $join .= '# Your entity name' . PHP_EOL . 'namespace cdr/Entity';
    $join .= 'class ' . $table . '{' . PHP_EOL;
    foreach ($result as $r) {
        # code...
        $join .= $r . PHP_EOL;
    }
    $join .= '}' . PHP_EOL;
    # Write to the file'
    $file = __DIR__ . '/../specific/entity';
    if(is_writable($file)){
        file_put_contents($file, $join);
        return true;
    }else{
        return false;
    }*/
    # Use the DESCRIBE Command of the MYSQL Database to generate an automatic entity for each table 
}

The problem is on line 8 where I tried to concatenate PHP code with SQL(Which is causing the problem) and this is the error it gives

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' name varchar(20)not null, ball char(100)not null, age int(11)not null,' at line 1 in /home/vagrant/Code/Project/public/500.php on line 20

Why do I need to use a line breaking statement?

The reason is because each new line specify a new column eg

CREATE TABLE IF NOT EXISTS Users 
( 
   name VARCHAR(32), 
   handle VARCHAR(16),# start a new line 
   pass CHAR(32), # s new line also
   email VARCHAR(256), 
   INDEX(name(6)), 
   INDEX(handle(6)), 
   INDEX(email(6)) 
);

This is what it returns

' name varchar(20)not null, ball char(100)not null, age int(11)not null,

It tried to translate all the Mysql code on one line instead of breaking them into different lines using the PHP_EOL or '<br>'.

How can I make the SQL code break into a new line when it see the or '<br>' or 'PHP_EOL' or better still, what will I use to make SQL to start a line?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教