首页 文章

通过php与pdo连接到mysql

提问于
浏览
0

我正在使用php并尝试使用pdo Build 与mysql的连接 . 我已经读过我必须首先进行pdo扩展(不知道我是否做过,也不知道如何检查它) . 我已经将phpmyadmin的config.inc.php文件更改为大声外部连接 . 看起来像这样:

<?php

/* Servers configuration */
$i = 0;

/* Server: localhost [1] */
$i++;
$cfg['Servers'][$i]['verbose'] = 'localhost';
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['port'] = '8000';
$cfg['Servers'][$i]['socket'] = '';
$cfg['Servers'][$i]['connect_type'] = 'http';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['auth_type'] = 'http';  ////I've also try with config here
$cfg['Servers'][$i]['user'] = 'xxxx';
$cfg['Servers'][$i]['password'] = 'xxxxx';
$cfg['Servers'][$i]['AllowNoPasswordRoot'] = true;

?>

我的php文件是这样的:

function connect() {
    global $pdo;
    $pdo = new PDO("mysql:host=localhost:8000;dbname=sakila", "xxxx", "xxxxxx");
}

function get_actors_by_last_name($letter){
    global $pdo;

    $stmt = $pdo->prepare('
        SELECT actor_id, first_name, get_actors_by_last_name
        from  actor
        where first_name like :letter
        limit 50');


    $stmt->execute(  array(':letter' => $letter . '%' ));


    return $stmt->fetchAll( PDO::FETCH_OBJ);

}

?>

页面是一个简单的表单,假设根据名称的第一个字母显示actor的名称 . 每次你尝试在加载时使页面库存工作,而不给我任何东西,不是问题或例外 . 如果我更改主机参数并将使用我的mysql的主机名值设置为这样(在实际代码中为真正的主机值):

$pdo = new PDO("mysql:host=mysql.value;dbname=sakila", "xxxx", "xxxxxx");

我收到一条消息:

致命错误:未捕获异常'PDOException',消息'SQLSTATE [HY000] [1130]主机'xxxx'不允许在/Users/josecarro/Desktop/jquery/ajax/dataBase/functions.php中连接到此MySQL服务器: 13堆栈跟踪:#0 /Users/josecarro/Desktop/jquery/ajax/dataBase/functions.php(13):PDO - > __ construct('mysql:host = MacB ...','xxxx','xxxx', NULL)#1 /Users/josecarro/Desktop/jquery/ajax/dataBase/index.php(7):connect()#2 抛出/Users/josecarro/Desktop/jquery/ajax/dataBase/functions.php在第13行

这让我相信实际上pdo扩展正在发挥作用 . 要打开服务器并在我的文件所在目录的终端上使用此行:

open http://localhost:8000 && python -m SimpleHTTPServer

并在同一个目录我开始PHP与

php -S localhost:8000

我不知道这个问题是什么 . 虽然我相信我有pdo我已经尝试用phpbrew安装它(以防万一),安装另一个php版本(7.0.7) . 但是当我尝试安装扩展程序时,我得到了库存

phpbrew ext install pdo-mysql

其实我真的不知道我到底在做什么 . 我在php文档中看到你必须像这样编译:

--with-pdo-mysql[=DIR]

我不知道这意味着什么...我很丢失,你可以看到 . 我认为它可能比我想象的要容易,但我不知道该怎么做,要改变什么 . 请帮忙!!!

1 回答

  • 0

    我在这里做了一些更改,而不是将您的PDO作为一个函数,创建一个名为'connection.php'的新页面,并将其包含在您需要连接的任何页面上 . EX:

    <?php
        try{
        $pdo= new PDO("mysql:dbname=NAME; host=localhost; charset=utf8", "username", "password");
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }catch(PDOException $e)
    {
    echo "Error: " . $e->getMessage();
    }
    
        ?>
    

    要包含该页面:

    include('connection.php');
    

    要么

    require_once('connection.php');
    

    更改

    return $stmt->fetchAll( PDO::FETCH_OBJ);
    

    $result[] = $stmt->fetchAll(PDO::FETCH_ASSOC);
    

    然后在foreach循环中迭代每个结果,具体取决于在结果集之前有多少外部数组,您可能需要1-2个foreach循环 .

    EX:

    foreach($result as subResult){
        foreach($subresult as $rows){
            echo $rows['actor_id'];
            //or do a var_dump($rows);
        }
    
    }
    

相关问题