0

I am trying to create a table using PDO my connection is fine as i just tested it with try catch block. But i am unable to create any table in my DB

function loadData($table)
{

$conn = new PDO("mysql:host=localhost;dbname=anlyatics_db", "root", "");
$query = "CREATE TABLE if not exists $table); ";
$stmt = $conn->prepare($query);
if ($stmt)
{
    $stmt->execute();
}
}
16
  • 1
    Do you get an error? A blank screen? What happens when this code runs? Is there anything in $stmt->ErrorInfo()? Commented Oct 23, 2013 at 17:28
  • havnet check that, let me get a hold on to that Commented Oct 23, 2013 at 17:29
  • Is your DB name spelled correctly? Commented Oct 23, 2013 at 17:29
  • what does $table contain? Commented Oct 23, 2013 at 17:29
  • 1
    please provide $table + the exact error you get. Commented Oct 23, 2013 at 17:31

2 Answers 2

3

Based on your comment:

" it contains name for the table e.g at the moment it has CDM_2011"

I would say you need to add columns to your create statement. I don't think you can have a create statement without column definitions.

Sign up to request clarification or add additional context in comments.

4 Comments

yeah looks legit need to have look at it
just to see if this is the issue, try this: CREATE TABLE if not exists $table (id int);
oh right: after $table you have a closing bracket, you nowhere have an opening bracket either
Marking it correct as table cant be created without column definiton and marking your post correct is for the face you point it out 1st.
1

Have you got the CREATE right?? See here for a MySQL User management?

For my part, I'm not sure that create table from PHP is safe!...

To retrieve the error, use

$conn->errorInfo();

PHP MANUAL

EDIT

Have you tried to put at least 1 column? As you can see here, MySQL CREATE statement needs 1 column...

2 Comments

the solution you mentioned returns Array ( [0] => 00000 [1] => [2] => )
Ok so in the array : ([0]=>00000, [1]=>, [2]=>)) the first mean : SQLSTATE, and it is 0... So no error returned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.