php - i try to create table in mysql with use of random() function.?

223

I want to create able in mysql with random name of table name i try but cant created. this is my code

$temp_table=rand(1000,9999);
$sql="CREATE TABLE $temp_table (FirstName CHAR(30),LastName CHAR(30),Age INT)";
527

Answer

Solution:

Identifiers may begin with a digit but unless quoted may not consist solely of digits. Try doing this instead:

$temp_table=randLetter().rand(1000,9999);
$sql="CREATE TABLE $temp_table (FirstName CHAR(30),LastName CHAR(30),Age INT)";

function randLetter()
{
  return chr(97 + mt_rand(0, 25));
}
224

Answer

Solution:

Try quoting the name:

$sql="CREATE TABLE `$temp_table` (FirstName CHAR(30),LastName CHAR(30),Age INT)";

MySQL doesn't like numbers as identifiers. (Or prefix with something liketable_.)

People are also looking for solutions to the problem: php - Mysql function returns empty result

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.