Il miglior tutorial PHP MySQL legge i dati Nel 2024, in questo tutorial puoi imparare Leggere i dati da un database MySQL,Esempi (MySQLi - Object Oriented),Esempi di (DOP),

PHP MySQL legge i dati

Leggere i dati da un database MySQL

SELECT viene utilizzato per leggere i dati dalla tabella dei dati:

SELECT column_name(s) FROM table_name

Per ulteriori informazioni su conoscenza di SQL, si prega di visitare il nostro tutorial di SQL .

Leggiamo i seguenti esempi dai MyGuests tabella ID, colonne e dati di nome e cognome visualizzati sulla pagina:

Esempi (MySQLi - Object Oriented)

<? Php
$ Nomeserver = "localhost";
$ Username = "username";
$ Password = "password";
$ Dbname = "myDB";

// Crea collegamento
$ Conn = new mysqli ($ servername, $ username, $ password, $ dbname);
// Prova connessione
if ($ conn-> connect_error) {
die ( "Connessione fallita:" $ conn-> connect_error.);
}

$ Sql ​​= "SELECT id, nome, cognome FROM MyGuests";
$ Result = $ conn-> query ($ sql);

if ($ result-> numero_colonne> 0) {
// Scrive ogni riga di dati
while ($ row = $ result-> fetch_assoc ()) {
. Echo "<br> id:" $ row [ "id"] "- Nome:" $ row [ "Nome"] "" [lastname "]; it. $ Row".
}
} Else {
echo "0 risultati";
}
$ Conn-> close ();
?>

Il seguente esempio legge tutti tabella registra MyGuests e visualizzato in una tabella HTML:

Esempi di (DOP)

<? Php
echo "<stile di tabella = 'border: 1px solid nero;'>";
echo "<tr> <th> ID </ th> <th> Nome </ th> <th> Cognome </ th> <th> E-mail </ th> <th> Data Reg </ th> </ tr> ";

Classe TableRows estende RecursiveIteratorIterator {
Funzione __construct ($ esso) {
parent :: __ construct ($ esso, self :: LEAVES_ONLY);
}

corrente function () {
ritorno "<td style = 'width: 150px; border: 1px solid nero;'>". parent :: corrente () "</ td>" ;.
}

beginChildren function () {
echo "<tr>";
}

endChildren function () {
echo "</ tr>" "\ n" .;
}
}

$ Nomeserver = "localhost";
$ Username = "username";
$ Password = "password";
$ Dbname = "myDBPDO";

try {
$ Conn = new PDO ( "mysql: host = $ servername; dbname = $ dbname", $ username, $ password);
$ Conn-> setAttribute (DOP :: ATTR_ERRMODE, DOP :: ERRMODE_EXCEPTION);
$ Stmt = $ conn-> prepare ( "SELECT * FROM MyGuests");
$ Stmt-> execute ();

// Imposta set di risultati è un array associativo
$ Result = $ stmt-> setFetchMode (DOP :: FETCH_ASSOC);

foreach (nuove TableRows (nuova RecursiveArrayIterator ($ stmt-> fetchAll ())) come $ k => $ v) {
echo $ v;
}
$ DSN = null;
}
catch (PDOException $ e)
{
echo "Errore:" $ e-> getMessage () ;.
}
$ Conn = null;
echo "</ table>";
?>

PHP MySQL legge i dati
10/30