I got problem with function get_row() in WordPress and can't find satisfied answer for my situation. That is a piece of my code
function take_category() {
$table_result = array();
$connect_to = connect_to_db();
$question = "select id_cat, name_cat from category";
if (!($result=$connect_to->get_results($question, ARRAY_N))) {
return false;
}
$prepared_row =$connect_to->prepare("%s", $question);
var_dump($prepared_row);
$row=$conntect_to->get_row($prepared_row, ARRAY_N, 1); // line 689
var_dump($row).'</br>';
for ($count=0; $row=$conntect_to->get_row($question, ARRAY_N, 1);
$count++) {
$table_result[$count] = $row;
.........
return $table_result;
this function is placed into functions.php. Function connect_to_db() is defined in this file as well:
function connect_to_db() {
global $wpdb;
$wpdb = new wpdb('****', '', '****store', 'localhost');
if (!$wpdb) {
return false;
} else{
$wpdb->query('SET autocommit = 1;');
return $wpdb;
}
}
Connection to Database is worked properly. Function prepare works correctly as well. This is var_dump($prepared_row):
string(39) "'select id_cat, name_cat from category'"
But next piece of code
$row=$conntect_to->get_row($prepared_row, ARRAY_N, 1); // line 689
var_dump($row);
shows result NULL: and fatal error
***Fatal error:** Uncaught Error: Call to a member function get_row() on
null in ....\functions.php:689 Stack trace: #0 ...\indeks.php(12):
take_category() #1 {main} thrown in ...\functions.php on line 689*
File indeks.php is a file where function take_category() is called:
$cat_table = take_category();
But if I changes my line 689 like this:
$row=$connect_to->get_row("select id_cat, name_cat from category",
ARRAY_N, 1);
I see var_dump($row):
array(2) { [0]=> string(1) "2" [1]=> string(9) "Textbooks" }
and the same fatal error!
What is wrong with function get_row?
Dear gentelments i found the problem. Sorry for waisting your time...
Should $conntect_to->get_row() not be $connect_to->get_row()?
Typo error in conntect_to-> and connect_to->. Use it carefully.
Variable is mispelled. You set $connect_to earlier. However now you are accessing $conntect_to. Rename it to $connect_to.
Related
I am new to Magento 2. I am testing a class with PhpUnit.
When I run the test I obtain this error:
ArgumentCountError : Too few arguments to function Magenio\Todo\Test\Unit\Service\TaskRepositoryTest::testGetList(), 0 passed in /opt/project/vendor/phpunit/phpunit/src/Framework/TestCase.php on line 1414 and exactly 1 expected
I checked the TestCase.php file and the line 1414 and related lines are those:
protected function runTest()
{
if (\trim($this->name) === '') {
throw new Exception(
'PHPUnit\Framework\TestCase::$name must be a non-blank string.'
);
}
$testArguments = \array_merge($this->data, $this->dependencyInput);
$this->registerMockObjectsFromTestArguments($testArguments);
try {
$testResult = $this->{$this->name}(...\array_values($testArguments));
} catch (\Throwable $exception) {
if (!$this->checkExceptionExpectations($exception)) {
throw $exception;
}
I didn't understand the syntax in this line:
$testResult = $this->{$this->name}(...\array_values($testArguments));
Can anyone explain me what the previous line means, please?
I forgot to ask another thing: what does mean ...\ before array_values?
Hello here is my website i receive this error on my header site
error code :
Warning
: Invalid argument supplied for foreach() in
/home/enghouse/kastratihome.com/wp-content/themes/funiter/framework/includes/theme-functions.php
on line
828
My code :
823 if ( $funiter_enable_vertical == 1 ) :
824 $locations = get_nav_menu_locations();
825 $menu_id = $locations['vertical_menu'];
826 $menu_items = wp_get_nav_menu_items( $menu_id );
827 $count = 0;
828 foreach ( $menu_items as $menu_item ) {
829 if ( $menu_item->menu_item_parent == 0 ) {
830 $count ++;
831 }
832 }
Here is the code please help me with it...
try{
/* [ your iteration on header.php, line 828, likely something that looks like foreach($foo as $f) { ... } */
}
catch(Exception $e){
/* technically, you don't need to do anything here unless it serves you */
/* but it's common to log an error or return a value */
}
You should make sure that you are passingan array to foreach using the is_array function.
If you are not sure whether you're going to pass an array or not, then you can change your code like the following.
If you are not sure it's going to be an array you can always check using the following PHP example code:
if(is_array($value) || is_object($value)){
foreach ($variable as $item) {
//do something
}
}
Check your foreach() in /home/enghouse/kastratihome.com/wp-content/themes/funiter/framework/includes/theme-functions.php and make sure you check whether it is an array before using foreach().
I have a problem with Oracle DB.
<?php
require_once 'includes/conn.php';
function connect_db()
{
if ($c=oci_pconnect(uname,pwd, host,'AL32UTF8'))
return $c;
else
die( "ERROR");
}
$conn=connect_db();
$query = "BEGIN :ds_id :=DS.REG_DS1(:F_NAME);END;";
$stmt=oci_parse($conn,$query);
$f_name='John Doe';
$ds_id=-1;
oci_bind_by_name($stmt, ":ds_id", $ds_id);
oci_bind_by_name($stmt, ":F_NAME", $f_name);
if(oci_execute($stmt))
{
echo 'good';
}
else
print_r(oci_error($stmt));
?>
Here is function REG_DS1
FUNCTION REG_DS1(F_NAME IN VARCHAR) RETURN NUMBER AS
DS_ID NUMBER(8,0):=9988;
BEGIN
-- INSERT INTO TEST VALUES(F_NAME,SYSDATE);
RETURN DS_ID;
END REG_DS1;
When I try to execute this function from Sql Developer, it runs with no problem.
But if I execute from PHP script above, it gives me error:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small ORA-06512: at line 1 [offset] => 0 [sqltext] => BEGIN :ds_id :=DS.REG_DS1(:F_NAME);END; )
If i change DS_ID variable to another number less than 100, it works great from both. but if i set it to a number more than 99, it gets me error from php script.
What can be a problem?
--jst idea and this is not solutioN
variable decleration or some defination problem
oci_connect($ODBuser,$ODBpass,$ODBhost,$db_charset);
should try like this
not like
oci_pconnect(uname,pwd, host,'AL32UTF8')
sample code :
-- just idea
<?php
$file = "../script/param.CML";
$encryptObj = new cast128;
$encryptObj->setkey("SABBIllustrateKey");
$fp=fopen($file,'r');
$strContent = fread($fp, filesize($file));
fclose($fp);
$strContent=$encryptObj->decrypt($strContent);
$ArConnect=split(" ",$strContent);
$OraService=$ArConnect[8];
$OraUser=$ArConnect[9];
$OraDBpass=$ArConnect[10];
$db_charset = 'AL32UTF8';
$cursor=oci_connect($OraUser,$OraDBpass,$OraService,$db_charset);
if(!$cursor)
{
echo "<br>Error In connection:- reason<BR>";
$e = oci_error(); // For oci_connect errors pass no handle
}
?>
<?php
$file = "../script/param.CML";
$encryptObj = new cast128;
$encryptObj->setkey("SABBIllustrateKey");
$fp=fopen($file,'r');
$strContent = fread($fp, filesize($file));
fclose($fp);
$strContent=$encryptObj->decrypt($strContent);
$con=split(" ",$strContent);
$OraService=$con[8];
$OraUser=$con[9];
$OraDBpass=$con[10];
$db_charset = 'AL32UTF8';
$cursor=oci_connect($OraUser,$OraDBpass,$OraService,$db_charset);
if(!$cursor)
{
echo "<br>Error In connection:- reason<BR>";
$e = oci_error(); // For oci_connect errors pass no handle
}
?>
Thx guys, finally I solved problem myself.
The problem was in variable size of returning value
By changing
oci_bind_by_name($stmt, ":ds_id", $ds_id);
to
oci_bind_by_name($stmt, ":ds_id", $ds_id,10,SQLT_INT );
the problem was solved.
I have created below collection query in my custom external magento page--
<?php
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set("display_errors", 1);
ini_set('max_execution_time', 3000);
umask(0);
Mage::init('default');
Mage::app();
$start_date='2016-03-01';
$end_date='2016-04-01';
$orderCollection=Mage::getModel('sales/order')->getCollection();
try{
$orderCollection->getSelect()->join(array('order_history'=> sales_flat_order_status_history), "main_table.entity_id=order_history.parent_id WHERE (order_history.entity_name = 'invoice' OR order_history.entity_name = 'shipment') AND order_history.status = 'complete' AND order_history.created_at >= '".$start_date."' and order_history.created_at < '".$end_date."' ", array('order_history.parent_id'));
}
catch(Exception $e)
{
echo $e->getMessage();
}
this query gives me following error--
Notice: Use of undefined constant sales_flat_order_status_history - assumed 'sales_flat_order_status_history'
I don't know, what wrong there ?
I don't know, whats the problem was there but I resolved this by converting it into my custom query--
<?php
$start_date='2016-04-01 00:00:00'; //yyyy-mm-dd fix
$end_date='2016-05-01 00:00:00'; //yyyy-mm-dd fix+1
$query="SELECT `main_table`.* FROM `sales_flat_order` AS `main_table` INNER JOIN `sales_flat_order_status_history` AS `order_history` ON main_table.entity_id=order_history.parent_id WHERE (order_history.entity_name = 'invoice' OR order_history.entity_name = 'shipment') AND order_history.status = 'complete' AND order_history.created_at >= '".$start_date."' and order_history.created_at < '".$end_date."' GROUP BY `main_table`.entity_id";
$orderCollection=$connectionRead->fetchAll($query);
I would like to upgrade my symfony 2 project from 2.3 to 2.7 LTS version. I have a problem in a repository to get result of a query. In 2.3, this query give me something :
public function findProtectedPublications( $steps, $start, $end)
{
$query= $this->getEntityManager()
->createQueryBuilder()
->select('d.pubRefs')
->from('ImpressionDemandBundle:Event', 'h')
->innerJoin('h.demand','d')
->where('d.protectedPublications = :pub')
->setParameter('pub', 1 )
->andWhere('h.date >= :start')
->setParameter('start', $start )
->andWhere('h.date <= :end')
->setParameter('end', $end )
->andWhere('h.stepId in (:steps)')
->setParameter('steps', $steps )
->orderBy('d.id','ASC')
->getQuery();
$results = $query->getResult();
$publications = array();
if ($results && ! empty ($results)){
foreach($results as $result){
$pubs = $result['pubRefs'];
if ($pubs && ! empty($pubs)){
foreach($pubs as $pub){
$publications[] = $pub;
}
}
}
}
return $publications;
}
But this code doesn't work in earlier version because $pubs variable in an ArrayCollection. So I changed the end of my code with this :
$results = $query->getResult();
$publications = array();
if ($results && ! empty ($results)){
foreach($results as $result){
$pubs = $result['pubRefs'];
var_dump($pubs);
if (! $pubs->isEmpty()){
$arrayPubs = $pubs->toArray();
foreach($arrayPubs as $pub){
$publications[] = $pub;
}
}
}
}
return $publications;
In this part, when I dump the $pubs variable, I have :
object(Doctrine\Common\Collections\ArrayCollection)#131 (2) {
["elements":"Doctrine\Common\Collections\ArrayCollection":private]=>
NULL
["_elements":"Doctrine\Common\Collections\ArrayCollection":private]=>
array(1) {
[0]=>
object(Impression\DemandBundle\Entity\Publication)#125 (5) {
["editor":"Impression\DemandBundle\Entity\Publication":private]=>
string(24) "Journal Le Monde 4-10-13"
["coauthors":"Impression\DemandBundle\Entity\Publication":private]=>
string(12) "Machin Machin"
["title":"Impression\DemandBundle\Entity\Publication":private]=>
string(57) "La tragédie de Lampedusa: s"émouvoir, comprendre, agir."
["nbPages":"Impression\DemandBundle\Entity\Publication":private]=>
float(1)
["nbCopies":"Impression\DemandBundle\Entity\Publication":private]=>
float(40)
}
}
}
So it seems that there are elements in this ArrayCollection, but the test $pubs->isEmpty() gives a true result, so I have nothing in $publications array.
Edit: In fact, the problem seems to be due to my data in the database : for an object previous from my upgrade, I have something like this in the database :
O:43:"Doctrine\Common\Collections\ArrayCollection":1:{s:54:"Doctrine\Common\Collections\ArrayCollection_elements";a:1:{i:0;O:42:"Impression\DemandBundle\Entity\Publication":5:{s:50:"Impression\DemandBundle\Entity\Publicationeditor";s:5:"BREAL";s:53:"Impression\DemandBundle\Entity\Publicationcoauthors";s:5:"MONOT";s:49:"Impression\DemandBundle\Entity\Publicationtitle";s:18:"USA Canada mexique";s:51:"Impression\DemandBundle\Entity\PublicationnbPages";d:150;s:52:"Impression\DemandBundle\Entity\PublicationnbCopies";d:150;}}}
and this gives the error.
For a object add after my upgrade, I have something like this in the database :
O:43:"Doctrine\Common\Collections\ArrayCollection":1:{s:53:"Doctrine\Common\Collections\ArrayCollectionelements";a:1:{i:0;O:42:"Impression\DemandBundle\Entity\Publication":5:{s:50:"Impression\DemandBundle\Entity\Publicationeditor";s:8:"dfg dfgd";s:53:"Impression\DemandBundle\Entity\Publicationcoauthors";s:7:"dfg dfg";s:49:"Impression\DemandBundle\Entity\Publicationtitle";s:5:"fdg d";s:51:"Impression\DemandBundle\Entity\PublicationnbPages";d:5;s:52:"Impression\DemandBundle\Entity\PublicationnbCopies";d:3;}}}
and the function findProtectedPublications() works without errors.
The difference between the two versions is ArrayCollection_elements for the first and ArrayCollectionelements for the second.
To correct this data, I tried with
UPDATE demand SET pub_refs = REPLACE (pub_refs, "ArrayCollection_elements', 'ArrayCollectionelements')
but this doesn't work because of special chars. Trying with
UPDATE demand SET pub_refs = REPLACE (pub_refs, "ArrayCollection�_elements', 'ArrayCollection�elements')
doesn't work better. How can I correct this data ?
Doctrine can populate results as an Array instead of an ArrayCollection, simply change the getResult() call to:
$results = $query->getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
This would be the most efficient way to complete your task however you could also use ArrayCollection's built-in toArray() method to convert its own data to array format:
$publications = $results->toArray();
As the problem seems to be due to a change in the storage of ArrayCollection in database between 2.3 and 2.7 version of symfony, I created an line command to update these in database.