1

I want to be able to format data from my website using a Go program. Right now my website is written in PHP and I am fetching data from an SQL table. My websites code is

<?php
include("connection.php");
$sql = mysqli_query($con, "SELECT * FROM talk");
while ($info = $sql->fetch_assoc()) {
    echo "Name: " . $info['name'];
    echo "<br>";
    echo "Message: " . $info['message'];
    echo "<br>";
}

And the page currently displays

Name: John
Message: My name is John
Name: Doe
Message: My name is Doe

I would like to be able to use a Go script to fetch the information of what the name and message is. I am open to changing the code for my website and I wrote it very simply to be able to give a brief understanding of what I am trying to do.

  • In Go you should do it in two steps: First: reading from the database, Second: Display the data on the template – Ahmed Dec 17 '21 at 00:42
  • @Ahmed Is it possible I could just read from the webpage or must I interact with the DB in Go? My goal is to have my webpage using PHP display the data by interacting with the DB and then have Go get the `Name` and `Message` information from the webpage. – FaithFosten Dec 17 '21 at 00:49
  • I know one way to do that. This is the way to write the Go code. You read data from the database. Then display the data on html templates I haven't tried writing Go code directly into the html template, Maybe there is another way that I don't know about. I've seen some 3rd party libraries that allow writing Go code directly to html. But I haven't tried it and I don't think it's the right way. – Ahmed Dec 17 '21 at 01:06
  • Here are some titles that may be useful : https://gowebexamples.com/ – Ahmed Dec 17 '21 at 01:10
  • Make php output JSON instead of html (then it's easily parseable by a calling script) and make your go program send a http request to retrieve the data – ADyson Dec 17 '21 at 07:31
  • It seems like you just want to download a webpage using a go program. Perhaps this answers your question? https://stackoverflow.com/questions/40643030/how-to-get-webpage-content-into-a-string-using-go – Paul Hankin Dec 17 '21 at 15:27

1 Answers1

2

I would suggest these two steps :

STEP 1 : PHP code produces plain text (eg : "John;Message 1\n;Doe;Message2\n") (see here : [PHP - Render plain text][1][https://stackoverflow.com/questions/4506679/rendering-plain-text-through-php)][1]

STEP 2 : In GO, use http GET to request the infos from the PHP server:

package phpgo

import ( "fmt" "io/ioutil" "net/http" "strings" )

func getFromPhp(url string, filename string) ([]string, error) { var err error

fmt.Printf("Downloading %s...\n", filename)

//Execute the HTTP GET request
resp, err := http.Get(url)
if err != nil || resp.StatusCode != 200 {
    err = fmt.Errorf("failed download from %s ", url)
    return err
}

//Read the content the from response body
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil || resp.StatusCode != 200 {
    err = fmt.Errorf("failed download from %s ", url)
    return err
}

//Put each element of data in a slice
infos := strings.Split(string(body), "\n")

return infos, err

}

[1]: Rendering plain text through PHP)

Myrer
  • 56
  • 5