Nu er jeg så bare stødt ind i det problem, at når jeg bruger SELECT, og får den til at return min query med "returnResult()", så modtager jeg kun 1 ud af evt mange columns.
hvor mange columns selecter du i
"SELECT password FROM users
hvis deter flere columns kunne det være
"SELECT * FROM users
men du mener måske rækker ??
if ($type == "select") {
$result = $this->stmt->get_result();
while ($row = $result->fetch_array(MYSQLI_ASSOC)){
$this->result[]=$row;
}
// kun 1 række
// $this->result = $result->fetch_array(MYSQLI_ASSOC);// this does work :)
}
udskrivning
$data = new mysqliCon("select", "SELECT * FROM users WHERE lastname=?", array("and"));
$data = $data->returnResult();
for($i=0, $lng=count($data);$i<$lng;$i++){
echo $data[$i]["password"];
echo $data[$i]["id"];
}
ved kun 1 række
echo $data["password"];
echo $data["id"];
komplet code
<?php
class mysqliCon {
public $mysqli;
public $query;
public $stmt;
public $result;
function __construct($type, $statement, $param = array()) {
// Connect to Database
$this->mysqli = new mysqli("localhost", "root", "", "test");
//If no error occured making the connection
if (!$this->mysqli->connect_error) {
//set the statement
$this->query = $statement;
//check if statement isset // just a precortion
if ($this->query) {
//prepare the statement for execute
if ($this->stmt = $this->mysqli->prepare($statement)) {
//call bind_param with the right type variable, and as refrences
call_user_func_array(array($this->stmt, "bind_param"), $this->refValues($param));
// if all is ok, execute the statement
if ($this->stmt->execute()) {
//only if the statements is of the type SELECT return result
if ($type == "select") {
$result = $this->stmt->get_result();
while ($row = $result->fetch_array(MYSQLI_ASSOC)){
$this->result[]=$row;
}
// $this->result = $result->fetch_array(MYSQLI_ASSOC);// this does work :)
}
} else {
error_log("Didn't work");
}
//close my statement
$this->stmt->close();
}
}
}
}
function returnResult() {
// returns the result outside the class
return $this->result;
}
function refValues($arr = array()) {
//check php version, and determine which type
if (strnatcmp(phpversion(), '5.3') >= 0) {
//$keywords = array_keys($arr);
//$tempArr = array();
$valueType = "";
for ($i = 0, $lng = sizeof($arr); $i < $lng; $i++) {
switch (gettype($arr[$i])) {
case "string": $valueType .= "s";
break;
case "integer": $valueType .= "i";
break;
case "double": $valueType .= "d";
break;
case "object": $valueType .= "b";
break;
}
}
foreach ($arr as $index => &$value) {
// Set values as references.. Do not! do this at home! foreach & references are evil!
$arr[$index] = &$value;
}
array_unshift($arr, $valueType);
//print_r($arr);
//return $arr;
}
return $arr;
}
}
// $data = new mysqliCon("select", "SELECT password FROM users WHERE lastname=?", array("and"));
$data = new mysqliCon("select", "SELECT * FROM users WHERE lastname=?", array("and"));
$data = $data->returnResult();
// print_r($data);
for($i=0, $lng=count($data);$i<$lng;$i++){
echo $data[$i]["password"];
echo $data[$i]["id"];
}
//echo $data["password"];
//echo $data["id"];
?>