Need some help with PHP programing...

User 2623310 Photo


Ambassador
282 posts

Hi All,

I need some help with some programing. I have created a page for pigeon breeders. On the top of that page is a map of the US States. If you click on a state it will bring up a list of breeders for that state.

Now I would like to create a page to show the following:
AL
(breeders name)
AK
(breeders name)

I can get the page show the 50 States but I can't get the breeders name to show from MySQL database.

I hope I have put this so everyone can understand what trying to do.

Thanks for any or all help in advance. :)


User 187934 Photo


Senior Advisor
20,245 posts

pdo_connect.php
<?php
$database = 'mysql: myHost =localhost;dbname=database-name-goes-here;charset=utf8';
$username = 'username';
$password = 'password';
try {
$pdo = new PDO($database, $username, $password);
$pdo ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo ->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>


<?php
$state = $_POST['state'];
include('pdo_connect.php');
try{
$query2 = $pdo->prepare("SELECT * FROM mytable_name WHERE state = :state");
$data2 = array(
':state' => $state
);
$query2->execute($data2);
foreach ($query2->fetchAll(PDO::FETCH_ASSOC) as $row) {
$breeders_name = $row['breeders_name'];
$othercolumn = $row['othercolumn'];
}
}
catch(PDOException $e){

}
echo $breeders_name;
?>



I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 2623310 Photo


Ambassador
282 posts

Hi Eric,

I have couple of questions for you.

The first question is pdo_connect.php will be a separate php file?

The second question is where do I put the second half of the code?

The page is HTML code Will be able to do this
AL
<?php
$state = $_POST['state'];
include('pdo_connect.php');
try{
$query2 = $pdo->prepare("SELECT * FROM mytable_name WHERE state = :state");
$data2 = array(
':state' => $state
);
$query2->execute($data2);
foreach ($query2->fetchAll(PDO::FETCH_ASSOC) as $row) {
$breeders_name = $row['breeders_name'];
$othercolumn = $row['othercolumn'];
}
}
catch(PDOException $e){

}
echo $breeders_name;
?>
AK
<?php
$state = $_POST['state'];
include('pdo_connect.php');
try{
$query2 = $pdo->prepare("SELECT * FROM mytable_name WHERE state = :state");
$data2 = array(
':state' => $state
);
$query2->execute($data2);
foreach ($query2->fetchAll(PDO::FETCH_ASSOC) as $row) {
$breeders_name = $row['breeders_name'];
$othercolumn = $row['othercolumn'];
}
}
catch(PDOException $e){

}
echo $breeders_name;
?>
So on

Do I have to replace anything in the second half of the code. It looks like to replace line from this

{
$query2 = $pdo->prepare("SELECT * FROM mytable_name WHERE state = :state");

to this

{
$query2 = $pdo->prepare("SELECT * FROM breeder_directory WHERE state = :state");


Thanks for the help :D
User 187934 Photo


Senior Advisor
20,245 posts

It was only a quick example as I don't know all the info that's needed. Yes, the pdo_connect is a separate file that is referenced with the include. This allows you a simple quick way of placing it on other pages. You can either change your page to a .php extension or make sure your server is setup to parse php inside html.
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 2623310 Photo


Ambassador
282 posts

Thanks for the help!

Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.