If I choose to retrieve some MySQL data using PHP, how do I put it in a certain area of my page? For example, if there is a div with an id of "putTextHere", how to make PHP echo/put text in this div? And how to get a certain value from MySQL? If there is a column named 'Name' and another one named 'Age', how to retrieve only the value of Name?

In a nutshell:

// connect to a database (mysql in this example, using mysqli extension)
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');

// check connection
if (mysqli_connect_errno()) {
    die("Connect failed: " . mysqli_connect_error());
}

// a query for selecting age
$query = 'SELECT Age FROM tablename WHERE condition=something';

// run the query and on success display the row
if ($result = mysqli_query($link, $query)) {

    // fetch row
    $row = mysqli_fetch_row($result);

    // echo the value within the div
    echo '<div id="putTextHere">' . $row[0] . '</div>';

}

// free result set
mysqli_free_result($result);

Now, if the pae already exists and you would like to inject age into existing div then you would have to use Ajax.

OK, clashed with Utrivedi's post which directs you to the same concept. I would only recomend using mysqli extension instead of mysql since the former is newer and offers more functionalities. Anyway, the approach is the same in both posts.

Also have a look at the example here. You can se the process of connecting to db, querying and fetching rows in both procedural and object oriented ways. At some stage it is useful to adopt the object oriented apprpoach and get familiar with since so many frameworks and libraries these days use it.

And here you can read about the difference between the mysql and mysqli extensions an a lot of other interesting stuff about how php works with mysql.

Ok, I am new to MySQL. I'm just 15. So I have a few questions-
1.What is mysqli?
2.Your code mentions a variable named result which is never created. The code won't work!
3.As I mentioned, I don't know anything. So do I have to learn Asynchronous JavaScript and XML?
4. Imagine this situation: There are four divs, each of them are being made in HTML(not a PHP echo command so that they can be stylised in CSS) and stylised in CSS. The fourth one's id is putTextHere. How to put age here?

Please answer in a list format.

And also explain the code, please.

what you asking is called embeding, simple php or simple html can not work alone

see sample embeded code here. this page must be named with extenstion .php

<html>
<?php 
//some php processing, it could be anythingin php, may be database fetch or vriables
$myname="hello";
$myaddress="someplace";

?>

<body>

<div id='putpagehere'>
Name:
<?php
echo $myname;
?>
<br>
Address:

<?php
echo $myaddress;
?>
</div>
</body>
</html>

One can not teach you in forum. we can help to solve ur problems.

Ok, I am new to MySQL. I'm just 15.

Best time to start learning :-)

1.What is mysqli?

mysqli is an extensions to the core functionality of PHP to enable you to deal with mysql database. See the link in my previous post (php.net is THE source you want to bookmark).

2.Your code mentions a variable named result which is never created. The code won't work!

$result is being created in line 13 of my code and stores the result of the query (a result object if succesfull or false if unsuccessful). If the code does not work there might be errors somewhere. Have you entered correct info to the connect function? Do you have any error messages?

3.As I mentioned, I don't know anything. So do I have to learn Asynchronous JavaScript and XML?

Yes, if you intend to do serious web devlopment. You should know javascript to some extent but not necessarily in depth. You can use jquery or some similar library that hides complexity for you, instead. XML is not compulsory since JSON is used in number of cases these days. And it is good if you understand the concept of AJAX.

  1. Imagine this situation: There are four divs, each of them are being made in HTML(not a PHP echo command so that they can be stylised in CSS) and stylised in CSS. The fourth one's id is putTextHere. How to put age here?

The divs can be stylised also if they are created by PHP there is no difference. The question is: are the HTML and the divs displayed first and age has to be injected afterwards on some user action or is the age known by the time of creation of html?

> mysqli is an extensions to the core functionality of PHP to enable you to deal with mysql database. See the link in my previous post (php.net is THE source you want to bookmark).
> Yes, if you intend to do serious web devlopment. You should know javascript to some extent but not necessarily in depth. You can use jquery or some similar library that hides complexity for you, instead. XML is not compulsory since JSON is used in number of cases these days. And it is good if you understand the concept of AJAX.

Ans- Why are these necessary? Look at the code urtrivedi posted-

<html>
<?php 
//some php processing, it could be anythingin php, may be database fetch or vriables
$myname="hello";
$myaddress="someplace";
?>
<body>
<div id='putpagehere'>
Name:
<?php
echo $myname;
?>
<br>
Address:
<?php
echo $myaddress;
?>
</div>
</body>
</html>

If we replace line 4 and 5 with MySQL processing code, it should work fine, right?

$result is being created in line 13 of my code and stores the result of the query (a result object if succesfull or false if unsuccessful). If the code does not work there might be errors somewhere. Have you entered correct info to the connect function? Do you have any error messages?

Sorry. That is because I thought it was a double equals sign (==). Cause I learnt Java a few years earlier, all the if statements for me will generate an error if a single equals sign is used. :-). Also in Java, variables cannot be created in an if statement.

