在php中使用pdo创建数据库 [英] create database with pdo in php

查看:48
本文介绍了在php中使用pdo创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在不使用绑定参数的情况下创建数据库时,它运行完美.

When I create database without using bind param, it works perfectly.

$login = 'root';
$password = 'root';
$dsn = "mysql:host=localhost";

$opt = array(
// any occurring errors wil be thrown as PDOException
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
// an SQL command to execute when connecting
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"
);

// Making a new PDO conenction.
$conn = new PDO($dsn, $login, $password,$opt);

$db = $conn->prepare( 'CREATE SCHEMA IF NOT EXISTS account');

$db->execute();      
// End Connection and Return to other files.

但是在应用bindParam之后,它无法正常工作.

But after applying the bindParam it is not working properly.

$db = $conn->prepare( 'CREATE SCHEMA IF NOT EXISTS ?');
$db->bindParam(1,`account`);
$db->execute();  //line 18

显示错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1' in /Applications/MAMP/htdocs/create/index.php:18 Stack trace: #0 /Applications/MAMP/htdocs/create/index.php(18): PDOStatement->execute() #1 {main} thrown in /Applications/MAMP/htdocs/create/index.php on line 18

更新:

<?php
$login = 'root'; // Login username of server host.
$password = 'root'; // Password of server host.
$dsn = "mysql:host=localhost"; // Set up a DSN for connection with Database Frat.
$dbb = 'sale';
$opt = array(
// any occurring errors wil be thrown as PDOException
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
// an SQL command to execute when connecting
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"
);

// Making a new PDO conenction.
$conn = new PDO($dsn, $login, $password,$opt);

$db = $conn->prepare( 'CREATE SCHEMA IF NOT EXISTS ?');
$db->bindParam(1,'$dbb'); //line 17
$db->execute();          

?>

显示错误: 无法通过引用在第17行传递参数2

it showing error: Cannot pass parameter 2 by reference on line 17

推荐答案

这里有两个大问题.首先是次要的.这些代码行将永远无法工作:

There are two big problems here. The first is minor. These lines of code will never work:

$db->bindParam(1,`account`);
$db->bindParam(1,'$dbb'); //line 17

这是因为他们俩都试图将bindParam作为 string 来调用.这是不可能的. bindParam需要引用变量.这就是为什么会出现无法通过引用传递参数2"错误的原因:您只能通过引用传递变量.

This is because both of them are attempting to call bindParam as a string. This is impossible. bindParam needs a reference to a variable. This is why you get a "cannot pass parameter 2 by reference" error: you can only pass variables by reference.

但是,这些方法中的任何一个都可以工作:

Either of these, however, would work:

$db->bindParam(1, $dbb); // call bindParam on a variable
$db->bindValue(1, 'account'); // call bindValue on a string literal

但是,更根本的问题是您对准备好的语句的理解.准备语句的思想是将字符串 not 简单地替换为另一个字符串.从根本上讲,它是关于查询结构与数据的分离.表名被认为是查询结构的一部分,而不是数据的一部分.您需要将表名放在原始查询中.您的第一个代码就是做到这一点的方法.

The more fundamental problem, however, is your understanding of prepared statements. The idea of prepared statements is not simple substitution of strings into another string. It is fundamentally about separation of the structure of the query from the data. The name of a table is considered part of the structure of the query, not part of the data. You need to put the table name in the original query. Your first code is the way to do it.

$db = $conn->prepare( 'CREATE SCHEMA IF NOT EXISTS account');

这篇关于在php中使用pdo创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