You are here:

MySQL/PHP/mysql and front-end

Advertisement


Question
Hello, stuart gregory

I have a website built using PHP/mysql. What technique would you choose to fetch the data from database to front-end?

I am familiar with RESTful API services, and so far I've been using those to fetch data from the server into the front-end. However, I got a website built in PHP and would like to know what is the best way to fetch data to the front-end.

Thank you

Regards

jk

PS: I am a front-end developer ( html, css and javascripts are my strength ), I was asked to join a project, which is a website mainly built using PHP/mysql. Now, I'd like to have a better idea on how server to client communication works on a PHP/mysql website.

Answer
Dear Sir

PHP is the most common programming language now and there are thousands of websites offering support and tutorials for almost every aspect you want to do.

I have offered a very simple php/mysql connection and a very simple select statement below to connect to the database, select users first and last name and the show the results to the page.


page called test.php
---------------
<?php

//lets connect to the database server

  define('DB_HOST', 'localhost');
   define('DB_USER', 'database_username');  //change this to your username
   define('DB_PASSWORD', 'database_password');  //change this to your password
   define('DB_DATABASE', 'your database_name'); //change this to your database
  
     //Connect to mysql server
  $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
  if(!$link) {
     die('Failed to connect to server: ' . mysql_error());
  }
  
  //Select database
  $db = mysql_select_db(DB_DATABASE);
  if(!$db) {
     die("Unable to select database");
  }

//we are now connected to the database so lets select the results

//i have assumed the database table holds a cell called firstname, and lastname
//i have also assumed the database table is called members

//not:   * means all so SELECT * FROM means SELECT ALL FROM

$qry = "SELECT * FROM members";
$result = mysql_query($qry);
while ($info = mysql_fetch_array($result)) {
      $firstname = $info['firstname'];
      $lastname = $info['lastname'];

echo "$firstname $lastname";  //the term echo is used to display this to the page
echo "<br />";  

} //close of while loop

?>

----------------------
end of page test.php

the out come above would select members firstname and lastname from the datbase table members and show the results on a new line, so say 10 names in the table, would outcome 10 lines with each name on a new line.

There are lots of options we could add a WHERE clause, so WHERE age is 22 and this would show only the members names who's age is 22.

Depending on your table content we could use a different clause.

I hope this helps giving you the basic understanding of selecting and displaying data

MySQL

All Answers


Answers by Expert:


Ask Experts

Volunteer


stuart gregory

Expertise

PHP html, xhtml, css coding and MySQL

Experience

My experiance is all first had as I am web developer and I have, and do build many websites all using php and mysql. This ranges from building a simple search engine to secure php login scripts. Some websites like http://www.web-application-development.info are complete database driven websites.

Organizations
I have a large number of websites which I host and maintain, such as. http://www.webreger.com http://www.universalscience.co.uk http://www.modellingelectronics.co.uk

Education/Credentials
Web application development. (Open University) BSC Honers Computing and development (Open University)

©2016 About.com. All rights reserved.