pdo查询alter table哪里的列名不存在?

时间:2013-12-30 07:48:36

标签: php mysql pdo

 <?php
   $month=$_SESSION['month'];
    $colname=$_SESSION['colname'];
require('connect.php');
global $pdo;
 $stmt=$pdo->prepare('SHOW COLUMNS FROM `selections` LIKE ":s%"');
$stmt=bindParam(':s',$colname);$stmt->execute();$row =$stmt->fetch()?true:false;
if($row==false){$sql=$pdo->prepare("ALTER TABLE  `$month` ADD  `$colname` VARCHAR( 120)NOT NULL DEFAULT 'absent'");
$sql->execute();}else{die("error".print_r($sql>errorinfo()));
}
 ?>

代码错误调用未定义的函数bindParam(); bindparm错误 这里$ month是月份由用户动态选择,$ colname也由用户选择

<?php
$month=$_SESSION['month'];
$colname=$_SESSION['colname'];
$tot='present';
require('connect.php');
  global $pdo;
$stmt=$pdo->prepare("UPDATE `$month` SET `$colname`=:a WHERE roll =:foo");
 $stmt=bindparam(':a',$tot);
  foreach( $value as $value)
           {
      $stmt>bindParam(':foo',$value);
       $stmt->execute();
          }
    if($stmt==false)
   {    
     die("error".print_r($stmt->errorinfo()));
      } ?>

2 个答案:

答案 0 :(得分:2)

错误1

你可能意味着:

$stmt->bindParam(':s',$colname);

错误2

此外,您还没有按session_start()

开始会话

错误3

应该是:

die("error".print_r($sql->errorinfo(), true));

错误4

您可能需要:

$stmt->bindParam(':foo',$value);

而不是:

$stmt>bindParam(':foo',$value);
顺便说一下,你更喜欢用单行编写代码吗?

答案 1 :(得分:-1)

试试这个:

session_start();
$month=$_SESSION['month'];
$colname=$_SESSION['colname'];
require('connect.php');
global $pdo;
$stmt=$pdo->prepare('SHOW COLUMNS FROM `selections` LIKE :s');
$query->bindValue(":s", $stmt);
$stmt->execute();
$row =$stmt->fetch()?true:false;
if($row==false){
  $sql=$pdo->prepare("ALTER TABLE  `$month` ADD  `$colname` VARCHAR( 120 )NOT NULL DEFAULT 'absent'");
  $sql->execute();
}else{
  die("error".print_r($sql>errorinfo()));
}

第二个错误:

$month=$_SESSION['month'];
$colname=$_SESSION['colname'];
$tot='present';
require('connect.php');
global $pdo;
$stmt=$pdo->prepare("UPDATE `$month` SET `$colname`=:a WHERE roll =:foo");
$stmt=bindparam(':a',$tot);
 //I don't know what are in the $value
foreach( $value as $v){
    $stmt->bindParam(':foo',$v);
    if( !$stmt->execute())
    {
        die("error".print_r($stmt->errorinfo()));
    }
}