1 / 15

PHP Programming for Internet: Database Access and APIs

Learn about accessing databases with PHP using deprecated and modern methods, exploring mysqli and PDO, connection setup, querying, data retrieval, and data insertion in a structured manner.

Télécharger la présentation

PHP Programming for Internet: Database Access and APIs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PrograMação para internet Prof.: Jean Carlo Mendes web@yahoo.com.br http://www.jeancarlomendes.com.br

  2. PHP – Acesso a BD • Três APIs • mysql (depreciada a partir do PHP 5.5) • mysqli • PDO

  3. PHP – tipos de BD

  4. PHP – mysqli • Pode ser construída usando OO ou de forma procedural • Não aceita parâmetros nomeados

  5. PHP – mysqli - Abrindo conexão • $mysqli = mysqli_connect('servidor', 'usuario', 'senha', 'banco_de_dados'); • $mysqli = new mysqli('servidor', 'usuario', 'senha', 'banco_de_dados');

  6. PHP – mysqli – Executando uma qry $sql = "SELECT id, nome, telefone FROM tblContatos"; $query = $mysqli->query($sql);

  7. PHP – mysqli – Recuperando os dados while ($dados = $query->fetch_array()) { echo $dados["id"] . ': ' . $dados["nome"] . ' | Telefone: ' . $dados["telefone"]. '<br>' ; }

  8. PHP – mysqli – Nr de registros... $query->num_rows

  9. PHP – Juntando as partes... <?php $mysqli = new mysqli('localhost', 'root', '', 'tdw2014'); $sql = "SELECT id, nome, telefone FROM tblContatos"; $query = $mysqli->query($sql); while ($dados = $query->fetch_array()) { echo $dados["id"] . ': ' . $dados["nome"] . ' | Telefone: ' . $dados["telefone"]. '<br>' ; } echo '<br>Registros encontrados: ' . $query->num_rows ?>

  10. PHP – Separando os dados de conexão Cria-se um arquivo php para conter os dados de conexão. Este arquivo é “incluído” em outros PHP quando necessário configura_conexao.PHP <?php $servidor = "localhost"; $banco = "tdw2014"; $usuario = "root"; $senha = ""; $conexao = new mysqli($servidor, $usuario, $senha, $banco); ?>

  11. PHP – Separando os dados de conexão Cria-se um arquivo php para conter os dados de conexão. Este arquivo é “incluído” em outros PHP quando necessário configura_conexao.PHP <?php $servidor = "localhost"; $banco = "tdw2014"; $usuario = "root"; $senha = ""; $conexao = new mysqli($servidor, $usuario, $senha, $banco); ?>

  12. PHP – Separando os dados de conexão <?phprequire_once('configura_conexao.php'); ?>

  13. PHP – Inserindo dados.. Recupera dados submetidos... $nome = $_POST["txtNome"]; $telefone = $_POST["txtTelefone"]; Constroi a query sql ... $sql = "insertintotblContatos (nome, telefone) values ('$nome','$telefone') ";

  14. PHP – Inserindo dados.. Executa a query $linha_inserida = $conexao->query($sql); O metodo query ira retornar true se a inserção for realizada... if($linha_inserida) { echo "Registro inserido com sucesso"; }

  15. PHP – Inserindo dados com comandos preparados.... $nome = $conexao->real_escape_string($_POST["txtNome"]); $telefone = $conexao->real_escape_string ($_POST["txtTelefone"]); $query = "insert into tblContatos (nome, telefone) values (?,?) "; $comando = $conexao->prepare($query); $comando->bind_param('ss', $nome, $telefone); if($comando->execute()) { echo "Registro inserido com sucesso"; }

More Related