How to retrieve or obtain data from the MySQL database using PHP

0 votes
After i had created mysql database and make connection between them. Now, how can i request data from the database using php?

thank you!!
Mar 27, 2020 in PHP by kartik
• 37,490 points
395 views

1 answer to this question.

0 votes

Hello kartik,

 Actually there are many functions that  are available in PHP to retrieve the data from the MySQL database.

The Function are :mysqli_fetch_array(), mysqli_fetch_row(), mysqli_fetch_assoc(), mysqli_fetch_object().

Few functions are mentioned below:

mysqli_fetch_array() – It is used to fetch the records as a numeric array or an associative array.

Sample code:

// Associative or Numeric array

$result=mysqli_query($DBconnection,$query);

$row=mysqli_fetch_array($result,MYSQLI_ASSOC);

echo "Name is $row[0]

";

echo "Email is $row['email']

";

mysqli_fetch_row() – It is used to fetch the records in a numeric array.

Sample code:

//Numeric array

$result=mysqli_query($DBconnection,$query);

$row=mysqli_fetch_array($result);

printf ("%s %s\n",$row[0],$row[1]);

mysqli_fetch_assoc() – It is used to fetch the records in an associative array.

Sample code:

// Associative array

$result=mysqli_query($DBconnection,$query);

$row=mysqli_fetch_array($result);

printf ("%s %s\n",$row["name"],$row["email"]);

mysqli_fetch_object() – It is used to fetch the records as an object.

Sample code:

// Object

$result=mysqli_query($DBconnection,$query);

$row=mysqli_fetch_array($result);

printf ("%s %s\n",$row->name,$row->email);

There are many other function that can be used to retrieve data from database for different purpose according to the user use of data.

Thank you!

answered Mar 27, 2020 by Niroj
• 82,680 points

Related Questions In PHP

0 votes
1 answer

What is Php Mysql Database? How php connect to database?

Hello kartik, MySQL is the most popular database ...READ MORE

answered Feb 25, 2020 in PHP by Dey
252 views
0 votes
1 answer

How to convert from MySQL datetime to another format with PHP?

Hello, To convert a date retrieved from MySQL ...READ MORE

answered May 19, 2020 in PHP by Niroj
• 82,680 points
191 views
0 votes
1 answer

How to find the php.ini file from the command line?

Hello @kartik, You can get a full phpinfo() using : php ...READ MORE

answered May 19, 2020 in PHP by Niroj
• 82,680 points
6,508 views
0 votes
1 answer

How to connect to SQL Server database from JavaScript in the browser?

Hello @kartik, You shouldn´t use client javascript to ...READ MORE

answered Jul 21, 2020 in PHP by Niroj
• 82,680 points
6,977 views
0 votes
1 answer

What is meant by passing the variable by value and reference in PHP?

Hello, When the variable is passed as value ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,680 points
411 views
0 votes
1 answer

Connection with MySQL server using PHP. How can we do that?

Hey @kartik, You have to provide MySQL hostname, ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,680 points
169 views
0 votes
1 answer

What are the differences between mysqli_connect and mysqli_pconnect?

Hello, mysqli_pconnect() function is used for making a persistence ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,680 points
977 views
0 votes
1 answer

How to get thumbnail of youtube video from youtube API?

Hii @kartik, If you want the biggest image ...READ MORE

answered Apr 1, 2020 in PHP by Niroj
• 82,680 points
728 views
0 votes
1 answer

How to export the MySql database structure without the data just the structure?

Hello @kartik, You can do with the --no-data option with ...READ MORE

answered Aug 18, 2020 in PHP by Niroj
• 82,680 points
273 views
0 votes
1 answer

In PHP, how to detect the execution is from CLI mode or through browser ?

Hello @kartik, Use the php_sapi_name() function. if (php_sapi_name() == "cli") { ...READ MORE

answered Oct 22, 2020 in PHP by Niroj
• 82,680 points
115 views