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?