temp-tabelle mit php erzeugen (MySql 3.23)
temp-tabelle mit php erzeugen (MySql 3.23)
am 24.01.2005 16:34:38 von j_wenke
Hallo,
gibts ein beispiel, wie man ein temp Tabelle mit php erzeugen kann
(MySql 3.23)?
nach:
http://dev.mysql.com/doc/mysql/de/example-maximum-column-gro up-row.html
wollt ich probieren:
$result2 = mysql_query("
CREATE TEMPORARY TABLE tmp
(artikel INT(4) UNSIGNED ZEROFILL DEFAULT
'0000' NOT NULL,
preis DOUBLE(16,2) DEFAULT '0.00' NOT NULL);
LOCK TABLES shop read;
INSERT INTO tmp SELECT artikel, MAX(preis)
FROM shop
GROUP BY artikel;
SELECT shop.artikel, haendler, shop.preis
FROM shop, tmp
WHERE shop.artikel=tmp.artikel AND
shop.preis=tmp.preis;
UNLOCK TABLES;
DROP TABLE tmp");
if(!$result2) echo "--- nichts ---";
while($print2 = mysql_fetch_row($result2)) {
echo "q3-> : ".$print2[0]."
"; }
echo "\n";
ergibt aber
--- nichts ---
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL
result resource ...
Habe ich das CREATE TEMPORARY TABLE so richtig untergebracht? Oder wo
liegt der Fehler?
Danke, Jörg
Re: temp-tabelle mit php erzeugen (MySql 3.23)
am 24.01.2005 17:46:19 von Thomas Hamacher
j wenke schrieb:
[Snip]
> Habe ich das CREATE TEMPORARY TABLE so richtig untergebracht? Oder wo
> liegt der Fehler?
16.2. Wieso kann ich mehrere, durch Semikolon getrennte Statements nicht
ausführen?
http://www.php-faq.de/q/q-sql-statements.html
Ausserdem solltest du ein Errorhandling einbauen, da du dann den Fehler
selber hättest lokalisieren und beheben können.
Re: temp-tabelle mit php erzeugen (MySql 3.23)
am 25.01.2005 00:10:22 von Niels Braczek
j wenke schrieb:
> $result2 = mysql_query("
> [SQL bulk statement]
> ");
Wenn du eine Fehlerbehandlung machen würdest, täte dir MySQL mitteilen,
wo der Fehler liegt.
> if(!$result2) echo "--- nichts ---";
> ergibt aber
> --- nichts ---
Logisch.
MfG
Niels
--
Je korrupter ein Staat, desto zahlreicher seine Gesetze. [Tacitus}
Re: temp-tabelle mit php erzeugen (MySql 3.23)
am 25.01.2005 15:04:52 von j_wenke
Hallo!
(das ist ja krass-kompliziert für so einen "Standard"-Fall. ?-) relativ
Well! Ohne die funct. zu ändern habe ich sie erstmal so eingebaut:
$sql = "..."; $release = "3.23.39"; $ret = TRUE;
$result = mysql_query(PMA_splitSqlFile(&$ret, $sql, $release));
if(!$result) echo "--- nix ---";
while($print2 = mysql_fetch_row($result)) {
echo "q-> : ".$print2[0]."
"; }
echo "\n";
$sqlfin = "UNLOCK TABLES;
DROP TABLE tmp";
mysql_query(PMA_splitSqlFile(&$ret, $sqlfin, $release));
Dabei habe ich $release = "3.23.39"; $ret = TRUE; "angenommen"
(Weiß jemand was &$ret ist)??
Das ergab leider noch kein Ergebnis und die Meldung:
Warning: Cannot use a scalar value as an array in (s.u.)
Da ich ein älteres phpadmin habe (+php 4.2.1),
habe die read_dump.php genommen und daraus die function PMA_splitSqlFile.
Ist doch ähnlich?
Danke, Grüße, Jörg
##############################################
function PMA_splitSqlFile(&$ret, $sql, $release) {
$sql = trim($sql);
$sql_len = strlen($sql);
$char = '';
$string_start = '';
$in_string = FALSE;
$time0 = time();
for ($i = 0; $i < $sql_len; ++$i) {
$char = $sql[$i];
// We are in a string, check for not escaped end of strings
except for
// backquotes that can't be escaped
if ($in_string) {
for (;;) {
$i = strpos($sql, $string_start, $i);
// No end of string found -> add the current substring
to the
// returned array
if (!$i) {
$ret[] = $sql;
return TRUE;
}
// Backquotes or no backslashes before quotes: it's
indeed the
// end of the string -> exit the loop
else if ($string_start == '`' || $sql[$i-1] != '\\') {
$string_start = '';
$in_string = FALSE;
break;
}
// one or more Backslashes before the presumed end of
string...
else {
// ... first checks for escaped backslashes
$j = 2;
$escaped_backslash = FALSE;
while ($i-$j > 0 && $sql[$i-$j] == '\\') {
$escaped_backslash = !$escaped_backslash;
$j++;
}
// ... if escaped backslashes: it's really the end
of the
// string -> exit the loop
if ($escaped_backslash) {
$string_start = '';
$in_string = FALSE;
break;
}
// ... else loop
else {
$i++;
}
} // end if...elseif...else
} // end for
} // end if (in string)
// We are not in a string, first check for delimiter...
else if ($char == ';') {
// if delimiter found, add the parsed part to the returned
array
$ret[] = substr($sql, 0, $i); //<<<<<<< WARNING
$sql = ltrim(substr($sql, min($i + 1, $sql_len)));
$sql_len = strlen($sql);
if ($sql_len) {
$i = -1;
} else {
// The submited statement(s) end(s) here
return TRUE;
}
} // end else if (is delimiter)
// ... then check for start of a string,...
else if (($char == '"') || ($char == '\'') || ($char == '`')) {
$in_string = TRUE;
$string_start = $char;
} // end else if (is start of string)
// ... for start of a comment (and remove this comment if found)...
else if ($char == '#'
|| ($char == ' ' && $i > 1 && $sql[$i-2] . $sql[$i-1]
== '--')) {
// starting position of the comment depends on the comment type
$start_of_comment = (($sql[$i] == '#') ? $i : $i-2);
// if no "\n" exits in the remaining string, checks for "\r"
// (Mac eol style)
$end_of_comment = (strpos(' ' . $sql, "\012", $i+2))
? strpos(' ' . $sql, "\012", $i+2)
: strpos(' ' . $sql, "\015", $i+2);
if (!$end_of_comment) {
// no eol found after '#', add the parsed part to the
returned
// array if required and exit
if ($start_of_comment > 0) {
$ret[] = trim(substr($sql, 0, $start_of_comment));
}
return TRUE;
} else {
$sql = substr($sql, 0, $start_of_comment)
. ltrim(substr($sql, $end_of_comment));
$sql_len = strlen($sql);
$i--;
} // end if...else
} // end else if (is comment)
// ... and finally disactivate the "/*!...*/" syntax if MySQL <
3.22.07
else if ($release < 32270
&& ($char == '!' && $i > 1 && $sql[$i-2] . $sql[$i-1]
== '/*')) {
$sql[$i] = ' ';
} // end else if
// loic1: send a fake header each 30 sec. to bypass browser timeout
$time1 = time();
if ($time1 >= $time0 + 30) {
$time0 = $time1;
header('X-pmaPing: Pong');
} // end if
} // end for
// add any rest to the returned array
if (!empty($sql) && ereg('[^[:space:]]+', $sql)) {
$ret[] = $sql; //<<<<<<< WARNING
}
return TRUE;
} // end of the 'PMA_splitSqlFile()' function
##############################################
Re: temp-tabelle mit php erzeugen (MySql 3.23)
am 25.01.2005 16:29:45 von Niels Braczek
j wenke schrieb:
> $result = mysql_query(PMA_splitSqlFile(&$ret, $sql,
> $release)); if(!$result) echo "--- nix ---";
Fehlerbehandlung sieht zB so aus:
$result = mysql_query($sqlStr) or die($sqlStr.'
'.mysql_error());
Dann siehst du, was MySQL dir zu sagen hat.
> (Weiß jemand was &$ret ist)??
Sieh im Manual nach unter 'References'.
MfG
Niels
--
Wenn Word für Längeres geeignet wäre, würde es nicht
Word, sondern Sentence, Page oder Article heißen.
[Matthias Mühlich de.comp.text.tex]
Re: temp-tabelle mit php erzeugen (MySql 3.23)
am 25.01.2005 20:10:22 von j_wenke
Ahh qui -
Nun sieht "es" so aus:
CREATE TEMPORARY TABLE tmp (artikel INT(4) UNSIGNED ZEROFILL DEFAULT
'0000' NOT NULL, preis DOUBLE(16,2) DEFAULT '0.00' NOT NULL); LOCK
TABLES shop read; INSERT INTO tmp SELECT artikel, MAX(preis) FROM shop
GROUP BY artikel; SELECT shop.artikel, haendler, shop.preis FROM shop,
tmp WHERE shop.artikel=tmp.artikel AND shop.preis=tmp.preis
Fehler in der Syntax bei '1' in Zeile 1.
--------im Vergleich zu ohne function, direkt ------------:
CREATE TEMPORARY TABLE tmp (artikel INT(4) UNSIGNED ZEROFILL DEFAULT
'0000' NOT NULL, preis DOUBLE(16,2) DEFAULT '0.00' NOT NULL); LOCK
TABLES shop read; INSERT INTO tmp SELECT artikel, MAX(preis) FROM shop
GROUP BY artikel; SELECT shop.artikel, haendler, shop.preis FROM shop,
tmp WHERE shop.artikel=tmp.artikel AND shop.preis=tmp.preis
Fehler in der Syntax bei '; LOCK TABLES shop read; I' in Zeile 3.
---------------ist was passiert---------------.
ich könnte es jetzt vorsingen ...
Der Fehler liegt m.E. noch an den Semikolons bzw. der fehlenden
"Behandlung".
Ich dachte, die Funct. bereitet die query auf ("formatiert")? - MYSQL-Front
hat nichts gegen den 1. query-string oben.
Im Manual unter 'References' habe ich mal "nachgesehen", konnte aber nicht
erkennen, um welchen Übergabeparameter es sich hätte handeln können (in den
anderen phpadmin-codedatein sah ich mal was von FALSE, daher ...)
Ich habe meine gutgemeinte $ret-Zuweisung jetzt einfach weggelassen
und bekomme jetzt noch oben beschriebenen Fehler.
Gruß, Jörg
------
A little sunlight is the best disinfectant. (Louis Brandeis)
Re: temp-tabelle mit php erzeugen (MySql 3.23)
am 25.01.2005 22:50:11 von Thomas Hamacher
j wenke schrieb:
[Snip]
> Der Fehler liegt m.E. noch an den Semikolons bzw. der fehlenden
> "Behandlung".
Was genau hast du an meiner Antwort nicht verstanden? SQL kennt keine
durch Semikolon getrennten Queries, der MySQL Client auch nicht, was
erwartest du?
> Ich dachte, die Funct. bereitet die query auf ("formatiert")? - MYSQL-Front
> hat nichts gegen den 1. query-string oben.
Wer lesen kann ist klar im Vorteil.
> Im Manual unter 'References' habe ich mal "nachgesehen", konnte aber nicht
> erkennen, um welchen Übergabeparameter es sich hätte handeln können (in den
> anderen phpadmin-codedatein sah ich mal was von FALSE, daher ...)
Dir fehlen Grundlagen.
> Ich habe meine gutgemeinte $ret-Zuweisung jetzt einfach weggelassen
> und bekomme jetzt noch oben beschriebenen Fehler.
Ach!. Eine Funktion ohne Sinn und Verstand anzuwenden
führt selten zum Ziel. Hier mal ein Tip:
$query = "LOCK ...; SELECT ...; UNLOCK ...";
$queries = array();
PMA_splitSqlFile($queries, $query, '3.23');
Re: temp-tabelle mit php erzeugen (MySql 3.23)
am 27.01.2005 03:10:00 von j_wenke
Hallo.
So. Soweit gelöst.
(mit Hilfe einer anderen, (etwas) gesprächigeren ng und eurer faq)
Was mir momentan noch nicht klar ist, ist beim m.E.
abschließenden Durchlauf die Meldung
Tabelle 'tmp' bereits vorhanden.
- In MySql-Front muß man damit
(UNLOCK TABLES; DROP TABLE tmp)
abschließen, um nichts Doppeltes angezeigt.
alles schaut jetzt so aus:
-------------------
$sql2 = " ... "; $release = "3.23.39";
PMA_splitSqlFile(&$ret, $sql2, $release);
while (list(, $value) = each ($ret))
foreach ($ret as $value) {
$result = mysql_query($value) or die("
no query:
".mysql_error()); }
$print2 = mysql_fetch_array($result);
echo "sql-> : ".$print2[0]." - ".$print2[1]." -
".$print2[2]."
";
$sqlfin = "UNLOCK TABLES;
DROP TABLE tmp";
PMA_splitSqlFile(&$ret, $sqlfin, $release);
while (list(, $value) = each ($ret))
foreach ($ret as $value) {
$result = mysql_query($value) or
die("
".mysql_error()); }
-------------------
Dank nochmal, Gruß, Jörg