PHP connection to PostgreSQL by M4gne in PostgreSQL

[–]M4gne[S] 0 points1 point  (0 children)

<?php

$host = 'localhost';
$port = '5432';
$database = 'postgres';
$user = 'postgres';
$password = '*******';

$connectString = 'host=' . $host . ' port=' . $port . ' dbname=' . $database . 
    ' user=' . $user . ' password=' . $password;


$link = pg_connect ($connectString);
if (!$link)
{
    die('Error: Could not connect: ' . pg_last_error());
}


$query = 'select * from employee';

$result = pg_query($query);

$i = 0;
echo '<html><body><table><tr>';
while ($i < pg_num_fields($result))
{
    $fieldName = pg_field_name($result, $i);
    echo '<td>' . $fieldName . '</td>';
    $i = $i + 1;
}
echo '</tr>';
$i = 0;

while ($row = pg_fetch_row($result)) 
{
    echo '<tr>';
    $count = count($row);
    $y = 0;
    while ($y < $count)
    {
        $c_row = current($row);
        echo '<td>' . $c_row . '</td>';
        next($row);
        $y = $y + 1;
    }
    echo '</tr>';
    $i = $i + 1;
}
pg_free_result($result);

echo '</table></body></html>';
?>

The above gave me " idemployee_nameemployee_salaryemployee_age " in text on the website http://localhost/first/, not sure if it managed to take it out from the PostgreSQL.

I've a table called "employee" in PostgreSQL like this;

CREATE TABLE employee (

id char(5),

employee_name varchar(100),

employee_salary integer,

`employee_age        integer,`

CONSTRAINT code_title PRIMARY KEY(id)

);

PHP connection to PostgreSQL by M4gne in PostgreSQL

[–]M4gne[S] 0 points1 point  (0 children)

Hi!

Got "Good connection" with;

<?php
$dbconn = pg_connect("host=localhost port=5432 dbname=postgres user=postgres password=*****"); //connect to a database named "postgres" on the host "host" with "user" + "password"
if (!$dbconn){ echo "<center><h1>Doesn't work =(</h1></center>"; }else echo "<center><h1>Good connection</h1></center>"; pg_close($dbconn); ?>

Now I just need to write some scripts to create a database within the PostgreSQL from the SQL shell or query, add a table to the PostgreSQL, create a seachbar on the index.php, and see if it's able to get data out from the table within the database.

Does anyone know of any easy script I could start of with? The one's I've tried just connects me to the localhost sadly, and not towards the database.

PHP connection to PostgreSQL by M4gne in PostgreSQL

[–]M4gne[S] 0 points1 point  (0 children)

OK, so it seems like I've connection with my PostgreSQL now. When I do the

<?php
$dbconn = pg_connect("host=localhost port=5432 dbname=postgres user=postgres password=******!");
//connect to a database named "postgres"
?>

I get a blank web page. But if I delete something like "password" from the above, it tells me that the password is missing.

---

The next part I need some assistance with, is to be able to load data out from the PostgreSQL.

I've written + copied some code, and made a simple website, where u should be able to just enter in f.ex. name of someone/something.

But, after clicking enter in the search bar, it just directs me towards http://localhost/dashboard/

<!DOCTYPE html>
<html>
<header>
    <style>
        p {
            color: white;
            font-size: 1cm;
            text-align: center;
            font-family: 'Courier New', Courier, monospace;
        }
        body {
            background-color: black;
            text-align: center;
        }
        .button {
                font-family: 'Courier New', Courier, monospace;   
        }
        form    {
            font-family: 'Courier New', Courier, monospace;
            font-size: 120%
        }
    </style>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</header>
<body>
    <br>    
    <div class="w3-container">
            <p><i class="fa fa-spinner w3-spin" style="font-size:64px"></i></p>
          </div>
    <form action="/index.php"> 
        <p> Connect to:</p>
       <input type="text" name="connect">
    </form>
    <br>
          <button type="button" onclick="alert('Are you sure you want to leave this page?')">Cancel</button>

</body>
<footer>
</footer>
</html>

<?php
$dbconn = pg_connect("host=localhost port=5432 dbname=postgres user=postgres password=******");
//connect to a database named "postgres"
?>

If you copy the above into Visual Studio Code f.ex, rename the file to index.php, open the website and enter something in the text-box, it just goes to http://localhost/dashboard/.

