1

Topic: How to receive value from a table field in PHP?

There are simple requests:

SELECT question FROM la_question WHERE id=1;

And

SELECT answer FROM la_answer WHERE id_question=1;

The first request deduces only one record, the second the certain set, three-five. I do request through the created class, a method:

//request
public function query ($sql) {
//we check connection
if (! $this-> connection) {
return false;
}
//we do request
$result = $this-> connection-> query ($sql);
//it is checked on errors
if (mysqli_error ($this-> connection)) {
throw new Exception (mysqli_error ($this-> connection));
}
//if request not on data acquisition, we return its result
if (is_bool ($result)) {
return $result;
}
//it is obtained the data in an array
$data = array ();
while ($row = $result-> fetch_object ()) {
$data [] = $row;
}
//we clear storage and we return result
mysqli_free_result ($result);
return $data;
}

Everything, basically works, but I need not it, and will not understand as. For example the code:

$proba = new Database (HOST, USER, PASS, DB);
$quest = $proba-> query ("SELECT question FROM la_question");
echo "<pre>";
var_export ($quest);
echo "</pre>";

Gives such answer:

wrote:

array (
0 =>
stdClass:: __ set_state (array (
' question ' => ' Good afternoon! We do a call? ';
));
)

I understand, it is possible and print_r to use, or still that, but I need not it, I need ' question ' to deduce, for example in the unit <DIV> through echo. And on the second request I generally want to push in buttons. But I cannot understand with these arrays.
On here it at me produces an error:

echo $quest [' question '];

Error:

wrote:

Notice: Undefined index: question in D:\xampp\htdocs\SalesPHP\proba.php on line...

Where to read, what I not so do? I need field value of the table, all array with a field title is not necessary to me.

2

Re: How to receive value from a table field in PHP?

Stang wrote:

Where to read

In the documentation.
mysqli_fetch_array

3

Re: How to receive value from a table field in PHP?

wrote:

in the documentation.
mysqli_fetch_array

That I already tried so many that I seem to me at all there I search. In this case to me produces something such:

wrote:

Warning: mysqli_fetch_array () expects parameter 1 to be mysqli_result, array given in

Here it is stupid from the documentation an example copied:

$row = mysqli_fetch_array ($quest, MYSQLI_NUM);
printf ("%s (%s) \n", $row [0]);

4

Re: How to receive value from a table field in PHP?

Also that as on me it is strange, like as at me not a normal array, and object and if I try to make here so:

$row = $quest-> fetch_assoc ();
echo "we Deduce?". $row [' question '];

That produces here such plan an error:

wrote:

Fatal error: Call to a member function fetch_assoc () on a non-object in

5

Re: How to receive value from a table field in PHP?

I so do

///
///Returns associative array
///
protected function execute () {
try {
$sth = $ this-> dbh-> prepare ($this-> sth);
$sth-> execute ($this-> params);
$this-> params=array ();
return $sth-> fetchAll (PDO:: FETCH_ASSOC);
}
catch (PDOException $e) {
die (' Sorry, an error has occured during execute (). Contact your su. Error: '. $ e.getMessage ());
}
}
///
///Returns non-associative array
///
protected function executeN () {
try {
$sth = $ this-> dbh-> prepare ($this-> sth);
$sth-> execute ($this-> params);
$this-> params=array ();
return $sth-> fetchAll (PDO:: FETCH_NUM);
}
catch (PDOException $e) {
die (' Sorry, an error has occured during executeN (). Contact your su. Error: '. $ e.getMessage ());
}
}

6

Re: How to receive value from a table field in PHP?

waszkiewicz, it is somehow difficult for perceiving for me. And there is no working variant hour, for example on GitHub to look? That I could trace all chain? I will not steal,  too, to me would wash off to understand...

7

Re: How to receive value from a table field in PHP?

Stang;

class DBCore {
protected $dbh;
protected $sth;
protected $params;
///
///Constructor
///
function __ construct ($path) {
$config=parse_ini_file ($path, TRUE);
$server = ' localhost ';
$dbname = $ config [someshit] [someshit];
$uname = $ config [someshit] [someshit];
$pass = $ config [someshit] [someshit];
try {
$this-> dbh=new PDO ("mysql:host = $ server; dbname = $ dbname", $uname, $pass);
}
catch (PDOException $e) {
die ("Sorry, an error has occured during connection. Contact your su. Error:". $e-> getMessage ());
}
}

Usage

class DIA extends DBCore {
function __ construct ($path) {
parent:: __ construct ($path);
}
function getExContents ($cat, $parentid) {
$this-> sth = "select ne.id id
,ne.name name
,p.name cat
,p.id pid
from nxExecs ne
join parameters p
on p.id=ne.category
where (ne.category =:cat or:cat is null)
and parentid =:parentid
order by name ";
$this-> params=array ("cat" => $cat, "parentid" => $parentid);
return $this-> execute ();
}
}

On Git is not present, I do not spread

8

Re: How to receive value from a table field in PHP?

printf ("<table class ='dtable '>");
foreach ($dia-> getExContents ($ _POST ["cond"] == "? null: $ _ POST ["cond"], $ _POST ["parent"]) as $row)
{
printf ("<tr data-id = ' % s' data-pid = ' % s'> <td> %s %s </td> <td> %s </td> <td> %s </td> </tr>"
,$row ["id"]
,$row ["pid"]
,"<a href ='# ' class ='nxexEdit '> Editing </a>"
,"<a href ='# ' class ='nxexDelete '> to Delete </a>"
,$row ["name"]
,$row ["cat"]);
}
printf ("</table>");