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 如何用stata画出文献中常见的安慰剂检验图
    • ¥15 c语言链表结构体数据插入
    • ¥40 使用MATLAB解答线性代数问题
    • ¥15 COCOS的问题COCOS的问题
    • ¥15 FPGA-SRIO初始化失败
    • ¥15 MapReduce实现倒排索引失败
    • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
    • ¥15 找一位技术过硬的游戏pj程序员
    • ¥15 matlab生成电测深三层曲线模型代码
    • ¥50 随机森林与房贷信用风险模型