How to better optimize MySQL connection and listing script?

-1

How can I better optimize the connection and listing script for MySQL ? For I check in my research several ways to do this, but I do not know if this is a correct practice of both database connection and to list information of MySQL .

connection.php

<?php
$con = mysqli_connect("localhost", "root", "", "guaraparivirtual");
?>

index.php

<?php include ("conexao.php"); ?>

<?php

$seleciona=mysqli_query($con,"select * from noticias");
while($campo=mysqli_fetch_array($seleciona)){
?>

<?php echo $campo["Titulo"]."</br>"; ?>

<?php
}
?>
    
asked by anonymous 03.07.2017 / 01:32

2 answers

2

Only select columns of interest to save server resources and streamline your search, in your case the column titulo

More stylish and easy to read.

<?php 
   include ("conexao.php");
   $seleciona=mysqli_query($con,"select titulo from noticias");
     while($campo=mysqli_fetch_array($seleciona))
     {
       echo $campo["Titulo"]."</br>"; ?>
     }
?>
    
03.07.2017 / 04:00
0

Instead of using SELECT * FROM , you can use SELECT titulo FROM noticias , you do not have to take all fields, only use the title and so on. This way the search will be faster. I also advise you to use PDO

<?php
try {
  $pdo = new PDO('mysql:host=localhost;dbname= guaraparivirtual', 'root', '');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}
?>


$sql = $pdo->prepare("SELECT titulo FROM noticias");
$ln = $sql->fetchObject();

echo $ln->titulo;

But regardless of what to use, always, that you will not use all the fields in the table, just select the ones that will be used.c

    
03.07.2017 / 01:56