1

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?

1
  • Unfortunately questions related to programming are off-topic for this site. You could ask this question over at Stack Overflow, but it would likely be considered too broad and incomplete.
    – dan
    Jan 7, 2015 at 5:26

1 Answer 1

5

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

4
  • Thanks that help me. But how to connect the sql and database on php script could you tell me Jan 7, 2015 at 5:01
  • Added SQL connection code and examples Jan 7, 2015 at 5:06
  • 1
    Excellent answer! Unfortunately, the question is off-topic here.
    – closetnoc
    Jan 7, 2015 at 5:44
  • Apologies, I didn't realise that it was off topic. Thanks for complimenting me on my answer though. :-) Jan 8, 2015 at 8:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.