我正在尝试使用 prepare 语句创建最佳全局选择查询,一切正常,除了我收到警告:
警告:mysqli_stmt_bind_result():绑定(bind)变量的数量与准备语句中的字段数量不匹配
独特的全局选择功能
function querysp($selectquery, $type_bind, $colval) {
global $db;
$stmt = $db->prepare($selectquery);
$stmt->bind_param($type_bind,$colval);
$stmt->execute();
mysqli_stmt_bind_result($stmt, $colval);
$arr = array();
if ($stmt) {
while ($result = mysqli_stmt_fetch($stmt)) {
array_push($arr, $result);
}
}
return $arr;
}
示例使用:
$select = "SELECT * from advertising WHERE status = ?";
$status = 1;
$advertising = querysp($select, 'i', $status);
foreach ($advertising as $ad) {
$banner[] = $ad['banner'];
$bannercode[] = $ad['bannercode'];
$location[] = $ad['location'];
$status = $ad['status'];
}
我错过了什么?抱歉,没有得到准确的准备,也在 SO 和 Google 上进行了检查,但无法解决这个问题。
编辑 2
我更改了选择查询,因为我将绑定(bind)参数的类型从 b(我认为是 bool 值)更改为 i,但仍然出现错误。
编辑 3 - 当前版本 - 仍然出现相同的错误:
$select = "SELECT banner, bannercode, location, status from advertising WHERE status = ?";
$status = 1;
$advertising = querysp($select, 'i', $status);
foreach ($advertising as $ad) {
$banner[] = $ad['banner'];
$bannercode[] = $ad['bannercode'];
$location[] = $ad['location'];
$status = $ad['status'];
}
和函数
function querysp($selectquery, $type_bind, $colval) {
global $db;
$stmt = $db->prepare($selectquery);
$stmt->bind_param($type_bind,$colval);
$stmt->execute();
$stmt->bind_result($result);
$arr = array();
while($stmt->fetch()) {
$arr[] = $result;
}
$stmt->close();
return $arr;
}
最佳答案
花了一些时间,但这是我准备好的语句的版本,它是一堵墙,但它几乎捕获了可能出现的大多数错误。我试着在这里和那里添加一些文档来解释发生了什么。只需逐步阅读它,您就有望理解发生了什么。如果有任何不清楚的地方,请提出问题。
要使用下面发布的类,请执行此操作。
$query = "SELECT ? FROM ?"; // can be any query
$params = array($param1, $param2); //must equal to ammount of "?" in query.
//an error will occur if $param1 or 2 is not of the type string, int,
//bool or double, it can however be a double like this 2,1 instead of 2.1
$db = new databaseHandler();
$result = $db->runQuery($query, $params);
//or for short runQuery("SELECT * FROM *" , array());
if($result !== false){
while($row = mysqli_fetch_array($result){
$column1 = $row['columnname'];
//just do with the result what you want.
}
}else{
echo "error occured";
}
这是能够处理数据库交互的类。不要以为您需要按照自己感觉最好的方式来设置连接。您可以对此运行所有类型的查询。
class databaseHandler{
private $x = ""; //required
//$query = the query, $params = array with params can be empty.
public function runQuery($query, Array $params){
if($this->checkparams($params, $query)){
//starts and returns the database connection
$con = startConnection(); //<--- CHANGE THIS SO IT WORKS FOR YOU
if($stmt = $con->prepare($query)){
//obtains type of params if there are any.
if(count($params) < 0){
$type = "";
$i=0;
foreach($params as $par){
$par = $this->checktype($par);
$params[$i] = $par;
$type = $this->setType($type);
if($type === false){
echo "type error occured"
return false;
}
$i++;
}
//sets $type on the first spot of the array.
array_unshift($params, $type)
//binds the params
call_user_func_array(array($stmt, 'bind_param'), $this->refValues($params));
}
$stmt->execute();
$result - $stmt->get_result();
stmt->close();
return $result; // return the result of the query.
}else{
echo "error occured, bad connection";
return false;
}else{
echo "params dont match prepared statement";
return false;
}
}
//checks if params are equal to prepared statement requirements.
checkparams($params, $query){
//counts occurences of ? in query.
$count = substr_count($q, "?");
$i = count($params);
if($count == $i){
return true;
}else{
return false;
}
}
//Checks the type of a param
public function checktype($par){
$this->x = gettype($par);
if($this->x == "string"){
$npar = str_replace(",", ".", $par);
if(is_numeric($npar)){
$this->x = "integer";
if(strpos($npar, ".")){
$this->x="double";
return $npar;
}
}
}
return $par;
}
//sets/adds to the type.
public function setType($type){
//$this->x from checktype;
switch($this->x){
case "string":
$type = $type."s";
break;
case "integer":
$type = $type."i";
break;
case "double":
$type = $type."d";
break;
case "boolean":
$type = $type."b";
break;
case "NULL":
$type = $type."s";
default:
return false;
}
return $type;
}
//This function exist to resolve a few version errors in php.
//not sure what it does exactly, but it resolved some errors I had in the past.
function refValues($arr){
if(strnatcmp(phpversion(),'5.3') >= 0){
$refs = array();
foreach($arr as $key => $value)
$refs[$key] = &$arr[$key];
return $refs;
}
return $arr;
}
}
}
这里发生的几乎是一组执行查询的检查,如果出现任何错误,它返回 false 如果没有错误,它返回查询的结果,即使结果为空。也可以不在一个类中完成所有这些,尽管这会使 $x 全局化。我认为如果您修改某些内容以使其最适合您的应用程序,那将是最好的。比如错误处理/消息变量名等。
此代码唯一不能保护您的是查询中的错误。
编辑 ---- 我确实发现了这段代码也没有防止的东西,插入 NULL 值。我更改了此代码以防止插入 NULL 值,它们将作为字符串类型插入。数据库将看到它的 NULL 并将其作为 NULL 值插入。
只是不要尝试插入对象或空值,因为无论如何那都是无用的。
关于php - 添加准备语句会导致警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23995711/
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
我试图使用yard记录一些Ruby代码,尽管我所做的正是所描述的here或here#@param[Integer]thenumberoftrials(>=0)#@param[Float]successprobabilityineachtrialdefinitialize(n,p)#initialize...end虽然我仍然得到这个奇怪的错误@paramtaghasunknownparametername:the@paramtaghasunknownparametername:success然后生成的html看起来很奇怪。我称yard为:$yarddoc-mmarkdown我做错了什么?
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA
最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru
当谈到运行时自省(introspection)和动态代码生成时,我认为ruby没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资
简而言之错误:NOTE:Gem::SourceIndex#add_specisdeprecated,useSpecification.add_spec.Itwillberemovedonorafter2011-11-01.Gem::SourceIndex#add_speccalledfrom/opt/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:91./opt/local/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails/gem_dependency.rb:275:in`==':und
我注意到类定义,如果我打开classMyClass,并在不覆盖的情况下添加一些东西我仍然得到了之前定义的原始方法。添加的新语句扩充了现有语句。但是对于方法定义,我仍然想要与类定义相同的行为,但是当我打开defmy_method时似乎,def中的现有语句和end被覆盖了,我需要重写一遍。那么有什么方法可以使方法定义的行为与定义相同,类似于super,但不一定是子类? 最佳答案 我想您正在寻找alias_method:classAalias_method:old_func,:funcdeffuncold_func#similartoca
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司