0

I am new to PHP and Mysql programming and I would like to know if I can access another database row when showing data in a form.

Here is the code:

$mysql_host     = "";
$mysql_database = "";
$mysql_user     = "";
$mysql_password = "";

$con = mysqli_connect($mysql_host, $mysql_user, $mysql_password, $mysql_database);

$result  = mysqli_query($con, "SELECT * FROM Elev WHERE Clasa = '" . $_SESSION['clasa'] . "' ORDER BY Nume ASC ");
$result2 = mysqli_query($con, "SELECT * FROM M_Profesori WHERE ID = '" . $_SESSION['ID_p'] . "' ");

while ($row2 = mysqli_fetch_array($result2)) {
    while ($row = mysqli_fetch_array($result)) {
        echo "<select>
                <option value='" . $row2["Materia"] . "'>" . $row2["Materia"] . "</option>
                <option value='" . $row2["Materia"] . "'>" . $row2["Materia"] . "</option>
             </select>";
    }
}

In the second option (<option value='" . $row2[Materia] . "'>" . $row2[Materia] . "/option>) I would like to access the next database row, not this one. Is this possible?

2
  • I'd say, this is very likely possible, however, what would you do on the last row? It wouldn't have a next row then. Commented Oct 25, 2014 at 14:59
  • If you're new to MySQL, checkout PDO and start using it. The safest way to query your database php.net/manual/en/class.pdo.php Commented Oct 25, 2014 at 15:05

1 Answer 1

1

If I've understood, you can try this:

$row2 = mysqli_fetch_array($result2);
foreach($row2 as $key => $value) {
   ...
   <option value='" . $row2[$key+1][Materia] . "'>" . $value . "</option>
   ...
}

try this...

In adicional, look this: You can use a template to build better codification. (site in portuguese) http://raelcunha.com/template.php

http://raelcunha.com/packages/extra/template/pt-br/api/index.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.