简体   繁体   English

为什么服务器拒绝我的PDO查询?

[英]Why is the server rejecting my PDO query?

I've been trying to write a simple search and display site with a drop down menu, I've tried using mysqli and PDO and I'm not getting the results. 我一直在尝试使用下拉菜单编写一个简单的搜索和显示站点,我曾尝试使用mysqli和PDO,但没有得到结果。 I've just checked the server log in cpanel and found that the access has been denied to the results page. 我刚刚检查了cpanel中的服务器日志,发现访问结果页面已被拒绝。 The access must be all right for the drop down page as it is populating the drop down list. 由于下拉页面正在填充下拉列表,因此访问权限必须是正确的。 I can't see why it is being stopped. 我看不到为什么它被停止了。 The server is running PHP 5.3.27 and MYSQL 5.5.36. 服务器正在运行PHP 5.3.27和MYSQL 5.5.36。

This is the drop down. 这是下拉列表。

 <form action="search3.php" method="post" >

<?php

    $mysqli = new mysqli('localhost','user','password','engraved_stamps');
    if ($mysqli->connect_error) {
    die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
    }

    $query = "SELECT DISTINCT Country FROM engravers ORDER BY Country";
    $result = $mysqli->query($query);

    ?>
    <select >
    <?php

       while ($row = $result->fetch_assoc()) {
         echo "<option value=\"{$row['Country']}\">";
         echo $row['Country'];
         echo "</option>";
    }
    $mysqli->close();
    ?>
      </select>

              <input type="submit" name="dropdown" />   
              </form>

This is the results page (action for restul) (called Search3.php) 这是结果页(针对restul的操作)(称为Search3.php)

    <?php

    $hostname = "localhost";
    $user = "user";
    $password = "password";
    $connection = mysqli_connect($hostname, $user, $password,);
    if(!$connection){
    echo"Could not connect to server";
    };
    mysqli_select_db($connection,'engraved_stamps');
    if(!mysqli_select_db($connection,'engraved_stamps')){
    echo"could not connect to database";
    };

    if(isset($_POST['dropdown'])){
    $Country = $_POST['dropdown'];
    }else{
    $Country = "none";
    }
    $sql= "SELECT * FROM engravers WHERE Country = :Country";
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':Country', $Country, PDO::PARAM_STR);
    $stmt->execute();
    $total = $stmt->rowCount();

    while ($row = $stmt->fetchObject()) {
    echo $row->Country;
    }

    $mysqli->close();

        ?>

I think PDO and mysqli are different PHP extensions. 我认为PDO和mysqli是不同的PHP扩展。 You should choose only one thing to deal with your work. 您应该只选择一件事情来处理您的工作。

eg, you can use mysqli only, or PDO. 例如,您只能使用mysqli或PDO。

For more information about PDO, see: http://php.net/manual/en/pdo.construct.php 有关PDO的更多信息,请参见: http : //php.net/manual/zh/pdo.construct.php

You are mixing two entirely different database extensions: 您正在混合两个完全不同的数据库扩展:

  1. MySQL Improved Extension (aka mysqli ): MySQL改进的扩展名 (又名mysqli ):

     $mysqli = new mysqli(...) ^^^^^^ 
  2. PHP Data Objects (aka PDO ): PHP数据对象 (又名PDO ):

     $stmt->bindParam(':Country', $Country, PDO::PARAM_STR); ^^^ 

Despite your question title and tags, your code seems to use mysqli entirely. 尽管您有问题标题和标签,但是您的代码似乎完全使用了mysqli。 You'll just need to get rid of the PDO bits. 您只需要摆脱PDO位。

You're also using some undefined variables: 您还使用了一些未定义的变量:

$stmt = $pdo->prepare($sql);
        ^^^^

You're apparently not getting the corresponding error messages, which suggests you haven't configured your PHP development box to do so. 您显然没有收到相应的错误消息,这表明您尚未配置PHP开发工具。 As soon as you do it, you'll be possibly warned of some other issues. 一旦这样做,您可能会被警告其他问题。

<?php
try {
    //here add user and password and database please be sure u have used correct one
$dbh = new PDO("mysql:host=localhost;dbname=engraved_stamps",'root','');
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>

<?php

$STM = $dbh->prepare("SELECT * FROM engravers WHERE Country = :Country");
 if(isset($_POST['dropdown'])){
    $Country = $_POST['dropdown'];
    }else{
    $Country = "name";//here you can assing % too for value of any if u need tell me to recode, NOTE here now the value u have assign is (name) is the record value on ur engravers table for column of Country.
    }


$STM->bindParam(':Country', $Country, PDO::PARAM_STR);
$STM->execute();                    
$row= $STM->fetchAll();
 if(count($row)){
    foreach($row as $data){
        echo $data['Country'] . "<br />";// here country is the column of ur table u can echo other columns too
    }
 }else {
    echo 'no row found';
 }

?> 

Sorry abas_rafiq. 对不起abas_rafiq。 the comments only allow a certain number of characters. 注释仅允许一定数量的字符。 Here is the structure: CREATE TABLE IF NOT EXISTS engravers ( Key int(10) NOT NULL AUTO_INCREMENT, StampImages text NOT NULL, Images text NOT NULL, Country text NOT NULL, Year int(4) NOT NULL, Description text NOT NULL, SGNumber text NOT NULL, ScottNumber text NOT NULL, Engraver1Surname text NOT NULL, Engraver1OtherNames text NOT NULL, Engraver2Surname text NOT NULL, Engraver2OtherNames text NOT NULL, Engraver3Surname text NOT NULL, Engraver3OtherNames text NOT NULL, Designer1Surname text NOT NULL, Designer1OtherNames text NOT NULL, Designer2Surname text NOT NULL, Designer2OtherNames text NOT NULL, Printer text NOT NULL, Notes text NOT NULL, PRIMARY KEY ( Key ), UNIQUE KEY Key ( Key ) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=91 ; 结构如下:如果不存在engravers创建表( Key int(10)NOT NULL AUTO_INCREMENT, StampImages文本NOT NULL, Images文本NOT NULL, Country文本NOT NULL, Year int(4)NOT NULL, Description文本NOT NULL, SGNumber文字NOT NULL, ScottNumber文字NOT NULL, Engraver1Surname文字NOT NULL, Engraver1OtherNames文字NOT NULL, Engraver2Surname文字NOT NULL, Engraver2OtherNames文字NOT NULL, Engraver3Surname文字NOT NULL, Engraver3OtherNames文字NOT NULL, Designer1Surname文字NOT NULL, Designer1OtherNames文字NOT NULL, Designer2Surname文本NOT NULL, Designer2OtherNames文本NOT NULL, Printer文本NOT NULL, Notes文本NOT NULL,主键( Key ),唯一键KeyKey ))ENGINE = MyISAM DEFAULT CHARSET = latin1 AUTO_INCREMENT = 91; They were not all "Not Null" when I set it up but I see they are now. 当我设置它们时,它们都不都是“ Not Null”,但我现在看到了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM