View and modify items in the database with php

Asked

Viewed 937 times

0

Esse e o painel para editar as informações

I need to display the database values in these input fields and also edit them, I already have the column created, with Title & Adscode table name is website inserir a descrição da imagem aqui

  • How is your connection class with the bank? You use mysql_* (obsolete), mysqli_* or PDO

  • mysql_* (obsolete). I am using PHP 5.2

2 answers

0


The functions mysql_* are obsolete and have been removed in PHP 7, butmysqli_* is similar, I’ll show you an example with mysqli...

Connection:

//inicia uma conexão com o banco, note que aqui ja é selecionado o banco de dados
$con = new mysqli("server", "user", "pass", "db_name");

//Caso algum erro ocorra mostra o erro
if(mysqli_connect_errno()) trigger_error(mysqli_connect_error());


Query in STMT standard:

//Prepara o SQL
$stmt = $con->prepare("SELECT * FROM site");

//Executa o SQL
$stmt->execute();

//Obtem o resultado do SQL
$res = $stmt->get_result();


To show SQL has several forms...

fetch_all - Stores the result in an array in another array:

$array = $res->fetch_all();
echo $array[x][y];

x - consultation line

y - consultation column

For example, $array[0][2] returns the id (column at position 2) of the first row of the query (row at position 0), remembering whenever the array starts at position 0, that is, position 0 = 1°, position 1 = 2°, ...


fetch_row - Stores a line in an array, usually a loop is used to catch all lines:

while($array = $res->fetch_row()) {
    echo "$array[0], $array[1], $array[2]<br>";
}

Shows all names, adscode and id, the <br> is just for better viewing


fetch_assoc - It works the same way as fetch_row, but instead of taking the position of the array, it is picked up by the name called by the database:

while($array = $res->fetch_row()) {
    echo $array["titulo"]." (".$array["id"].") - ".$array["adscode"]."<br>"
}

In the fetch_assoc it is not necessary to use in the order of the database columns, but not to use interpolation (variable in the middle of the string, "texto $variavel mais texto"), error cause


fetch_array - Saves data in an array, can be used both $array["coluna"] as $array[x]:

while($array = $res->fetch_array()) {
    echo $array["titulo"]." (".$array["id"].") - $array[2]<br>";
}

In the fetch_array can be used interpolation on numbers


fetch_object - Returns an object with the data:

while($array = $res->fetch_object()) {
    echo "$array->titulo ($array->id) - $array->adscode<br>";
}

In the fetch_object it is possible to use interpolation quietly

0

You can create a php file with the code below:

<?php 

include("conexaoMysql.php");

class Result{
    public $titulo;
    public $adscode;

}

function getResultado($conn){
    $sqlf = "
        SELECT titulo,adscode FROM tabela
    ";

    $stmt = $conn->prepare($sqlf);
    $stmt->execute();
    $stmt->bind_result($titulo, $anuncio);

    while(($stmt->fetch()) ){
        $result = new Result();

        $result->numero = $titulo;
        $result->data = $anuncio;

        $listaResult[] = $result;
    }

    return $listaResult;
}   

try{
    $listaResult = getResultado($conn);  
}catch (Exception $e){
    $msgErro = $e->getMessage();
}
?>

If it is more than one line of records, I created the $listResult variable to store all records.

This line: "include("connectionMysql.php");" refers to a file with the database connection data, I created just to make it easier, but it would look something like this if I were using SQL:

<?php
$HOST = "";
$USER = "";
$PASSWORD = "";
$DATABASE = "";

$conn = new mysqli($HOST, $USER, $PASSWORD, $DATABASE);

if ($conn->connect_error)
    throw new Exception('Falha na conexão com o MySQL: ' . $conn->connect_error);
?>
  • did not work I’m trying to display <?php echo $title; ? > of the indefinite variable

  • 1

    Edit the question by adding the code you have so far.

  • Post the code of your file that connects to the bank.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.