Sei sulla pagina 1di 5

7/5/2016

PHP: Ejemplo general de la extensin MySQL - Manual

Downloads
Documentation
GetInvolved
Help
Search

GettingStarted
Introduction
Asimpletutorial
LanguageReference
Basicsyntax
Types
Variables
Constants
Expressions
Operators
ControlStructures
Functions
ClassesandObjects
Namespaces
Errors
Exceptions
Generators
ReferencesExplained
PredefinedVariables
PredefinedExceptions
PredefinedInterfacesandClasses
Contextoptionsandparameters
SupportedProtocolsandWrappers
Security
Introduction
Generalconsiderations
InstalledasCGIbinary
InstalledasanApachemodule
FilesystemSecurity
DatabaseSecurity
ErrorReporting
UsingRegisterGlobals
UserSubmittedData
MagicQuotes
HidingPHP
KeepingCurrent
Features
HTTPauthenticationwithPHP
Cookies
Sessions
DealingwithXForms
Handlingfileuploads
http://php.net/manual/es/mysql.examples-basic.php

1/5

7/5/2016

PHP: Ejemplo general de la extensin MySQL - Manual

Usingremotefiles
Connectionhandling
PersistentDatabaseConnections
SafeMode
Commandlineusage
GarbageCollection
DTraceDynamicTracing
FunctionReference
AffectingPHP'sBehaviour
AudioFormatsManipulation
AuthenticationServices
CommandLineSpecificExtensions
CompressionandArchiveExtensions
CreditCardProcessing
CryptographyExtensions
DatabaseExtensions
DateandTimeRelatedExtensions
FileSystemRelatedExtensions
HumanLanguageandCharacterEncodingSupport
ImageProcessingandGeneration
MailRelatedExtensions
MathematicalExtensions
NonTextMIMEOutput
ProcessControlExtensions
OtherBasicExtensions
OtherServices
SearchEngineExtensions
ServerSpecificExtensions
SessionExtensions
TextProcessing
VariableandTypeRelatedExtensions
WebServices
WindowsOnlyExtensions
XMLManipulation
KeyboardShortcuts
?
Thishelp
j
Nextmenuitem
k
Previousmenuitem
gp
Previousmanpage
gn
Nextmanpage
G
Scrolltobottom
gg
Scrolltotop
gh
Gotohomepage
gs
http://php.net/manual/es/mysql.examples-basic.php

2/5

7/5/2016

PHP: Ejemplo general de la extensin MySQL - Manual

Gotosearch
(currentpage)
/
Focussearchbox
FuncionesdeMySQL
Ejemplos
ManualdePHP
Referenciadefunciones
Extensionesdebasesdedatos
Extensionesdebasesdedatosespecficasdelproveedor
MySQL
MySQL(Original)
Ejemplos
Changelanguage: Spanish
EditReportaBug

EjemplogeneraldelaextensinMySQL
Estesimpleejemplomuestracomoconectar,ejecutarunaconsulta,imprimirlasfilasresultantesy
desconectaraunabasededatosMySQL.
Ejemplo#1EjemplogeneraldelaextensinMySQL
<?php
//Conectando,seleccionandolabasededatos
$link=mysql_connect('mysql_host','mysql_user','mysql_password')
ordie('Nosepudoconectar:'.mysql_error());
echo'Connectedsuccessfully';
mysql_select_db('my_database')ordie('Nosepudoseleccionarlabasededatos');
//RealizarunaconsultaMySQL
$query='SELECT*FROMmy_table';
$result=mysql_query($query)ordie('Consultafallida:'.mysql_error());
//ImprimirlosresultadosenHTML
echo"<table>\n";
while($line=mysql_fetch_array($result,MYSQL_ASSOC)){
echo"\t<tr>\n";
foreach($lineas$col_value){
echo"\t\t<td>$col_value</td>\n";
}
echo"\t</tr>\n";
}
echo"</table>\n";
//Liberarresultados
mysql_free_result($result);
//Cerrarlaconexin
mysql_close($link);
http://php.net/manual/es/mysql.examples-basic.php

3/5

7/5/2016

PHP: Ejemplo general de la extensin MySQL - Manual

?>

addanote

UserContributedNotes1note
up
down
5
zjrwjzzzat163dotcom
1yearago
Be aware that if you are trying to foreach the associative array returned by
mysql_fetch_array() with MYSQL_ASSOC or mysql_fetch_assoc(), you have to ensure the
return value that is used in foreach structure is exactly a array rather than a
FALSE value.
Or you might encounter the warning below:
Warning: Invalid argument supplied for foreach()
Here is an example (assume that $result have already stored the return value from
mysql_query()):
If you have some block of code like:
<?php
// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
?>
If this is the case, you should use the code below instead:
<?php
// Check ahead, before using it
if (mysql_num_rows($result) > 0) {
// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
http://php.net/manual/es/mysql.examples-basic.php

4/5

7/5/2016

PHP: Ejemplo general de la extensin MySQL - Manual

echo "</table>\n";
}
?>
addanote

Ejemplos
EjemplogeneraldelaextensinMySQL
Copyright20012016ThePHPGroup
MyPHP.net
Contact
OtherPHP.netsites
Mirrorsites
Privacypolicy

http://php.net/manual/es/mysql.examples-basic.php

5/5

Potrebbero piacerti anche