Jump to content

Temporary Tables in MYSQL and php


arianhojat

Recommended Posts

Hello all,
I am trying to get a temp table to work via php(5) and MYSQL(4.1); but i cant get it to work, not sure whats wrong, i run the queries via command client and they run fine ... here is my code:

[code]
$createTempString = 'CREATE TEMPORARY TABLE IF NOT EXISTS disaster_order';
$createTempString .= '(';
$createTempString .= ' empID int(5) NOT NULL,';
$createTempString .= ' orderNum int(5) unsigned NOT NULL,';
$createTempString .= ' PRIMARY KEY (empID,orderNum)';
$createTempString .= ');';
$result = mysql_query($createTempString) or die ("Error in query: $createTempString. ".mysql_error());


//this array is really set via returning an unserialized database value which works fine in my code
$empArray = array(46, 35, 40, 314, 393, 32, 59, 26, 54, 25, 51, 37, 58, 72, 76);

$insertString = 'INSERT INTO disaster_order(empID, orderNum) VALUES';
foreach($empArray as $key=>$empID)//0, 46 //1, 35
{
$empOrder = $key;

$insertString .= ' ('. $empID .', '. $empOrder .')';

if($key==(count($empArray)-1))
$insertString .= ';';
else if( count($empArray) > 1 )
$insertString .= ',';
}
$result = mysql_query($createTempString) or die ("Error in query: $insertString. ".mysql_error());

//everything runs fine (no die() errors reported so i assume the temp table is created somewhere )

$Query = "SELECT * FROM disaster_order";
$result = mysql_query($Query) or die ("Error in query: $Query. ".mysql_error());

//echo 'count='. mysql_num_rows( $result ); 0 reported back
while($row=mysql_fetch_assoc($result))
{
echo 'empID'='.$row['empID'] .'<br/>';
}[/code]


I assume I dont need to select some sort of predfined default named database where I would run my SELECT query against. like
$db = "mysql_temp_storage";
mysql_select_db($db) or die ("Unable to select database!");
$Query = "SELECT * FROM disaster_order";


I do however select a real database be4 the code I pasted above, just in case the temp table queries need to have some database open (like when it works in the command line client, i do have to USE some database just to run the query.)
However my select statement in php isnt returning anything.

PS the Queries my Php makes are:

CREATE TEMPORARY TABLE IF NOT EXISTS disaster_order( empID int(5) NOT NULL, orderNum int(5) unsigned NOT NULL, PRIMARY KEY (empID,orderNum));
INSERT INTO disaster_order(empID, orderNum) VALUES (46, 0), (35, 1), (40, 2), (314, 3), (393, 4), (32, 5), (59, 6), (26, 7), (54, 8), (25, 9), (51, 10), (37, 11), (58, 12), (72, 13), (76, 14);
Link to comment
Share on other sites

how can i test to see if its there?
i thought the select statement is only way to test if its really there.

also none of the create/alter query statements that create/fill the table produced errors/die()'s

i just decided to use an actual permanent table to do it but i woulda prefered to try doing it via a temporary table since i never have tried it in php and would like it in my bag of tricks to use if i need it.
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.