How do I select from Php in MySQL

1

I want to do a simple select from mysql, how do I do it? I think it would be something like that?

<?php
mysql_conect('127.0.0.1','root','clave');
mysql_query ('Select coluna1,colunna2 from tabla')
?>
    
asked by Juan Carlos Villamizar Alvarez 08.08.2016 в 03:20
source

2 answers

4

The correct way (and recommended by PHP) is to use the MySQLi extension (you can read more here: Improved MySQL extension )

$conexion = mysqli_connect("127.0.0.1", "usuario", "password", "base_de_datos");
$resultado = mysqli_query($conexion, "SELECT * FROM TABLA");
    
answered by 08.08.2016 / 03:52
source
0

As they also say that will depend a lot on whether you are using a framework such as laravel or codeigniter, but as you put it in your example I assume that it is structured; First you create your file

conexion.php

  

$ hostname_cn="localhost"; // or you can put your 127.0.0.1 '

     

$ database_cn="database";

     

$ username_cn="root";

     

$ password_cn="xxx";

     

$ cn = mysqli_connect ($ hostname_cn, $ username_cn, $ password_cn,   $ database_cn) or trigger_error (mysqli_error (), E_USER_ERROR);

index.php

  

<?php require_once('conexion.php'); // you include the connection file

    $sql = "SELECT * FROM articulo";//preparas tu query
$rs = mysqli_query($cn, $sql) or die(mysqli_error()); //ejecutas la consulta

while($row = mysqli_fetch_array( $rs)){  //siempre usa while para hacer el recorrido
         $variable = $row["nombre de las columna de bd"]; y así con las demás columnas de la tabla seleccionada


        }                                   
        ?>

If you want to cut the php and put the html and draw a div and print it inside the <?php echo $variable ?>

Remember if you are going to be this in a framework with codeigniter you forget connection; and you only configure in databases.php files and you only fill the parameters as well as the image,

Well I would have to talk about the Mvc but I think that is not the point of the good topic, a framework always simplifies life.

    
answered by 08.08.2016 в 05:12