The divs can be stylised also if they are created by PHP there is no difference. The question is: are the HTML and the divs displayed first and age has to be injected afterwards on some user action or is the age known by the time of creation of html?

The age has to be inserted when the page is loaded. i.e. the age is known by the time of creation of HTML.

Two more questions -

  1. How to retrieve only one record instead of retrieve all of them?

  2. Today is my birthday. :-). I plan to purchase a book on PHP and MySQL because the PHP tutorial was really complicated for me (the advanced stuff). Here is that website - tizag.com. The MySQL tutorial was really short. (phpforkids.com). Any suggestions?

Requirements -
Easy for me to understand.
Available in India (probably through flipkart.com)

It would probably be better if you posted a number of books.

Today is my birthday. :-)

All the best :-)

How to retrieve only one record instead of retrieve all of them?

The condition must be such that returns only one record:

$query = 'SELECT Age FROM tablename WHERE condition=something';

Have a look at http://phpacademy.org/ which will be easiest way to learn.

The book I used a couple of years ago was PHP6 and MySql Bible. It is avaliable on Amazon but I it is not cheap and I am not sure if it is simple.

All the best :-)

Thanks!

The condition must be such that returns only one record:
$query = 'SELECT Age FROM tablename WHERE condition=something';

As I thought. Understood.

Have a look at http://phpacademy.org/ which will be easiest way to learn.

Learn what? Anyways I am getting a refresher plus lots more on PHP from thenewboston ( the videos on PHP are made by a guy called Alex. At the beginning of each video, he says - "Hi this is Alex from PHPacademy.org." So indirectly I am learning from phpacademy.org)

The book I used a couple of years ago was PHP6 and MySql Bible. It is avaliable on Amazon but I it is not cheap and I am not sure if it is simple.

Leave the book idea, after looking at quite a few forums, I am convinced that the best tutorials are online.

Please answer the rest of my questions in my previous post.

I do not quite understand what other questions are. Can you state them clearly?

Then I shall reword them -

Firstly you explained the MySQLI plugin to me. Then you said I should also learn AJAX. Why is all of this necessary? In the code that urtrivedi has posted -

<html>
<?php 
//some php processing, it could be anythingin php, may be database fetch or vriables
                   $myname="hello";
                   $myaddress="someplace";
?>
<body>
<div id='putpagehere'>
Name:
<?php
echo $myname;
?>
<br>
Address:
<?php
echo $myaddress;
?>
</div>
</body>
</html>

If we replace lines 4 and 5 (extremely indented) with MySQL retrieval code, it will work right?

And that $result doubt, it was only because I thought it was a == sign (cause I come from a Java background which I learnt a few years earlier)

The divs can be stylised also if they are created by PHP there is no difference. The question is: are the HTML and the divs displayed first and age has to be injected afterwards on some user action or is the age known by the time of creation of html?

The age is known by the time of creation of HTML.

So everything has been reworded and a much shorter post this is :-). Hopefully you shall understand my doubts now.

Firstly you explained the MySQLI plugin to me. Then you said I should also learn AJAX. Why is all of this necessary?

Well, it is not necessary, but dynamic web application usually stores to and uses data from a database and MySql is very popular database (and free). You can choose to use any other database (Oracle, MSSql, you name it), it is up to you (hopefully you have a reason to choose so).

Ajax is a way of using Javascript and XML (or PHP or JSON instead) that improves user experience. It is a very good idea to get familiar with it since once you do serious development you will need it. It is not very hard and you can use libraries like jQuery to make the most of it without really spending to much time on it.

If we replace lines 4 and 5 (extremely indented) with MySQL retrieval code, it will work right?

Yes, as long as you do not make any syntax or other errors :-). It is perfectly right to pull data from database, store it in variables and use it later in a script.

It is perfectly right to pull data from database, store it in variables and use it later in a script.

Then why do we need AJAX? Ok, I get the idea of MySQLi, it is like an updated/improved edition of MySQL, but why use AJAX when we can do what I said?

Could you suggest some MySQLi tutorials? (other than PHP.net, of course)

Plenty of mysqli examples on Google like this one.

Ajax you need to change the contents of a webpage (like div or span) without refreshing the page. An example is a basket content in a web shop.

mysqli is not updated/improved edition of MySQL but a PHP extension to access MySql. You can access MySql database with either mysql or mysqli extension (or PDO) but you are always accessing MySql database. mysqli is newer, will be supported in future and has more functionalities (like prepared statements).

And about php.net. This is definetly the best source of information but I understand that is sometimes hard to comprehend since it is not written for beginners. But if you get used to it and its structure (syntax, examples, user comments) it will give you the most and up to date information. Just keep on using it and you will get used to it.

I won't really need AJAX, though I am learning it from Tizag. I won't need it because I don't want to refresh. This is the webpage layout:

All About Animals (heading)
Information (subheading)

blablablablablablablablablabla

Scientific Classification (subheading

Here is where I need MySQL. I am storing the kingdom, phylum, subphylum etc in a MySQL database. So it is already known when the DOM loads. So need to refresh.

and more.... (but no more MySQL)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.