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

Link to comment
Share on other sites

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
?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.