Ineedtodisplaythedatabasevaluesintheseinputfieldsandalsoeditthem,Ialreadyhavethecolumncreated,withTitle&AdsCodetablenameissite
The mysql_*
functions are obsolete and have been removed in PHP 7, but mysqli_ * are similar, I will show an example with mysqli ...
//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());
//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
- Save the result in an array in another array:
$array = $res->fetch_all();
echo $array[x][y];
x
- query row
y
- query column
$array[0][2]
returns the id (column in position 2) of the first row of the query (line of position 0), always remembering that the array starts at position 0, that is, position 0 = 1 °, position 1 = 2 °, ...
fetch_row
- Save a line in an array, a loop is usually used to fetch all lines:
while($array = $res->fetch_row()) {
echo "$array[0], $array[1], $array[2]<br>";
}
Show all names, adscode and id, <br>
is just for better viewing
fetch_assoc
- Works the same way that the fetch_row
, however instead of taking the position of the array, is caught by the name denominated by the bank:
while($array = $res->fetch_row()) {
echo $array["titulo"]." (".$array["id"].") - ".$array["adscode"]."<br>"
}
No fetch_assoc
does not have to be used in the order of the columns of the bank, but does not use interpolation (variable in the middle of the string, "texto $variavel mais texto"
), causes error
fetch_array
- Saves the data in an array can be used both $array["coluna"]
and $array[x]
:
while($array = $res->fetch_array()) {
echo $array["titulo"]." (".$array["id"].") - $array[2]<br>";
}
In%% can be used interpolation in numbers
fetch_array
- Returns an object with the data:
while($array = $res->fetch_object()) {
echo "$array->titulo ($array->id) - $array->adscode<br>";
}
No fetch_object
can use interpolation quietly
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 row of records, I created the variable $ listResult to store all the records.
This line: "include (" connectionMysql.php "); I have created a file with the database connection data, I created it just to make it easier, but it would be something like this if you are 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);
?>