Joomfish migration script returns error - php

Here is a script that upgrades joomfish (joomla translation component) from joomla 1.5 to 2.5:
$db = new PDO("mysql:host=localhost;dbname=db;charset=UTF8", "root", "pass");
$stmt = $db->prepare("select distinct(jfc.reference_id),c.catid,jfc.language_id,c.modified,c.modified_by,c.version,c.modified_by ,c.ordering,c.created_by,c.metadesc ,c.created_by_alias from jos_jf_content jfc ,jos_content c where jfc.reference_id = c.id and jfc.reference_table = 'content' ");
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($results as $row) {
$count_row = $db->prepare("select * from jos_jf_content where reference_id = ? and language_id = ?");
$count_row->bindValue(1, $row['reference_id']);
$count_row->bindValue(2, $row['language_id']);
$lang_code = $db->prepare("select lang_code from j25_languages where lang_id = ?");
$lang_code->bindValue(1, $row['language_id']);
$lang_code->execute();
$l_code = $lang_code->fetch(PDO::FETCH_OBJ);
$language_code = $l_code->lang_code;
$count_row->execute();
$title ="";
$fulltext ="";
$introtext ="";
$alias ="";
$published ="";
while($col = $count_row->fetch(PDO :: FETCH_ASSOC))
{
if($col['reference_field'] == "title")
{
$title = $col['value'];
}
if($col['reference_field'] == "fulltext")
{
$fulltext = $col['value'];
}
if($col['reference_field'] == "introtext")
{
$introtext = $col['value'];
}
if($col['reference_field'] == "alias")
{
$alias = $col['value'];
}
$published = $col['published'];
}
$exe = $db->prepare("insert into j25_content (`title`,`alias`,`introtext`,`fulltext`,`published`,`catid`,`created`,`created_by`,`created_by_alias`,`modified`,`modified_by`,`version`,`ordering`,`metadesc`,`language`) values(:title,:alias,:introtext,:fulltext,:published,:categoryid,:created,:created_by,:created_by_alias,:modified,:modified_by,:version,:ordering,:metadesc,:language_code)");
$exe->execute(array(':title' => $title,':alias' => $alias,':introtext' => addslashes($introtext),':fulltext' => addslashes($fulltext),':published' => ".$published.",':categoryid' => $row['catid'],':created' => date("Y-m-d H:i:s"),':created_by' => $row['created_by'],':created_by_alias' => "".$row['created_by_alias']."",':modified' => date("Y-m-d H:i:s"),':modified_by' =>$row['modified_by'],':version' => $row['version'],':ordering' => $row['ordering'],':metadesc' => $row['metadesc'],':language_code' => $language_code));
$i = $db->lastInsertId('id');
$asst = $db->prepare("select asset_id from j25_categories where id = ? ");
$asst->bindValue(1, $row['catid']);
$asst->execute();
$asst_id = $asst->fetch(PDO::FETCH_OBJ);
$cassetid = $asst_id->asset_id;
$sel = $db->prepare("select lft,rgt FROM `j25_assets` where id = (SELECT max(id) FROM `j25_assets`)");
$sel->execute();
$select = $sel->fetch(PDO::FETCH_OBJ);
$left = $select->lft;
$right = $select->rgt;
$left=$left+1;
$right = $right+1;
$stmt = $db->prepare("insert into j25_assets (`parent_id`,`lft`,`rgt`,`level`,`name`,`title`) values(:cassetid,:left,:right,:level,:name,:title)");
$stmt->execute(array(':cassetid' => $cassetid,':left' => $left,':right' => $right,':level' => 4,':name' => "com_content.article.".$i,':title' => $title));
$insertedId = $db->lastInsertId('id');
$update = $db->prepare("update j25_content set asset_id = ? where id = ?");
$update->bindValue(1, $insertedId);
$update->bindValue(2, $i);
$update->execute();
$stmt = $db->prepare("insert into j25_jf_translationmap (language,reference_id,translation_id,reference_table) values (:language_code,:reference_id,:translation_id,:content)");
$stmt->execute(array(':language_code' => $language_code,':reference_id' => $row['reference_id'],':translation_id' => $i,':content' => 'content'));
}
Line of code:
$language_code = $l_code->lang_code;
Returns:
Trying to get property of non-object
I'm not an author of the script and not good in PHP, but I've tried to print_r($l_code->lang_code); and I got expected result en-GB from [lang_code] => en-GB. What I need to change in this code? Thanks.

The line $language_code = $l_code->lang_code > 0; sets $language_code to boolean value. Try var_dump($l_code); and var_dump($language_code); to debug your results. You should also check if $l_code actually is an object, or perhaps null was returned. Hope that helps.

Related

How to get two mysql querie results in one JSON array?

