M. Niyazi Alpay
M. Niyazi Alpay
M. Niyazi Alpay

I've been interested in computer systems since a very young age, and I've been programming since 2005. I have knowledge in PHP, MySQL, Python, MongoDB, and Linux.

 

about.me/Cryptograph

  • admin@niyazi.org

PHP - Connecting to MySQL Database and Reading Information (Illustrated Explanation)

PHP - Connecting to MySQL Database and Reading Information (Illustrated Explanation)

The database we will connect to: ornek_veritabani

We have created our database and our table named isim_listesi.

PHP - Connecting to and Reading from MySQL Database (Visual Guide)

We defined the type of the column named id as int, which means integer, and defined it as the primary index, which means the primary key.

 

What is this primary key?

It is a unique value like the TC identity number written in our identity, it will assign a number to each value saved in the table, and each number will be different from each other, so each piece of information will have its own identity number, the reason we checked auto increment is to automatically enter a value for each added information, otherwise the identity number would not be defined.

 

Now, how do we connect to this database?

We need a database user to connect, our user is: ornek_kullanici

The command to connect is

mysql_connect("server address","username","password" );

mysql_connect("localhost","ornek_kullanici","12345" );

we connect with this command, but such a use is not very secure, it is better to do with an if query

We will assign our connection command to a variable and check that variable with an if statement,

$connection = mysql_connect("localhost","ornek_kullanici","12345" );
if (!$connection){
	die('Could not connect to database: ' . mysql_error());
}

here it says "if the connection cannot be established, stop executing the php script and print an error message to the screen"

Example of usage:

PHP - Connecting to and Reading from MySQL Database (Visual Guide)

PHP - Connecting to and Reading from MySQL Database (Visual Guide)

when we run it, the page appears blank because there is no content on the page, let's change the connection password or username and look at the page again

PHP - Connecting to and Reading from MySQL Database (Visual Guide)

PHP - Connecting to and Reading from MySQL Database (Visual Guide)

as you can see, it gave an error, it says the user does not have connection permission, because the password is wrong

 

Executing Queries in the Database

We use the mysql_query("sql query" ); command to execute a query

The sql command we will use here is select, select is used to select and list from the database

SELECT * FROM isim_listesi = * statement means all columns, it means select all columns in the isim_listesi table

SELECT id FROM isim_listesi = it means select only the id column from the isim_listesi table

let's select all

mysql_query("SELECT * FROM isim_listesi" );

now we selected the table but how are we going to bring this information to the screen

we bring the information to the screen with mysql_fetch_array(); the usage is as follows:

We define a variable

$db_read=mysql_fetch_array(mysql_query("SELECT * FROM isim_listesi" ));

if we print this variable to the screen, it writes "Array" on the screen, the reason is that this variable is an array variable.

PHP - Connecting to and Reading from MySQL Database (Visual Guide)

PHP - Connecting to and Reading from MySQL Database (Visual Guide)

 

$db_read["name"]; if we print it like this, we can reach the actual result

PHP - Connecting to and Reading from MySQL Database (Visual Guide)

PHP - Connecting to and Reading from MySQL Database (Visual Guide)

if we want all the information in the database to be written to the screen, we can do it with a loop

$query=mysql_query("SELECT * FROM isim_listesi" );

while($db_read=mysql_fetch_array($query)){
	$name=$db_read["name"];

	$surname=$db_read["surname"];

	echo "Name : ".$name." - Surname: ".$surname;
}

 

PHP - Connecting to and Reading from MySQL Database (Visual Guide)

PHP - Connecting to and Reading from MySQL Database (Visual Guide)

 

In my next documents, I will show you how to delete, update, and add information

Muhammed Niyazi ALPAY - Cryptograph

Senior Software Developer & Senior Linux System Administrator

Meraklı

PHP MySQL MongoDB Python Linux Cyber Security

You may also want to read these

There are none comment

Leave a comment

Your email address will not be published. Required fields are marked *