Latest web development tutorials

i dati delle query MySQL

MySQL database utilizzando l'istruzione SQL SELECT per interrogare i dati.

È possibile mysql> finestra prompt dei comandi per interrogare i dati nel database, oppure tramite script PHP per interrogare i dati.

grammatica

Segue una query di dati comuni in un database MySQL sintassi SELECT:

SELECT column_name,column_name
FROM table_name
[WHERE Clause]
[OFFSET M ][LIMIT N]
  • È possibile eseguire una query utilizzando una o più tabelle, usare una virgola tra le tabelle (,) la segmentazione, e utilizzare una clausola WHERE per impostare i criteri di ricerca.
  • comando SELECT legge uno o più record.
  • È possibile utilizzare un asterisco (*) al posto degli altri campi, istruzione SELECT restituisce tutti i campi della tabella di dati
  • È possibile utilizzare la clausola WHERE per includere tutte le condizioni.
  • È possibile specificare i dati compensati dichiarazione query SELECT inizia da offset. Per impostazione predefinita, l'offset è 0.
  • Limitare il numero di record che è possibile utilizzare per impostare la proprietà restituita.

Prendi i dati dal prompt dei comandi

Gli esempi che seguono ci arriveremo tabelle di dati MySQL w3big_tbl dati tramite comando SELECT:

Esempi

L'esempio seguente restituisce tutti i record della tabella di dati w3big_tbl:

root@host# mysql -u root -p password;
Enter password:*******
mysql> use w3big;
Database changed
mysql> SELECT * from w3big_tbl 
+-------------+----------------+-----------------+-----------------+
| w3big_id | w3big_title | w3big_author | submission_date |
+-------------+----------------+-----------------+-----------------+
|           1 | Learn PHP      | John Poul       | 2007-05-21      |
|           2 | Learn MySQL    | Abdul S         | 2007-05-21      |
|           3 | JAVA Tutorial  | Sanjay          | 2007-05-21      |
+-------------+----------------+-----------------+-----------------+
3 rows in set (0.01 sec)

mysql>

Utilizzare script PHP per recuperare i dati

Utilizzare la funzione PHP mysql_query () e il comando SQL SELECT per recuperare i dati.

Questa funzione viene utilizzata per eseguire comandi SQL, e poi attraverso la funzione PHP mysql_fetch_array () da utilizzare o dati in uscita per tutte le query.

la funzione mysql_fetch_array () Fetch riga come un array associativo, un array numerico o entrambi, in base a una serie di rendimento ottenuto dal risultato Imposta righe generati se non ci sono più righe restituite da risultati falsi.

I seguenti esempi sono leggere tutti i record dei dati nella tabella di w3big_tbl.

Esempi

Provare i seguenti esempi per mostrare tutti i record di tabelle di dati w3big_tbl.

<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT w3big_id, w3big_title, 
               w3big_author, submission_date
        FROM w3big_tbl';

mysql_select_db('w3big');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    echo "Tutorial ID :{$row['w3big_id']}  <br> ".
         "Title: {$row['w3big_title']} <br> ".
         "Author: {$row['w3big_author']} <br> ".
         "Submission Date : {$row['submission_date']} <br> ".
         "--------------------------------<br>";
} 
echo "Fetched data successfully\n";
mysql_close($conn);
?>

L'esempio sopra, i record leggere ogni linea è assegnato al $ variabile di riga, e quindi stampare su ciascun valore.

Nota: Ricordate che se avete bisogno di usare le variabili nella stringa, impostare la variabile posto in parentesi graffe.

Nell'esempio precedente, PHP mysql_fetch_array () funzione del secondo parametro è MYSQL_ASSOC, impostare l'array associativo risultati delle query parametro restituito, è possibile utilizzare il nome del campo come l'indice di campo.

PHP fornisce un'altra funzione mysql_fetch_assoc (), la funzione Carica una riga del risultato come un array associativo. Restituisce un array associativo ottenuto dal risultato Imposta righe generato se non ci sono più righe, restituisce false.

Esempi

Provate il seguente esempio utilizza la funzione mysql_fetch_assoc () per produrre tutti i record di tabelle di dati w3big_tbl:

<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT w3big_id, w3big_title, 
               w3big_author, submission_date
        FROM w3big_tbl';

mysql_select_db('w3big');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_assoc($retval))
{
    echo "Tutorial ID :{$row['w3big_id']}  <br> ".
         "Title: {$row['w3big_title']} <br> ".
         "Author: {$row['w3big_author']} <br> ".
         "Submission Date : {$row['submission_date']} <br> ".
         "--------------------------------<br>";
} 
echo "Fetched data successfully\n";
mysql_close($conn);
?>

È inoltre possibile utilizzare il MYSQL_NUM costante PHP mysql_fetch_array () Il secondo argomento della funzione restituisce un array di numeri.

Esempi

L'esempio seguente utilizza il parametro MYSQL_NUM visualizza tutti i record nella tabella di dati w3big_tbl:

<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT w3big_id, w3big_title, 
               w3big_author, submission_date
        FROM w3big_tbl';

mysql_select_db('w3big');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_NUM))
{
    echo "Tutorial ID :{$row[0]}  <br> ".
         "Title: {$row[1]} <br> ".
         "Author: {$row[2]} <br> ".
         "Submission Date : {$row[3]} <br> ".
         "--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>

Le suddette tre esempi di risultati di output sono gli stessi.


rilascio di memoria

Dopo che abbiamo finito l'attuazione del SELECT, la memoria cursore rilasciato è una buona abitudine. . In funzione PHP mysql_free_result () per ottenere il rilascio della memoria.

L'esempio seguente mostra come utilizzare la funzione.

Esempi

Provare i seguenti esempi:

<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT w3big_id, w3big_title, 
               w3big_author, submission_date
        FROM w3big_tbl';

mysql_select_db('w3big');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_NUM))
{
    echo "Tutorial ID :{$row[0]}  <br> ".
         "Title: {$row[1]} <br> ".
         "Author: {$row[2]} <br> ".
         "Submission Date : {$row[3]} <br> ".
         "--------------------------------<br>";
}
mysql_free_result($retval);
echo "Fetched data successfully\n";
mysql_close($conn);
?>