3

I'm using PHP to create a dynamic table in my database and store some data in it.

Sometimes when i run the query the table is created and data query is executed but then again if i refresh the query or refresh my page, it shows Error saying table doesn't exist and again if i refresh my page it will load data in 2 or 3 tries.

I'm creating table dynamically and then perform operation on it and at the end I'm dropping the table. Here is my code

<?php

$droptable = "DROP TABLE IF EXISTS temp_post";

if(!mysql_query($droptable))
{echo msql_error();}

    $sqlcreatetable = "CREATE TABLE temp_post(
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    seq_id INT (6),
    name VARCHAR(64) NOT NULL)";

    if(!mysql_query($sqlcreatetable))
        {
            echo " ERROR CREATING TABLE --> ".mysql_error();
        }
    else{
           //query here
        }
        $sqldel = "DROP TABLE temp_post";
        if(!mysql_query($sqldel)){
            echo "ERROR DELETING TABLE --> ".mysql_error();
        }

      ?>

I've gone through some other posts and tried the solutions but still its not working properly.Help!!.. CHEERS

4
  • 2
    what error are you getting? Commented Dec 17, 2015 at 9:38
  • During operation query this error - > Table 'temp_post' doesn't exist... and at the end DROP TABLE Query, this error -> DELETING TABLE --> Unknown table 'temp_post'... Commented Dec 17, 2015 at 9:41
  • I dont see any connection to a MySQL Server or a database within that server! Do we assume you left that out of your code sample or did you forget that completely Commented Dec 17, 2015 at 9:52
  • nope.. it wasn't necessary to write the connection strings here... so just relevant code. Commented Dec 17, 2015 at 9:59

2 Answers 2

1

May be conflicts.. 2 simultaneous requests, so one request can drop table that is used by another request
Try to change CREATE TABLE to CREATE TEMPORARY TABLE

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

6 Comments

let me try.. then i'll post whats comes up
do i need to drop this CREATE TEMPORARY TABLE by query or it will be dropped automatically at the end?
According to man it is dropped automatically. "A TEMPORARY table is visible only to the current session, and is dropped automatically when the session is closed. This means that two different sessions can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. (The existing table is hidden until the temporary table is dropped.)"
No errors so far.. i think it worked.. i've refreshed my page a couple of times and its fine..
and I've not removed this drop query at the bottom.. should i?
|
1

Why create and drop anyway? Use CREATE TEMPORARY TABLE or just TRUNCATE the table after use

1 Comment

let me try.. then i'll post whats comes up

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.