Jump to content

problem creating a table on mysql with php


Perigoso

Recommended Posts

Hi there guys

I'm having a problem witch i can't solve, i would like anyone to help me out.

I have this code.

<?php
$fp = fopen("fod.txt", "r");
$ler2=fread($fp,8092);
fclose($fp);
$f= unserialize($ler2);

$dbhost='localhost';
$dbusername='root';
$dbname='apa';

$link_id = mysql_connect ($dbhost, $dbusername);

if (!mysql_select_db($dbname)) die(mysql_error());

$result= "CREATE TABLE $f (id int(5) auto_increment, matprima char(30),fornecedor char(100), qld char(15), gran char(10), peso char(10),horas char(50),qtdent char(20),cod char(100), PRIMARY KEY (id))";
if (mysql_query($result)){

print ("<p><h3 align=center>Table created...</h3></p>");

mysql_close($link_id);


}

 

 

The problem is on the $f variable.If it has the value of "tom", the table can be created, if it has the value of "tom jones" i canot create it.

Is it because of the space in between?

Is there another way?

Thanks

you are right the problem is the space between tom and jones, you cannot have a table name with spaces in it. you can write yourself a little function that removes spaces and replaces it with an underscore

 

<?php
function createTableName($text)
{
return str_replace(" ", "_", $text);//replaces spaces with an underscore
}

$fold= unserialize($ler2);//f old 

$fnew = createTableName($fold);//f new. then use $fnew instead of $f in your query
?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.