Preluare date din MySql

postat acum 4 ani de Stefanescu Mihai in categorie PHP

Informatiile pot fi preluate din baza de date folosind functia PHP query() si SELECT.

Ce-a mai folosita metoda de preluat datele este fecth_assoc(), dar putem folosi si fetch_array.

Functia fetch_assoc() returneaza un array asociativ cu valorile din baza de date sub forma nume_coloana => valoare, iar fetch_array()  returneaza un array cu index numeric.

Mai jos preluam informatiile dn tabelul users:

<?php
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'parola';
$dbdb = 'test';
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbdb);
if( !$conn ){
  die('Nu ma pot conecta: ' . $conn->error());
}
$sql = 'SELECT nume, adresa, varsta FROM users';

$result = $conn->query( $sql, $conn );
if( !$result ){
  die('Nu pot prelua datele: ' . $conn->error());
}

while($row = $result->fetch_assoc() ){  
	echo "Salut, eu sunt ".$row['nume'].", am ".$row['varsta']." ani si locuiesc la ".$row['adresa']."<br />";
} 

echo "Succes!";
$conn->close();
?>

In exemplul de mai sus am folosit functia fetch_assoc() pentru a prelua datele intr-un array asociativ, astfel incat mai apoi sa le accesat folosind sintaxa $nume_array[‘nume_coloana’].

 

Sunt un tanar programator din Bucuresti ce lucreaza in PHP/Mysql (MySqli/PDO), Laravel, CodeIgniter, MySQL, PostgreSQL, Wordpress, HTML5/CSS3, Sass, Photoshop si multe altele.
Google+ Community Facebook Group
Acest articol a fost mutat de pe vechea platforma.
Pentru orice eroare aparuta la mutare va rog sa ma contactati!

Comentariu postate de g la data de 13.05.2019
Pretty! This has been a really wonderful post. Thank you for providing this info.
Comentariu postate de minecraft download pc la data de 15.05.2019
Hi there, just became aware of your blog through Google, and found that it is really informative. I'm going to watch out for brussels. I'll be grateful if you continue this in future. Many people will be benefited from your writing. Cheers!
Comentariu postate de hogan outlet online la data de 15.05.2019
Thank you so much for giving everyone such a remarkable opportunity to read from this website. It can be so beneficial plus full of a lot of fun for me personally and my office co-workers to search your web site at the least 3 times every week to read the new items you will have. And indeed, I'm certainly astounded concerning the impressive points you serve. Certain 3 points in this post are unquestionably the very best we have ever had.
Comentariu postate de minecraft download free la data de 17.05.2019
Hello, after reading this amazing piece of writing i am too glad to share my knowledge here with colleagues.
Comentariu postate de minecraft download free pc la data de 17.05.2019
Way cool! Some extremely valid points! I appreciate you penning this write-up plus the rest of the website is really good.