Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何正确地从mysql迁移到PDO_Php_Mysql_Pdo - Fatal编程技术网

Php 如何正确地从mysql迁移到PDO

Php 如何正确地从mysql迁移到PDO,php,mysql,pdo,Php,Mysql,Pdo,更新: 这个问题已经回答了 我有一个简单的表单,用户可以在其中更新mysql数据库。我曾使用mysql\uquot/code>连接到数据库,但我知道pdo是一种更好的连接方式,因为mysql\uquot/code>被贬低了 以下是旧方法的完整形式,然后是新方法的完整形式。 新方法会产生错误 老办法: <?php $host = 'ip_address'; $user = 'user_name'; $password = 'password'; $link = mysql_connect

更新: 这个问题已经回答了 我有一个简单的表单,用户可以在其中更新mysql数据库。我曾使用
mysql\uquot/code>连接到数据库,但我知道pdo是一种更好的连接方式,因为
mysql\uquot/code>被贬低了

以下是旧方法的完整形式,然后是新方法的完整形式。 新方法会产生错误

老办法:

<?php

$host = 'ip_address';
$user = 'user_name';
$password = 'password';

$link = mysql_connect($host, $user, $password);

$selected = mysql_select_db('db_name', $link);

if(!isset($_POST['text-input']))

?>

<form method="post">
    %slice%
    <input type="submit" value="Submit" />
</form>
%[if !edit]%

<?php
%[repeat items]%
$form_input%id=repeatIndex% =  $_POST['element-%id=repeatIndex%'] ;
%[endrepeat]%

$query = 'INSERT INTO `table_name` (%[repeat items]%%[endif]%%html="Edit Me"%%[if !edit]%,%[endrepeat]%) VALUES (%[repeat items]%"' . $form_input%id=repeatIndex% . '",%[endrepeat]%);';
$query = preg_replace('/,\);/',');',$query);
$query = preg_replace('/,\) /',')',$query);
mysql_query($query);

?>
%[endif]%

我希望我已经提供了足够的信息。

解决方案如下:

<?php

//Connection vars
$host = '%id=server%';
$user = '%id=username%';
$password = '%id=password%';
$databasename = '%id=database%';

//This block establishes the connection
try{
    //
    $connectiondetails = "mysql:host={$host};dbname={$databasename}";
    $db = new PDO($connectiondetails , $user , $password);
    $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
}
catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

//Check if all the input fields of the form have been submitted
if(
    %[repeat items]%
    isset($_POST['element-%id=repeatIndex%'])&&
    %[endrepeat]%
    (true===true)
){


    //The variables to read the output
    %[repeat items]%
    $form_input%id=repeatIndex% = $_POST["element-%id=repeatIndex%"] ;
    %[endrepeat]%

    //DB
    global $db;     //must set this, otherwise can't access the db  
    $query =  'INSERT INTO `%id=table%` (%[endif]%%[repeat items]%%html="Edit Me"%%[if !edit]%,%[endif]%%[endrepeat]%%[if !edit]%)%[endif]% %[if !edit]%VALUES (%[repeat items]%"' . $form_input%id=repeatIndex% . '",%[endrepeat]%);';
    $query = preg_replace('/,\);/',');',$query);
    $query = preg_replace('/,\) /',')',$query);
    $db->query($query);
    //[DB]
}

?> 

您仍在使用PDO调用
mysql\u query()
。这两者不相容。使用
prepare()
execute()
query()
查看许多示例。尽管它是关于SQL注入的,但它为您提供了有关PDO的基本信息。
Warning: mysql_query() [function.mysql-query]: Access denied for user
'kuler'@'localhost' (using password: NO) in
/home/path_to/index.php on line 125

Warning: mysql_query() [function.mysql-query]: A link to the server could not be
established in /home/path_to/index.php on line 125
<?php

//Connection vars
$host = '%id=server%';
$user = '%id=username%';
$password = '%id=password%';
$databasename = '%id=database%';

//This block establishes the connection
try{
    //
    $connectiondetails = "mysql:host={$host};dbname={$databasename}";
    $db = new PDO($connectiondetails , $user , $password);
    $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
}
catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

//Check if all the input fields of the form have been submitted
if(
    %[repeat items]%
    isset($_POST['element-%id=repeatIndex%'])&&
    %[endrepeat]%
    (true===true)
){


    //The variables to read the output
    %[repeat items]%
    $form_input%id=repeatIndex% = $_POST["element-%id=repeatIndex%"] ;
    %[endrepeat]%

    //DB
    global $db;     //must set this, otherwise can't access the db  
    $query =  'INSERT INTO `%id=table%` (%[endif]%%[repeat items]%%html="Edit Me"%%[if !edit]%,%[endif]%%[endrepeat]%%[if !edit]%)%[endif]% %[if !edit]%VALUES (%[repeat items]%"' . $form_input%id=repeatIndex% . '",%[endrepeat]%);';
    $query = preg_replace('/,\);/',');',$query);
    $query = preg_replace('/,\) /',')',$query);
    $db->query($query);
    //[DB]
}

?>