in my current project I have to create a JSON array, in which for each day a status with a colour is displayed in a calendar (FullCalendar) and additionally a button with a link is to be displayed for all days where an event is planned.
For this I have two SELECT's which both work on their own. I have already tried some things how to put the results of both SELECT's into one JSON array, but I keep getting the error 'Undefinded Variable'.
How do I have to pack the results of the two SELEC T's put together that all results are displayed correctly?
header('Content-Type: application/json');
include "../../../includes/db.php";
if(isset($_GET['team_id'])) {
$team_id = $_GET['team_id'];
}
$stmt = $connection->prepare("SELECT date FROM date");
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()) {
$date = $row['date'];
$team_planner_user_status_present = 1;
$team_planner_user_status_absent = 2;
$team_planner_user_status_reservation = 3;
$stmt1 = $connection->prepare("SELECT
(
SELECT COUNT(*)
FROM team_planner_user
INNER JOIN user ON team_planner_user.user_id = user.user_id
WHERE user.team_id = ?
AND team_planner_user.date = ?
AND team_planner_user.team_planner_user_status = ?
) AS present,
(
SELECT COUNT(*)
FROM team_planner_user
INNER JOIN user ON team_planner_user.user_id = user.user_id
WHERE user.team_id = ?
AND team_planner_user.date = ?
AND team_planner_user.team_planner_user_status = ?
) AS absent,
(
SELECT COUNT(*)
FROM team_planner_user
INNER JOIN user ON team_planner_user.user_id = user.user_id
WHERE user.team_id = ?
AND team_planner_user.date = ?
AND team_planner_user.team_planner_user_status = ?
) AS reservation
");
$stmt1->bind_param("sssssssss", $team_id, $date, $team_planner_user_status_present,
$team_id, $date, $team_planner_user_status_absent,
$team_id, $date, $team_planner_user_status_reservation);
$stmt1->execute();
$result1 = $stmt1->get_result();
while($row1 = $result1->fetch_array()) {
$present = $row1['present'];
$absent = $row1['absent'];
$reservation = $row1['reservation'];
if($present >= 5) {
$color = '#a1ff9e';
} else if($present + $reservation >= 5) {
$color = '#fcff9e';
} else {
$color = '#ff9e9e';
}
}
$stmt1->close();
$stmt2 =$connection->prepare("SELECT * FROM game_planned INNER JOIN game_role ON game_planned.match_role_id = game_role.match_role_id WHERE team_id = ? AND game_planned_date = ?");
$stmt2->bind_param("ss", $team_id, $date);
$stmt2->execute();
$result2 = $stmt2->get_result();
while($row2 = $result2->fetch_array()) {
$match_role_name = $row2['match_role_name'];
$game_planned_id = $row2['game_planned_id'];
}
$data[] = array(
'start' => $date,
'display' => 'background',
'color' => $color,
);
if($result->num_rows === 0) {
echo "";
} else {
$data[] = array(
'title' => $match_role_name,
'url' => 'http://localhost/r6team-redesign/team/team.php?team=details-match-plan&match_planned_id='.$game_planned_id,
'start' => $date,
);
}
}
$stmt->close();
echo json_encode($data);

UPDATE record serialized - php mysql

I have this problem, I have to update a record of a table that has the values of a serialized column. the call to the function works and passes the data correctly. I can enter the record, but I can not update. This is my code:
public function update($user_id, $array_check)
{
$arrayB = array();
$array_check = unserialize($array_check);
foreach ($array_check $key => $value) {
if($value["id"] == $array_check){
$idRow = $value["id"];
if($value["value"] == "0"){
$valueRow = "1";
}else{
$valueRow = "0";
}
}else{
$idRow = $value["id"];
$valueRow = $value["value"];
}
$row = array("id" => $idRow, "value" => $valueRow);
$arrayB[] = $row;
}
$stmt = $this->_db->prepare('UPDATE data_docs SET docs_selected = :docs_selected WHERE user_id = :user_id');
$row = $stmt->execute(array(':user_id' => $user_id, ':docs_selected' => serialize($arrayB) ) );
return $arrayB;
}
edit.
Replace this:
$stmt = $this->_db->prepare('UPDATE data_docs SET docs_selected = :docs_selected WHERE user_id = :user_id);
with:
$deseralized = serialize($arrayB);
$stmt = $this->_db->prepare('UPDATE data_docs SET docs_selected = '$deseralized ' WHERE user_id = '$user_id');

Categories Multi Level display using php and mysql in json format

I need to display categories and its sub categories in json format, here problem is each category has unlimited levels, some categories has 4 levels and some 2 and some 3 levels. I have tried one solution it is showing only one under level, but I need to show all levels.
Table schema:
Code Example:
function categories() {
header('Content-Type: application/json');
$sql = "select id,name,parent_id from categories where parent_id = 0";
$q = $this->db->conn_id->prepare($sql);
$q->execute();
$main_cat = array();
while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
$sql2 = "select id,name,parent_id from categories where parent_id = ?";
$sub_cat = array();
$r = $this->db->conn_id->prepare($sql2);
$r->bindParam(1, $row['id']);
$r->execute();
while ($row1 = $r->fetch(PDO::FETCH_ASSOC)) {
array_push($sub_cat, array_filter($row1));
}
$row['subcategories'] = $sub_cat;
array_push($main_cat, array_filter($row));
}
echo json_encode(array("categories" => $main_cat));
}
Above Code Json Response Example:
{"categories":[{"id":"1","name":"Development","subcategories":[{"id":"2","name":"All Development","parent_id":"1"},{"id":"3","name":"Web Development","parent_id":"1"},{"id":"12","name":"Mobile Apps","parent_id":"1"}]},{"id":"4","name":"Design","subcategories":[{"id":"5","name":"All Design","parent_id":"4"}]},{"id":"11","name":"IT & Software"}]}
Any one can help me how to solve this issue.
I have tried this solution works for me.
function categories() {
$sql = "SELECT * FROM categories where parent_id = 0";
$results = $this->db->conn_id->prepare($sql);
$results->execute();
while ($result = $results->fetch(PDO::FETCH_ASSOC)) {
$subcat = array();
$id = $result['id'];
$childs = $this->hasChilds($id);
$categories[] = array("id" => $result['id'], "name" => $result['name'], "parent_id" => $result['parent_id'], "subcats" => array_filter($childs));
}
echo json_encode($categories);
}
function hasChilds($id) {
$sql = "SELECT * FROM categories where parent_id = ? ";
$results = $this->db->conn_id->prepare($sql);
$results->bindParam(1, $id);
$results->execute();
$count = $results->rowCount();
$array = array();
if ($count > 0) {
while ($result = $results->fetch(PDO::FETCH_ASSOC)) {
$array[] = array("id" => $result['id'], "name" => $result['name'], "parent_id" => $result['parent_id'], "subcats" => array_filter($this->hasChilds($result['id'])));
}
} else {
$array[] = null;
}
return $array;
}

Unknown column 'E5V7D24M10Y2015' in 'where clause'

I have the code below to update (more 'add to') a row in a database. I have read a few very similar posts, but still can't see where I'm going wrong...
I am getting the error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''EventID' = '2' WHERE GameID = 'E2V1D24M10Y2015' AND PlayerID = '55'' at line 1
In this case, 'E5V7D24M10Y2015' is the value of $GameID. I am looking for a column called GameID where the value is E5V7D24M10Y2015, not a column of that name. Please tell me why my $sql is looking for a column named after the value it is looking for.
Each time the function runs the count($Runners) will be different and the values in each variable will be different. That is why I have the SQL in a loop.
if ($formtype == "gameresults"){
$Runners = $_POST['runners'];
$event = $_POST['event'];
$eid = $_POST['eid'];
$vid = $_POST['vid'];
$GameID = $_POST['GameID'];
$date = $_POST['date'];
$season = $_POST['season'];
$region = $_POST['region'];
$notes = $_POST['notes'];
$kev = "kev#email.com";
$email = $_POST['manager'];
$notes = wordwrap($notes,70);
$subject = ("Results for " . $event);
$tix = "";
$cashs = "";
for ($x = 1; $x < ($Runners + 1); $x++){
$ID = $_POST['ID' . $x];
$Name = $_POST['Name' .$x];
$Place = $_POST['Place'.$x];
$Points = $_POST['Points'.$x];
$Cash = $_POST['Cash'.$x];
$Ticket = $_POST['Ticket'.$x];
$vn = $_POST['vn'];
$buyin = $_POST['buyin'];
$data = array($eid,$vid,$region,$buyin,$GameID,$date,$season,$ID,$Name,$Place,$Points,$Ticket,$Cash,$Runners);
$fields = array('EventID','VenueID','Region','Buyin','GameID','Date','Season','PlayerID','Name','Position','Points','Ticket?','Cash','Runners');
for ($x = 0; $x < (count($data) - 1); $x++){
$sql = "UPDATE results SET '$fields[$x]' = '$data[$x]' WHERE GameID = '$GameID' AND PlayerID = '$ID'";
$res = mysqli_query($dbcon, $sql) or die("Update failed. <br>" . mysqli_error($dbcon));
}
You could modify your code to be more clear. I did example but I did not map all the fields so you need to adjust it.
$eid = 4;
$vid = 5;
$region = 'aa';
$buyin = 'asd';
$gameID = 5;
$date = 5;
$playerID = 10;
$fields = array(
'EventID' => $eid,
'VenueID' => $vid,
'Region' => $region,
'Buyin' => $buyin,
'GameID' => $gameID,
'PlayerID'=> $playerID,
'Date' => $date,
);
$numItems = count($fields);
$query = "UPDATE `results` SET ";
$i = 0;
foreach($fields as $name => $value) {
++$i;
if($name == 'GameID' || $name == 'PlayerID') {
continue;
}
$query .= sprintf(" `%s` = '%s'%s ", $name, $value, ($i === $numItems ? "": ","));
}
$query .= sprintf(" WHERE `GameID` = '%d' AND `PlayerID` = '%d'", $fields['GameID'], $fields['PlayerID']);
echo $query;
Fields can be maped via $key => $value and then used in foreach loop. Also using sprintf() makes is more clear to read. However, the best option would be using prepared statements.
Also you don't need to make multiple UPDATE queries, just SET more parameters in one query.
$data = array($eid,$vid,$region,$buyin,$GameID,$date,$season,$ID,$Name,$Place,$Points,$Ticket,$Cash,$Runners);
$fields = array('EventID','VenueID','Region','Buyin','GameID','Date','Season','PlayerID','Name','Position','Points','Ticket?','Cash','Runners');
for ($x = 0; $x < (count($data) - 1); $x++){
$sql = "UPDATE results SET " . $fields[$x] . " = '$data[$x]' WHERE results.GameID = $GameID AND results.PlayerID = $ID";
$res = mysqli_query($dbcon, $sql) or die("Update failed. <br>" . mysqli_error($dbcon));
}
Try this by specifying alias.

Querying 2 tables from the same database NOT working : PHP it has to be EASY but I do not see it

I would appreciate some help here, I am new to PHP.
I have one database with two tables ( products and categories ), as you can imagine I want to query both tables in order to have the whole picture. The problem is that it is not working. Before I used to have this info in the same table and it worked, so I guess it has something to do with querying two tables from the same PHP file.
This is the code:
$query = "SELECT id, stock, nombre, foto, descripcion, celda , precio
FROM productos
WHERE 1 = 1";
$result = mysql_query($query);
$num = mysql_numrows($result);
$i=0;
$data = array();
while ($i < $num) {
$data_id[$i] = mysql_result($result,$i,"id");
$data_stock[$i] = mysql_result($result,$i,"stock");
$data_nombre[$i] = mysql_result($result,$i,"nombre");
$data_foto[$i] = mysql_result($result,$i,"foto");
$data_precio[$i] = mysql_result($result,$i,"precio");
$data_celda[$i] = mysql_result($result,$i,"celda");
$data_descripcion[$i] = mysql_result($result,$i,"descripcion");
++$i;
}
$query2 = "SELECT namec, picturec
FROM categorias
WHERE 1 = 1 ";
$result2 = mysql_query($query2);
$num2 = mysql_numrows($result2);
$h = 0;
while ($h < $num2) {
$data_celda_nombre[$h] = mysql_result($result2,$h,"namec");
$data_celda_foto[$h] = mysql_result($result2,$h,"picturec");
++$h;
}
$data = array(
"ids" => $data_id,
"stocks" => $data_stock,
"nombre" => $data_nombre,
"foto" => $data_foto,
"precio" => $data_precio,
"celda" => $data_celda,
"descripcion" => $data_descripcion,
"celda_foto" => $data_celda_foto,
"celda_nombre" => $data_celda_nombre
);
echo (json_encode($data));
The thing is I am not getting named and picturec , WHY?
Can anyone help me?
Thanks a lot.
Roberto L.
Try this code - I haven't really changed anything, just refactored the code so it is more "standard", if this still doesn't do what you want I will try and work out what problem you are still having.
$data_id = $data_stock = $data_nombre = $data_foto = $data_precio = $data_celda = $data_descripcion = $data_celda_nombre = $data_celda_foto = array();
$query = "SELECT id, stock, nombre, foto, descripcion, celda , precio
FROM productos";
if (!$result = mysql_query($query)) die('MySQL Error: '.mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$data_id[] = $row['id'];
$data_stock[] = $row['stock'];
$data_nombre[] = $row['nombre'];
$data_foto[] = $row['foto'];
$data_precio[] = $row['precio'];
$data_celda[] = $row['celda'];
$data_descripcion[] = $row['descripcion'];
}
$query = "SELECT namec, picturec
FROM categorias";
if (!$result = mysql_query($query)) die('MySQL Error: '.mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$data_celda_nombre[] = $row['namec'];
$data_celda_foto[] = $row['picturec'];
}
$data = array(
"ids" => $data_id,
"stocks" => $data_stock,
"nombre" => $data_nombre,
"foto" => $data_foto,
"precio" => $data_precio,
"celda" => $data_celda,
"descripcion" => $data_descripcion,
"celda_foto" => $data_celda_foto,
"celda_nombre" => $data_celda_nombre
);
echo json_encode($data);

Categories