mysql - How to keep temporary mysqli table available in php during statement execution? -
i busy trying execute set of statements involve use of temporary table.
my goal create temporary table, insert values , comparison of temporary tables contents table.
these statements working in phpmyadmin when executed raw sql, i'm assuming table not available when try insert data.
below code php function + mysqli execution:
function searcharticles($tags){ global $dbconn, $statuscode; $count = 0; $tagcount = count($tags); $selecttext = ""; $result_array = array(); $article_array = array(); foreach($tags $tag){ if($count == 0){ $selecttext .= "('%".$tag."%')"; }else { $selecttext .= ", ('%".$tag."%')"; } $count++; } $query = "create temporary table tags (tag varchar(20));"; $stmt = $dbconn->prepare($query); if($stmt->execute()){ $query2 = "insert tags values ?;"; $stmt = $dbconn->prepare($query2); $stmt->bind_param("s", $selecttext); if($stmt->execute()){ $query3 = "select distinct art.articleid article art join tags t on (art.tags t.tag);"; $stmt = $dbconn->prepare($query3); if($stmt->execute()){ $stmt->store_result(); $stmt->bind_result($articleid); if($stmt->num_rows() > 0){ while($stmt->fetch()){ array_push($article_array, array("articleid"=>$articelid)); } array_push($result_array, array("response"=>$article_array)); }else{ array_push($result_array, array("response"=>$statuscode->empty)); } }else{ array_push($result_array, array("response"=>$statuscode->sqlerror)); } }else{ array_push($result_array, array("response"=>$statuscode->sqlerror)); } }else{ array_push($result_array, array("response"=>$statuscode->sqlerror)); } $stmt->close(); return json_encode($result_array); }
the first statement executes perfectly, second statement gives me error of:
php fatal error: call member function bind_param() on non-object
if error temp table not existing, how preserve table long enough run rest of statements?
i have tried use:
$stmt = $dbconn->multi_query(query);
with queries in one, need insert data 1 query , data select query.
any appreciated, thank you!
this not issue temporary table. should remain throughout same connection (unless resets timeout, not sure part).
the error $stmt
non-object. means query invalid (syntax error), mysqli refused create instance of mysqli_stmt
, returned boolean instead.
use var_dump($dbconn->error)
see if there errors.
edit: noticed query $query2
insert tags values ?
(the ;
redundant anyway). if becomes string "text"
, become insert tags values "text"
. sql syntax error. should wrap ?
()
, becomes insert tags values (?)
.
in conclusion, change line:
$query2 = "insert tags values ?;";
to:
$query2 = "insert tags values (?);";
also note don't need ;
terminate sql statements passed mysqli::prepare
.
Comments
Post a Comment