Mobile app version of vmapp.org
Login or Join
Kaufman445

: Basic looping on PHP for web design I am very much a newbie in PHP. Could you tell me about basic looping in PHP if I want create a web design. Also, how do I connect to a MySQL database?

@Kaufman445

Posted in: #Mysql #Php

I am very much a newbie in PHP. Could you tell me about basic looping in PHP if I want create a web design. Also, how do I connect to a MySQL database?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Kaufman445

1 Comments

Sorted by latest first Latest Oldest Best

 

@Fox8124981

LOOPING

There are two methods to loop in PHP:
for, and;
foreach

The PHP for Loop

The for loop is used when you know in advance how many times the script should run.

Syntax

for (init counter; test counter; increment counter) {
code to be executed;
}


Parameters:

init counter: Initialize the loop counter value
test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
increment counter: Increases the loop counter value
The example below displays the numbers from 0 to 10:

Example

<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>


The PHP foreach Loop

The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

Syntax

foreach ($array as $value) {
code to be executed;
}


For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.

The following example demonstrates a loop that will output the values of the given array ($colors):

Example

<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {
echo "$value <br>";
}
?>


Source: PHP 5 for Loops



SQL CONNECTION

Open a Connection to MySQL

Before we can access data in the MySQL database, we need to be able to connect to the server:

Example (MySQLi Object-Oriented)

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>


A note on the object-oriented example above: $connect_error was broken until PHP 5.2.9 and 5.3.0. If you need to ensure compatibility with PHP versions prior to 5.2.9 and 5.3.0, use the following code instead:

// Check connection
if (mysqli_connect_error()) {
die("Database connection failed: " . mysqli_connect_error());
}


Example (MySQLi Procedural)

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>


Close the Connection

The connection will be closed automatically when the script ends. To close the connection before, use the following:

Example (MySQLi Object-Oriented)

$conn->close();


Example (MySQLi Procedural)

mysqli_close($conn);


Source: PHP Connect to MySQL

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme