我有一个运行良好的数据库查询功能 – 除了我遇到了
mysqli预处理语句和longtext字段显然已知的问题.即使通过PHPMyAdmin运行查询工作正常,longtext字段总是空的,会发生什么.根据
http://www.workinginboxershorts.com/php-mysqli-returns-empty-variables-from-longtext-column,将数据类型切换为文本可以解决问题.然而,在我的情况下,我真的更愿意离开这个领域,因为我可以预见这个额外空间有价值的时间.
// Bind results to an array // $stmt = sql query,$out = array to be returned function stmt_bind_assoc (&$stmt,&$out) { $data = MysqLi_stmt_result_Metadata($stmt); $fields = array(); $out = array(); $fields[0] = $stmt; $count = 1; while($field = MysqLi_fetch_field($data)) { $fields[$count] = &$out[$field->name]; $count++; } call_user_func_array('MysqLi_stmt_bind_result',$fields); } // DB Query // $query = sql query,$params = array of parameters,$rs = whether or not a resultset is expected,$newid = whether or not to retrieve the new ID value; // $onedimensionkey = key required to convert array into simple one dimensional array function db_query($query,$params,$rs = true,$newid = false,$onedimensionkey = false) { $link = MysqLi_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME); if (!$link) { print 'Error connecting to MysqL Server. Errorcode: ' . MysqLi_connect_error(); exit; } // Prepare the query and split the parameters array into bound values if ($sql_stmt = MysqLi_prepare($link,$query)) { if ($params) { $types = ''; $new_params = array(); $params_ref = array(); // Split the params array into types string and parameters sub-array foreach ($params as $param) { $types .= $param['type']; $new_params[] = $param['value']; } // Cycle the new parameters array to make it an array by reference foreach ($new_params as $key => $parameter) { $params_ref[] = &$new_params[$key]; } call_user_func_array('MysqLi_stmt_bind_param',array_merge(array($sql_stmt,$types),$params_ref)); } } else { print 'Error: ' . MysqLi_error($link); exit(); } // Execute the query MysqLi_stmt_execute($sql_stmt); // If there are results to retrive,do so if ($rs) { $results = array(); $rows = array(); $row = array(); stmt_bind_assoc($sql_stmt,$results); while (MysqLi_stmt_fetch($sql_stmt)) { foreach ($results as $key => $value) { $row[$key] = $value; } $rows[] = $row; } if ($onedimensionkey) { $i = 0; foreach ($rows as $row) { $simplearray[$i] = $row[$onedimensionkey]; $i++; } return $simplearray; } else { return $rows; } } // If there are no results but we need the new ID,return it elseif ($newid) { return MysqLi_insert_id($link); } // Close objects MysqLi_stmt_close($sql_stmt); MysqLi_close($link); }
根据我发布的链接,有一个解决方法涉及完成任务的顺序,但要么我以完全不同的方式处理我的查询,要么我只是不理解重要的事情.
感谢任何能提供帮助的人!
编辑:感谢Corina的回答,我已经解决了这个问题 – 对于遇到此问题的任何其他人,您只需要在MysqL_stmt_execute命令之后添加以下内容:
// Execute the query MysqLi_stmt_execute($sql_stmt); // Store results MysqLi_stmt_store_result($sql_stmt);
我设法通过在绑定数据之前调用
mysqli_stmt_store_result来解决相同的问题.
有人遇到同样的问题并在php.net website上分享了答案:
Apparently,if you have longtext present,you HAVE to call
store_result before using bind_result.07002