Joak
-
Posts
20 -
Joined
-
Last visited
Posts posted by Joak
-
-
I'm getting this values ...
[09-Jun-2014 12:44:17] Array ( [id] => 2 [name] => Jhon ) [09-Jun-2014 12:44:17] [09-Jun-2014 12:44:17] [09-Jun-2014 12:44:17] Execute failed to run [09-Jun-2014 12:44:17] Array ( [value1] => 2 [value2] => This was sent from ios to server ) [09-Jun-2014 12:44:17] J [09-Jun-2014 12:44:17] J
The code of function
function redeem() { // Check for required parameters $json = file_get_contents('php://input'); $obj = json_decode($json,true); // prepare the query $stmt = $this->db->prepare('INSERT INTO prueba (id,nombre) VALUES (?, ?)'); $stmt->bind_param('is',$id,$name); // loop througght he json objects to insert into mysql foreach($obj as $assocArray) { error_log(print_r($obj,true)); $id = $assocArray['id']; $name = $assocArray['name']; error_log(print_r($id,true)); error_log(print_r($name,true)); if (!$stmt->execute()) // execute the query´ { error_log(print_r('Execute failed to run',true)); }else{ $stmt = $this->db->prepare('COMMIT'); $stmt->execute(); } } }
I'm confused .... why in the first loop, the variables are null? when excute the Insert .,.... I don't have any value in the variables
-
Psycho ... I'm only checking the errors in error_log file.
My code
<?php class RedeemAPI { private $db; // Constructor - open DB connection function __construct() { $this->db = new mysqli('localhost', 'futchoco', 'Futcho1907', 'futchoco_futsoft'); /* verificar la conexión */ if (mysqli_connect_errno()) { printf("Conexión fallida: %s\n", mysqli_connect_error()); exit(); } $this->db->autocommit(FALSE); } // Destructor - close DB connection function __destruct() { $this->db->close(); } // Main method to redeem a code function redeem() { // Check for required parameters $json = file_get_contents('php://input'); $obj = json_decode($json,true); // prepare the query $stmt = $this->db->prepare('INSERT INTO prueba (id,nombre) VALUES (?, ?)'); $stmt->bind_param('is',$id,$name); // loop througght he json objects to insert into mysql foreach($obj as $assocArray) { //error_log(print_r($obj,true)); $id = $assocArray['id']; $name = $assocArray['name']; error_log(print_r($id,true)); error_log(print_r($name,true)); $stmt->execute(); // execute the query $stmt = $this->db->prepare('COMMIT'); $stmt->execute(); } } } // This is the first thing that gets called when this page is loaded // Creates a new instance of the RedeemAPI class and calls the redeem method $api = new RedeemAPI; $api->redeem(); ?>
-
Don't you have to bind after you have assigned the values?
After assigned the values .... I have this...The id it's null and the name only has the first letter
[09-Jun-2014 11:57:32]
[09-Jun-2014 11:57:32]
[09-Jun-2014 11:57:32] J
[09-Jun-2014 11:57:32] J
-
Which is very different from what you stated in the first post
Try this:
$stmt = $mysqli->prepare('INSERT INTO prueba (id,nombre) VALUES (?, ?)'); $stmt->bind_param('is',$id,$name); foreach($obj as $assocArray) { $id = $assocArray['id']; $name = $assocArray['name']; $stmt->execute(); // execute the query }
Psycho ..... I don't have any error ...... but I can't insert the record
-
that error would mean that the input data you have shown in this thread isn't what the actual data is.
what does the following show for the contents of $obj -
echo '<pre>',print_r($obj,true),'</pre>';
This contents of $obj
[09-Jun-2014 10:48:55] Array
(
[id] => 2
[name] => Jhon
)
-
because bind_parm() uses references to variables, i was only able to get this to work as follows -
$stmt = $mysqli->prepare('INSERT INTO prueba (id,nombre) VALUES (?, ?)'); $stmt->bind_param('is',$id,$name); foreach($obj as $assocArray) { $id = $assocArray[0]['id']; $name = $assocArray[1]['name']; $stmt->execute(); // execute the query }
Thanks Mac_gyver, but I have gotten a new message error just the first line inside foreach ..... PHP Fatal error: Cannot use string offset as an array
-
Ch0cu3r, thanks for you comment. I fix it, I have not any error message, but how can I do the commit?
$stmt = $this->db->commit();
I can do the commit ..... but the table has the number 0 has id, and null in name column
-
You're using mysqli_prepare incorrectly,
With mysqli_prepare you only define the query, the values being replaced by placeholders. It them returns a statement object ($stmt). With the $stmt object you'd call the bind_param() method to bind the values to the placeholders. Finally you'd call mysqli_execute to execute the query.
Example code
// prepare the query $stmt = $this->db->prepare('INSERT INTO prueba (id,nombre) VALUES (?, ?)'); $assocArray = array('id' => '', 'name' => ''); // define $assocArrat var first // bind the values to the placeholders, describing their data types $stmt->bind_param('is', intval($assocArray['id']), $assocArray['name']); // loop througght he json objects to insert into mysql foreach($obj as $assocArray) { $stmt->execute(); // execute the query }
$stmt = $this->db->commit();
-
Hi guys
A mobile application send data in Json format, so I want to insert the data into Mysql table, when I try to insert I have gotten the error "PHP Warning: mysqli::prepare() expects exactly 1 parameter, 3 given", my table only has 2 columns, "id" and "name" column.
The array has this data
( [0] => ('id', '2') [1] => ('name', 'Jhon') )
The function to parse and insert data ...
function redeem() { // Check for required parameters $json = file_get_contents('php://input'); $obj = json_decode($json,true); // Assumes $obj == array(0 => $assocArray, 1 => $assocArray) foreach($obj as $index => $assocArray) { // Assumes $assocArray == array(0 => array('id' => '2'), 1 => array('name' => 'John')) $stmt = $this->db->prepare('INSERT INTO prueba (id,nombre) VALUES (%d,%s)',$assocArray[0]['id'],$assocArray[1]['name']) or die(mysqli_error($this->db)); $stmt->execute(); } }
-
Hello guys.
I execute a query and then put the records into array, the query has ORDER BY .... why I don't have the same order in the array?, If I execute the query, the record one it's no the same that $arreglo[0].
$stmt = $this->db->prepare('SELECT a.id_torneo, b.tor_nombre, a.id_jornada, a.id_juego, a.cal_fecha_hora, id_arbitro, a.cal_estatus, a.cal_default, c.id_equipo, d.equ_nombre, c.enc_locvis, e.id_jugador, e.jug_nombre_pila, e.jug_apellido_pat, e.jug_apellido_mat, e.jug_representante, e.jug_numero, jug_fechimp_reg FROM calendario a, torneo b, encuentro c, equipo d, jugador e WHERE a.id_cliente =? AND a.id_sucursal = ? AND date(a. cal_fecha_hora)= date(?) AND a.id_cliente = b.id_cliente AND a.id_sucursal = b.id_sucursal AND a.id_torneo = b.id_torneo AND a.id_cliente = c.id_cliente AND a.id_sucursal = c.id_sucursal AND a.id_torneo = c.id_torneo AND a.id_jornada = c.id_jornada AND a.id_juego = c.id_juego AND a.id_cliente = d.id_cliente AND c.id_sucursal = d.id_sucursal AND c.id_torneo = d.id_torneo AND c.id_equipo = d.id_equipo AND d.id_cliente = e.id_cliente AND d.id_sucursal = e.id_sucursal AND d.id_torneo = e.id_torneo AND d.id_equipo = e.id_equipo AND e.jug_estatus = "A" ORDER BY 5,11') or die(mysqli_error($this->db)); $stmt->bind_param("iis", $cliente, $sucursal, $fecha); $stmt->execute(); $stmt->bind_result($id_torneo, $tor_nombre, $id_jornada, $id_juego, $fecha_partido, $id_arbitro, $cal_estatus, $cal_default, $id_equipo, $equipo_nombre, $locvis, $id_jugador, $jug_nombre, $jug_apellidop, $jug_apellidom, $jug_rep, $jug_playera, $jug_fechareg); $arreglo = array() ; $contador = 0; while ($stmt->fetch()) { $arreglo[$contador] = array("id_torneo"=>$id_torneo,"torneo_nombre"=>$tor_nombre,"id_jornada"=>$id_jornada,"id_juego"=>$id_juego,"fecha_partido"=>$fecha_partido,"id_arbitro"=>$id_arbitro,"cal_estatus"=>$cal_estatus,"cal_default"=>$cal_default,"id_equipo"=>$id_equipo,"equipo_nombre"=>$equipo_nombre,"locvis"=>$locvis,"id_jugador"=>$id_jugador,"jug_nombre"=>$jug_nombre,"jug_apellidop"=>$jug_apellidop,"jug_apellidom"=>$jug_apellidom,"jug_rep"=>$jug_rep,"jug_player"=>$jug_playera,"jug_fechareg"=>$jug_fechareg); $contador++; }
-
WEBSTYLES ... you right .... ALL are player's Vodka team .. they have to exist in the first level
{JSON}
{0}
id_equipo:"7"
nom_equipo:"Vodka Juniors"
locvis: "L"
[ ]Jugadores
{0}
id_jugador:"1"
jugador_nom:"Juan Carlos"
-
Thanks Psycho
The problem it's when I want to convert to JSON format
with your code I got this...
{JSON}
{0}
id_equipo:"7"
nom_equipo:"Vodka Juniors"
locvis:"L"
Jugadores
{0}
{1}
And I want this format ..... notice Jugadores it's another level of my first array(Equipos)
{JSON}
{0}
id_equipo:"7"
nom_equipo:"Vodka Juniors"
locvis:"L"
Jugadores
{0}
{1} -
Zane .... I have gotten the same result .... Jugadores doesen't part for the Equipo array
$equipo['Jugadores'] = $jugador;
{
"0": {
"id_equipo": "7",
"nom_equipo": "Vodka Juniors",
"locvis": "L"
},
"Jugadores": [
{
"id_jugador": "12",
"jugador_nom": "Omar",
"jugador_pat": "Ortiz",
"jugador_mat": "Flores",
"jug_repre": "N",
"jug_playera": "16",
"fechareg": null
}
]
}I want this....
{
"0": {
"id_equipo": "7",
"nom_equipo": "Vodka Juniors",
"locvis": "L",
"Jugadores": [
{
"id_jugador": "12",
"jugador_nom": "Omar",
"jugador_pat": "Ortiz",
"jugador_mat": "Flores",
"jug_repre": "N",
"jug_playera": "16",
"fechareg": null
}
]
}
} -
Hello WebStyles
The results...
//Jugadores Array
Array
(
[0] => Array
(
[id_jugador] => 12
[jugador_nom] => Omar
[jugador_pat] => Ortiz
[jugador_mat] => Flores
[jug_repre] => N
[jug_playera] => 16
[fechareg] =>
)
[1] => Array
(
[id_jugador] => 1
[jugador_nom] => Francisco
[jugador_pat] => Rojas
[jugador_mat] => Ortega
[jug_repre] => N
[jug_playera] => 17
[fechareg] =>
)
)
//Equipo Array
Array
(
[0] => Array
(
[id_equipo] => 7
[nom_equipo] => Vodka Juniors
[locvis] => L
)
) -
This is my code
$equipojug = array_slice($equipo,0,3,true)+array("Jugadores"=>$jugador);
$equipo and $jugador are my two arrays -
Hello
I still unresolved my case .... I use the array_slice function, but "Jugadores" it's wrong level I want to put down "locvis" level .....I get this array ..{
"0": {
"id_equipo": "7",
"nom_equipo": "Vodka Juniors",
"locvis": "L"
},
"Jugadores": [
{
"id_jugador": "10",
"jugador_nom": "Mario Jaxiel",
"jugador_pat": null,
"jugador_mat": "Vargas",
"jug_repre": "N",
"jug_playera": "898",
"fechareg": null
},
{
"id_jugador": "6",
"jugador_nom": "Misael Yahir",
"jugador_pat": null,
"jugador_mat": "Morlan",
"jug_repre": "N",
"jug_playera": "1",
"fechareg": null
}
]
}The correct array
{
"0": {
"id_equipo": "7",
"nom_equipo": "Vodka Juniors",
"locvis": "L",
"Jugadores": [
{
"id_jugador": "10",
"jugador_nom": "Mario Jaxiel",
"jugador_pat": null,
"jugador_mat": "Vargas",
"jug_repre": "N",
"jug_playera": "898",
"fechareg": null
},
{
"id_jugador": "6",
"jugador_nom": "Misael Yahir",
"jugador_pat": null,
"jugador_mat": "Morlan",
"jug_repre": "N",
"jug_playera": "1",
"fechareg": null
}
]
}
} -
Hello
I have two arrays, and both I want to convert in one like this structure .... both arrays are Equipos array and Jugadores array, how can I do?Regards
Equipos
0
id_equipo : 5
nom_equipo: San Blas
locvis: L
Jugadores
0
id_jugador : 1
name: Robert
-
Hello Requinix.
Oks, If I get all the data with one query (no problem, in fact I started with one query) ... how can i build a tree structure array?
-
Hello guys
I Have to try to build an array that has a tree structure(4 levels), but I have gotten an error when I tried to put the 2 level ... commands out of sync; you can't run this command now.
I attached a file with the code, what's wrong?
Regards.
Insert into Table associative array
in PHP Coding Help
Posted
Yes ..... $obj it's an array, and only has 'id' => 2,'name' => 'Jhon'; the value1 and value2 are the names "original" that I recive bye the mobile application ... I forgot change when paste the result of error_log file ..... I can't do "echo" ... I have a hostting server.