使用pdo

时间:2015-07-24 10:21:25

标签: php sql-server pdo

我无法使用pdo php在表(MSSQL)中插入值。我收到消息说连接已建立,但查询不起作用。我从来没有使用过MSSQL服务器我不确定这个查询是否合适。

$database = "db";
$server = "xxx\SQLEXPRESS";
$conn = array( "Database"=>"db", "UID"=>"user", "PWD"=>"xxx" , "CharacterSet" => "UTF-8");
if( $conn ) {
     echo "connection established";
}else{
     echo "Connection could not be established.";
     die( print_r( sqlsrv_errors(), true));
 }

 // insert values into table, 
 // variables are defined above didnt write them here

$query = $conn->prepare("INSERT INTO dbo.FKNarudzbaKupacaStavke(IdFirma, VrstaDokumenta, BrojDokumenta, BrojDokumentaKroz, 
                        DatumDokumenta, IdKupac, VrstaCijene, NacinPlacanja, DatumZadnjeAkcije, Status, StatusArhive, StatusIzmjene, 
                        StatusStampe, VrstaFakture) VALUES(:IdFirma, :VrstaDokumenta, :BrojDokumenta, :BrojDokumentaKroz, :DatumDokumenta, :IdKupac, :VrstaCijene, :NacinPlacanja, 
                        :DatumZadnjeAkcije, :Status, :StatusArhive, :StatusIzmjene, :StatusStampe, :VrstaFakture)");

    $query->bindParam(':IdFirma',$IdFirma);
    $query->bindParam(':VrstaDokumenta',$VrstaDokumenta);
    $query->bindParam(':BrojDokumenta',$BrojDokumenta);
    $query->bindParam(':BrojDokumentaKroz',$BrojDokumentaKroz);
    $query->bindParam(':DatumDokumenta',$DatumDokumenta);
    $query->bindParam(':IdKupac',$IdKupac);
    $query->bindParam(':VrstaCijene',$VrstaCijene);
    $query->bindParam(':NacinPlacanja',$NacinPlacanja);
    $query->bindParam(':DatumZadnjeAkcije',$DatumZadnjeAkcije);
    $query->bindParam(':Status',$Status);
    $query->bindParam(':StatusArhive',$StatusArhive);
    $query->bindParam(':StatusIzmjene',$StatusIzmjene);
    $query->bindParam(':StatusStampe',$StatusStampe);
    $query->bindParam(':VrstaFakture',$VrstaFakture);
    $query->execute();

我收到了这个错误:

  

致命错误:在非对象上调用成员函数prepare()。

感谢任何帮助或建议!

2 个答案:

答案 0 :(得分:1)

你写的方式, ' import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import java.util.UUID; public class CreateSampleDocuments { String text=""; String str=""; Map<Integer, File> selection = new HashMap<Integer, File>(1000); public void readSampleFiles(File files[])throws IOException { while (selection.size() <= files.length/5) { int value = (int)Math.round(Math.random() * files.length); if (!selection.containsKey(value)) { selection.put(value, files[value]); } } for (File file : selection.values()) { if(file.isFile()) { String name = UUID.randomUUID().toString(); PrintWriter pw=new PrintWriter("/home/gauge/Documents/Docs/Misc"+"/"+name); BufferedReader br=new BufferedReader(new FileReader(file)); while((text=br.readLine())!=null) { pw.write(text+"\n"); pw.flush(); } //System.out.println(file); } else if(file.isDirectory()) { } } } public static void main(String args[])throws IOException { File files[] = new File("/home/gauge/Documents/Docs/Filtered Documents/Orissa/TextFiles/Year1952").listFiles(); CreateSampleDocuments d=new CreateSampleDocuments(); d.readSampleFiles(files); } } 不是连接,它只是一个数组。

尝试连接数据库:

$conn

答案 1 :(得分:0)

您实际上没有在那里建立连接,请更改您的$ conn变量,如下所示

$conn = new PDO("mssql:host=".$server.";dbname=db", "user", "xxx");