Help with PDO sql query

I am attempting to convert my code to use PDO, but am running into issues when trying to display the results from a query constructed from a webform. I have obviously done something wrong as no results are displayed. Any help would be appreciate it. I assume it has something to do with the prepared statements.

Here is my main form.php page, where I have the user input the hostname value to be used in my query-
[php]

Enter Servername? [/php]

Here is the main php where I make the connection to the database, run the query and try to display the results.
[php]<?php

$hostname = $_POST[ ‘hostname’];

$user = ‘public’;
$pass = ‘’;
$db = new PDO (‘mysql:host=localhost;dbname=Inventory’, $user, $pass );

$sql= “SELECT * FROM serverlist WHERE hostname = :hostname”;
$stmt = $pdo->prepare($sql);
$stmt->bindParam(’:hostname’, $hostname, PDO::PARAM_STR);
$stmt->execute();

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

?>[/php]

I really don’t know what you are trying to do? You have a webform, yet you are attempting to output from PDO? I’m guessing you want to edit this information? Anyways, reply back in what you are trying to do. I’m sure someone will help you out. In the meantime I spruce up you form a little. :stuck_out_tongue:

[code]

My Webform .container { box-sizing:border-box; width: 500px; height: 700px; padding: 10px; } fieldset { border: 2px solid orange; padding: 20px; } legend { color : orange; } textarea { resize: none; border: 2px solid orange; width: 420px; height: 400px; padding: 10px; } input[type=submit] { margin-left: 360px; } My Webform My TextArea

[/code]

Thanks for your quick reply. Ultimately what I am trying to do is take the values from the textarea on the webform and then use them in a select statement, and the display the results. So for example select * from serverlist where hostname = “the value from the textarea” I am using textarea as I will eventually like to include multiple values so it would be something like select * from serverlist where hostname in (‘value1’, ‘value2’,) and so forth.

I have this working using the old mysql_connect way, but it was suggest not to use this way as it was vulnerable to sql injection. Let me know if I need to explain in more detail.

Thanks

Sounds like you may have a bad database design. Your host names should not be in more than one column. Post your database schema for us to look at it.

Sorry my explanation and use of terms is probably off… The hostnames are in the same column. Below is the layout of the database

CREATE TABLE IF NOT EXISTS `serverlist` ( `id` int(4) NOT NULL DEFAULT '0', `hostid` int(4) DEFAULT NULL, `domainhostname` varchar(30) DEFAULT NULL, `hostname` varchar(15) DEFAULT NULL, `ipaddress` varchar(15) DEFAULT NULL, `console` varchar(8) DEFAULT NULL, PRIMARY KEY (`id`)

It may help you to download my PDO bump start database in my signature link.

Sponsor our Newsletter | Privacy Policy | Terms of Service