<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MySQL &#8211; Nexto</title>
	<atom:link href="https://nexto.ch/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>https://nexto.ch</link>
	<description>Business Empowerment</description>
	<lastBuildDate>Sat, 26 Oct 2024 15:28:54 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>
	<item>
		<title>Setting Up a MySQL Database and Connecting with PHP</title>
		<link>https://nexto.ch/mysql/setting-up-a-mysql-database-and-connecting-with-php/</link>
		
		<dc:creator><![CDATA[Nexto]]></dc:creator>
		<pubDate>Sat, 15 Jun 2024 14:17:49 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">https://nexto.ch/?p=226593</guid>

					<description><![CDATA[Introduction MySQL is a widely used open-source relational database management system. PHP...]]></description>
										<content:encoded><![CDATA[<h3>Introduction</h3>
<p>MySQL is a widely used open-source relational database management system. PHP is a popular server-side scripting language. This tutorial will guide you through setting up a MySQL database and connecting to it using PHP.</p>
<h4>Prerequisites</h4>
<ul>
<li>A server with MySQL and PHP installed.</li>
<li>Access with sudo privileges.</li>
</ul>
<h5>Step 1: Install MySQL and PHP (if not already installed)</h5>
<pre>sudo apt update
sudo apt install mysql-server php php-mysql -y
</pre>
<h5>Step 2: Secure MySQL Installation</h5>
<p>Run the security script:</p>
<pre>sudo mysql_secure_installation
</pre>
<p>Follow the prompts to set the root password and secure the installation.</p>
<h5>Step 3: Create a MySQL Database and User</h5>
<p>Log in to MySQL:</p>
<pre>sudo mysql -u root -p
Create a new database and user:

CREATE DATABASE mydatabase;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
</pre>
<h5>Step 4: Create a PHP Script to Connect to the Database</h5>
<p>Create a file named db_connect.php in your web server’s root directory (e.g., /var/www/html):</p>
<pre>&lt;?php
$servername = "localhost";
$username = "myuser";
$password = "mypassword";
$dbname = "mydatabase";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn-&gt;connect_error) {
    die("Connection failed: " . $conn-&gt;connect_error);
}
echo "Connected successfully";
?&gt;
</pre>
<h5>Step 5: Test the Database Connection</h5>
<p>Open a web browser and navigate to http://your_server_ip/db_connect.php. You should see “Connected successfully”.</p>
<h5>Step 6: Handling Database Operations</h5>
<p>Modify db_connect.php to perform database queries:.</p>
<pre>// Perform a query
$sql = "SELECT * FROM mytable";
$result = $conn-&gt;query($sql);

if ($result-&gt;num_rows &gt; 0) {
    // Output data
    while($row = $result-&gt;fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. "&lt;br&gt;";
    }
} else {
    echo "0 results";
}
$conn-&gt;close();
</pre>
<h5>Step 7: Preventing SQL Injection</h5>
<p>Always use prepared statements or parameterized queries to prevent SQL injection attacks.</p>
<h4>Conclusion</h4>
<p>You have set up a MySQL database and connected to it using PHP. You can now build dynamic web applications that interact with your database.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
