E ae pessoal, segue script simples em php de conexão ao MySQL, só teste mesmo:
Esconder código-fonte
<?
$conecta = mysql_connect("host", "database", "password") or print (mysql_error());
print "Conectado ao banco de dados: OK!";
mysql_close($conecta);
?>
Scripts recomendados
Cotação do Dólar em PHP + XML
RandomBanner - Classe para mostrar banner aleatório em imagem ou flash
Busca Plug Play
Cotação do Dólar (segunda edição)
Classe para criar menus de listas
Comentários
tem um errinho.. no lugar de "database" é "username".
$conn = mysql_connect('host', 'username', 'password') or die(mysql_error());
mysql_select_db('database', $conn);
Minha opnião é que você deveria colocar este script - no minimo - em uma função.
<?php
function ConMysql( $host = 'localhost', $user = 'user', $pass = '123456', $db = 'banco' )
{
if( is_string( $host) and is_string($user) and is_string($pass) and is_string($db) )
{
$con =@ mysql_connect( $host, $user, $pass );
if( $con )
{
$db =@ mysql_select_db( $db, $con );
if( $db )
{
return $db;
}
else
{
exit( 'Este banco de dados não existe';
}
}
else
{
exit( 'Não foi possivel conectar ao host MySql: verifique os dados da conexão' );
}
}
else
{
exit( 'Não foi possivel conectar ao host MySql: verifique os dados da conexão' );
}
}
?>
Mas, o melhor seria utilizar o PDO.
Mensagem
Minha opnião é que você deveria colocar este script - no minimo - em uma função.
<?php
function ConMysql( $host = 'localhost', $user = 'user', $pass = '123456', $db = 'banco' )
{
if( is_string( $host) and is_string($user) and is_string($pass) and is_string($db) )
{
$con =@ mysql_connect( $host, $user, $pass );
if( $con )
{
$db =@ mysql_select_db( $db, $con );
if( $db )
{
return $db;
}
else
{
exit( 'Este banco de dados não existe';
}
}
else
{
exit( 'Não foi possivel conectar ao host MySql: verifique os dados da conexão' );
}
}
else
{
exit( 'Não foi possivel conectar ao host MySql: verifique os dados da conexão' );
}
}
?>
Mas, o melhor seria utilizar o PDO.
ronin, teria como mostrar isso usando o PDO?
Mensagem
ronin, teria como mostrar isso usando o PDO?
Mensagem
Recomendo: ADOdb library for PHP
http://phplens.com/lens/adodb/docs-adodb.htm
Sempre uso esse script que fiz, ele ajuda a encontrar o erro, na onde está errando e qual linha e coluna, bem legal :
<?php
include '/config/config.php';
abstract class database {
protected $_HOST = HOST;
protected $_USER = USER;
protected $_PASS = PASS;
protected $_DBNM = DBNM;
protected $conn = NULL;
protected $SLDB = NULL;
public function __construct(){
$this->connection();
$this->SelectDB();
}
function __destruct(){
if($this->conn != NULL){
mysql_close($this->conn);
}
}
function connection(){
$this->conn = mysql_connect($this->_HOST, $this->_USER, $this->_PASS)
or die($this->handleErro(__FILE__,__FUNCTION__,mysql_errno(),mysql_error()));
}
function SelectDB(){
$this->SLDB = mysql_select_db($this->_DBNM)
or die($this->handleErro(__FILE__,__FUNCTION__,mysql_errno(),mysql_error()));
}
function handleErro($file = false, $instance = false, $numError = false, $msgError = false){
if($file == false): $file = __FILE__; endif;
if($instance == false): $instance = __FUNCTION__; endif;
if($numError == $numError): $numError = mysql_errno(); endif;
if($msgError == FALSE): $msgError = mysql_error(); endif;
echo "
<pre>
ARQUIVO: {$file}
INSTANCIA: {$instance}
NUMERO DE ERRO: {$numError}
MENSAGEM DE ERRO: {$msgError}
</pre>
";
}
}
?>
qualquer duvida, entre em contato comigo.
Mensagem
Sempre uso esse script que fiz, ele ajuda a encontrar o erro, na onde está errando e qual linha e coluna, bem legal :
<?php
include '/config/config.php';
abstract class database {
protected $_HOST = HOST;
protected $_USER = USER;
protected $_PASS = PASS;
protected $_DBNM = DBNM;
protected $conn = NULL;
protected $SLDB = NULL;
public function __construct(){
$this->connection();
$this->SelectDB();
}
function __destruct(){
if($this->conn != NULL){
mysql_close($this->conn);
}
}
function connection(){
$this->conn = mysql_connect($this->_HOST, $this->_USER, $this->_PASS)
or die($this->handleErro(__FILE__,__FUNCTION__,mysql_errno(),mysql_error()));
}
function SelectDB(){
$this->SLDB = mysql_select_db($this->_DBNM)
or die($this->handleErro(__FILE__,__FUNCTION__,mysql_errno(),mysql_error()));
}
function handleErro($file = false, $instance = false, $numError = false, $msgError = false){
if($file == false): $file = __FILE__; endif;
if($instance == false): $instance = __FUNCTION__; endif;
if($numError == $numError): $numError = mysql_errno(); endif;
if($msgError == FALSE): $msgError = mysql_error(); endif;
echo "
<pre>
ARQUIVO: {$file}
INSTANCIA: {$instance}
NUMERO DE ERRO: {$numError}
MENSAGEM DE ERRO: {$msgError}
</pre>
";
}
}
?>
qualquer duvida, entre em contato comigo.
Contribuir com comentário
Enviar