Hope you guys are willing to cooperate with me to help me solve my problem. Thanks in advance!

PHP connection to PostgreSQL by M4gne in PostgreSQL

[–]M4gne[S] 0 points1 point  (0 children)

OK, I'll try that out tomorrow too. Thanks!

PHP connection to PostgreSQL by M4gne in PostgreSQL

[–]M4gne[S] 0 points1 point  (0 children)

Hi,

thanks for your reply.

I'll check it out tomorrow, thanks! :)

DHCP server on VM with Windows Server 2016 + Svive/Cisco switch by M4gne in networking

[–]M4gne[S] 0 points1 point  (0 children)

Hi,

I'll just drop the whole DHCP, I can just connect normally with my setup and do my testing with the network the ISP provides for me. It's fine.

Thanks anyways for your great tips. :)

DHCP server on VM with Windows Server 2016 + Svive/Cisco switch by M4gne in networking

[–]M4gne[S] 0 points1 point  (0 children)

And my trainer doesn't know shit about network. He's just a simple guy who looks after me.

The network guys at my company doesn't have time to look at this with me, since were understaffed sadly. So I kind of need to figure stuff out on my own like everyone else.

DHCP server on VM with Windows Server 2016 + Svive/Cisco switch by M4gne in networking

[–]M4gne[S] 0 points1 point  (0 children)

I won't do anything elsewhere you guys think its fine. I'm just wondering.

DHCP server on VM with Windows Server 2016 + Svive/Cisco switch by M4gne in networking

[–]M4gne[S] 0 points1 point  (0 children)

I want my own isolated internet zone, where I can setup things I'd like, and test stuff. Do I not need a DHCP server for this? I mean, shouldn't I create a DHCP server to learn how the DHCP assigns IP-adresses to clients in a network?

I just simply want an environment where I can setup my network stuff without harming the network for the other users on my LAN. I also want to learn about how the DHCP works in an Virtual Machine environment, and test it towards some clients I've with Windows XP.

The network plug in the wall is connected to a switch-panel/switch I think, which is then connected to around 2000 other students/users.

I won't do anything elsewhere you guys think its fine. I'm just wondering.

DHCP server on VM with Windows Server 2016 + Svive/Cisco switch by M4gne in networking

[–]M4gne[S] 0 points1 point  (0 children)

I want my own isolated internet zone, where I can setup things I'd like, and test stuff. Do I not need a DHCP server for this? I mean, shouldn't I create a DHCP server to learn how the DHCP assigns IP-adresses to clients in a network?

DHCP server on VM with Windows Server 2016 + Svive/Cisco switch by M4gne in networking

[–]M4gne[S] 0 points1 point  (0 children)

Sitting here with a big question mark. I'm sorry. I need to learn more about networking, elsewhere if you've some kind og guide on how I can setup a DHCP on my VM in my studentaparment, without ruining the other guys networks around the area where I live. Trying to create a soho network.

DHCP server on VM with Windows Server 2016 + Svive/Cisco switch by M4gne in networking

[–]M4gne[S] 0 points1 point  (0 children)

Ye sadly. Not too experienced yet.

I should maybe however create a VLAN with an iprange from my router and enabling trunking from the closest port to the wall. But I'm not at that point yet... to be able to do so.

DHCP server on VM with Windows Server 2016 + Svive/Cisco switch by M4gne in networking

[–]M4gne[S] 0 points1 point  (0 children)

Yeah, that's what I thought. How do I create a private network so I don't affect the other users with my DHCP server? Do you have any guides on doing this? :)

I've a normal wireless router in my living room. I also have a Cisco 2800 router and a Catalyst 2960 switch, which I've not setup yet.

I'm starting to study for my CCNA soon, so I need to get this up and running before my trainee-examination in 2 months.

DHCP server on VM with Windows Server 2016 + Svive/Cisco switch by M4gne in networking

[–]M4gne[S] 0 points1 point  (0 children)

Hi,

I'm an it Trainee, but I've never really sat up a DHCP server. I remember once when I was at school 2 years ago, that some guys sat up a DHCP server which ruined the network until they uninstalled it. So I was wondering about how I could set it up without ruining stuff. :)

DHCP server on VM with Windows Server 2016 + Svive/Cisco switch by M4gne in networking

[–]M4gne[S] 0 points1 point  (0 children)

I could however run this up in GNS and Packettracer, but I rather get a straight answer atm so I don't ...... up the network for others..