Hello I have a connection problem with my bdd because it does not want to connect. In my error it says that it's a sql query problem, I've looked for it but I can't find it...
I think it is my function query(Table.php) which is not good and therefore my function prepare(Databse.php) which does not validate it.
error:
Fatal error: Uncaught TypeError: PDOStatement::execute(): Argument #1 ($params) must be of type ?array, string given in C:\xampp\htdocs\gra\app\Database.php:61 Stack trace:
#0 C:\xampp\htdocs\gra\app\Database.php(61): PDOStatement->execute('App\Table\Artic...')
#1 C:\xampp\htdocs\gra\app\Table\Table.php(36): App\Database->prepare(' SELECT article...', 'App\Table\Artic...', 'App\Table\Artic...', false)
#2 C:\xampp\htdocs\gra\app\Table\Article.php(18): App\Table\Table::query(' SELECT article...', 'App\Table\Artic...')
#3 C:\xampp\htdocs\gra\pages\home.php(12): App\Table\Article::getLast()
#4 C:\xampp\htdocs\gra\public\index.php(28): require('C:\xampp\htdocs...')
#5 {main} thrown in C:\xampp\htdocs\gra\app\Database.php on line 61
// My prepare function of my Database.php page`
public function prepare($statement, $attributes, $class_name, $one = false){
$req = $this->getPdo()->prepare($statement);
$req->execute($attributes);
$req->setFetchMode(PDO::FETCH_CLASS, $class_name);
if($one){
$datas = $req->fetch();
}else {
$datas = $req->fetchAll();
}
return $datas;
}
//Function query of my Table.php page
public static function query($statement, $attributes = null, $one = false ){
if($attributes){
return App::getDB()->prepare($statement, $attributes, get_called_class(), $one);
} else {
return App::getDB()->query($statement, get_called_class(), $one);
}
}
Here is my github:
https://github.com/xavgdt66/grablog/tree/main/gra
I tried to put execute but it doesn't work.
Related
This question already has answers here:
Use an array in a mysqli prepared statement: `WHERE .. IN(..)` query [duplicate]
(8 answers)
Closed 1 year ago.
I want to bind variable number parameters using call_user_func_array in this way(please read comments at the code):
function dbQuery()
{
global $dbconnection;
if (func_num_args() < 1 || func_num_args() === 2) {
return error('خطای داخلی');
} else if (func_num_args() === 1) {
$stmt = $dbconnection->prepare(func_get_arg(0)); // Creating stmt
if (!$stmt) {
return error('خطای پایگاه داده');
}
if (!$stmt->execute()) {
return error('خطا در تعامل با پایگاه داده');
}
return $stmt;
} else {
$args = func_get_args();
$stmt = $dbconnection->prepare($args[0]); // args[0] is the query with question marks
if (!$stmt) {
return error('خطای پایگاه داده');
}
array_shift($args);
foreach ($args as $index => $arg) {
${'arg' . $index} = $dbconnection->real_escape_string($arg); // making args as variables because bind_params does not allow direct values
$args[$index] = ${'arg' . $index};
}
if (!call_user_func_array($stmt->bind_param,$args)) { // ERROR IS HERE LINE 67
return error('خطای امنیتی پایگاه داده');
}
if (!$stmt->execute()) {
return error('خطا در تعامل با پایگاه داده');
}
return $stmt;
}
}
$stmt = dbQuery('SELECT * FROM payments WHERE id=?','i',31);
$result = dbCheck($stmt);
var_dump($result->fetch_all());
and the error is:
PHP Warning: Undefined property: mysqli_stmt::$bind_param in /home/mwxgaf/w/projects/foodorder/test.php on line 67
PHP Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($function) must be a valid callback, no array or string given in /home/mwxgaf/w/projects/foodorder/test.php:67
Stack trace:
#0 /home/mwxgaf/w/projects/foodorder/test.php(76): dbQuery()
#1 {main}
thrown in /home/mwxgaf/w/projects/foodorder/test.php on line 67
call_user_func_array expects a callable as the first argument, and $stmt->bind_param is the wrong format for this. [$stmt, 'bind_param'] would be the proper callable
Source: https://www.php.net/manual/en/language.types.callable.php
A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1. Accessing protected and private methods from within a class is allowed.
What is the mean Argument of the type array, null given,
Error:
Fatal error: Uncaught TypeError: Argument 3 passed to
Magento\Framework\View\Element\UiComponentFactory::mergeMetadataElement()
must be of the type array, null given, called in
/var/www/html/giriusa/lib/internal/Magento/Framework/View/Element/UiComponentFactory.php
on line 384 and defined in
/var/www/html/giriusa/lib/internal/Magento/Framework/View/Element/UiComponentFactory.php:337
Stack trace: #0
/var/www/html/giriusa/lib/internal/Magento/Framework/View/Element/UiComponentFactory.php(384):
Magento\Framework\View\Element\UiComponentFactory->mergeMetadataElement(Array,
'configurableExi...', NULL, false) #1
/var/www/html/giriusa/lib/internal/Magento/Framework/View/Element/UiComponentFactory.php(412):
Magento\Framework\View\Element\UiComponentFactory->mergeMetadataItem(Array,
Array, false) #2
/var/www/html/giriusa/lib/internal/Magento/Framework/View/Element/UiComponentFactory.php(412):
Magento\Framework\View\Element\UiComponentFactory->mergeMetadataItem(Array,
Array, false) #3 /var/www/html/giriusa/lib/internal/Magento/Fram in
/var/www/html/giriusa/lib/internal/Magento/Framework/View/Element/UiComponentFactory.php
on line 337
Path : lib/internal/Magento/Framework/View/Element/UiComponentFactory.php
UiComponentFactory::mergeMetadataElement() must be of the type array,
null given
protected function mergeMetadataElement(array $bundleComponents, $name, array $data, $reverseMerge = false)
{
if (isset($bundleComponents[$name])) {
$bundleComponents[$name] = $reverseMerge
? array_replace_recursive($data, $bundleComponents[$name])
: array_replace_recursive($bundleComponents[$name], $data);
return [$bundleComponents, true];
} else {
foreach ($bundleComponents as &$childData) {
if (isset($childData['attributes']['class'])
&& is_a($childData['attributes']['class'], \Magento\Ui\Component\Container::class, true)
&& isset($childData['children']) && is_array($childData['children'])
) {
list($childData['children'], $isMerged) = $this->mergeMetadataElement(
$childData['children'],
$name,
$data,
$reverseMerge
);
if ($isMerged) {
return [$bundleComponents, true];
}
}
}
}
return [$bundleComponents, false];
}
Note: After migration from 1.9 getting above error before its working good.
How to fix this error:
Warning: Illegal offset type in isset or empty in F:\xampp\htdocs\digikalamvc\core\model.php on line 140
Fatal error: Uncaught Error: Unsupported operand types in F:\xampp\htdocs\digikalamvc\models\model_showcart4.php:90 Stack trace: #0 F:\xampp\htdocs\digikalamvc\controllers\showcart4.php(31): model_showcart4->saveOrder(Array) #1 F:\xampp\htdocs\digikalamvc\core\app.php(34): Showcart4->saveorder() #2 F:\xampp\htdocs\digikalamvc\index.php(7): App->__construct() #3 {main} thrown in F:\xampp\htdocs\digikalamvc\models\model_showcart4.php on line 90
Code in model.php on line 140: ( if (isset($_SESSION[$name])) { )
public static function sessionGet($name) {
if (isset($_SESSION[$name])) {
return $_SESSION[$name];
} else {
return false;
}
}
Code in model_showcart4.php on line 90:
$priceTotal =$basketPrice-$basketDiscount+$postprice;
Code model_showcart4:
$basket = $basketInfo[0];
$basket = serialize($basket);
$basketPrice = $basketInfo[1];
$basketDiscount = $basketInfo[2];
$postprice = $this->calculatePostPrice($addressInfo['city']);
$postType = self::sessionGet(['postType']);
if ($postType == 1) {
$postprice = $postprice['pishtaz'];
} elseif ($postType == 2) {
$postprice = $postprice['sefareshi'];
}
$priceTotal =$basketPrice-$basketDiscount+$postprice;
Code showcart4.php on line 31:
function saveorder() {
$this->model->saveOrder($_POST);
}
$postType = self::sessionGet(['postType']);
as you can see, argument is an array.
so here
isset($_SESSION[$name]) code gets to be invalid, because array key should be integer or string, not an array.
$postType = self::sessionGet('postType'); - this should work, I guess
Your warning indicates that the key given to your function doesn't exist in the $_SESSION array. In model_showcart4.php on line 83 you passed an array to your function, which is probably the cause of this error.
I would recommend to add an extra check in your if statement. Use array_key_exists function to check if the key actually exists in the given array, so if for some reason a key is given which simply doesn't exists, your code wouldn't break.
F:\xampp\htdocs\digikalamvc\core\model.php
public static function sessionGet($name) {
if (array_key_exists($name, $_SESSION) && isset($_SESSION[$name])) {
return $_SESSION[$name];
} else {
return false;
}
}
I have a MongoDBException:
"Invalid find by call Bundle\Document\Property::$fieldName
(criteriaWith)".
I don't understand what is wrong here. Can someone help me please?
Here's the Stack Trace:
1 . in vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/MongoDBException.php at line 38
public static function invalidFindByCall($documentName, $fieldName, $method) {
return new self(sprintf('Invalid find by call %s::$fieldName (%s)', $documentName, $fieldName, $method));
}
at
MongoDBException ::invalidFindByCall ('\Bundle\Document\Property',
'criteriaWith', 'findByCriteriaWith')
in vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/DocumentRepository.php at line 222
if ($this->class->hasField($fieldName)) {
return $this->$method(array($fieldName => $arguments[0]));
} else {
throw MongoDBException::invalidFindByCall($this->documentName, $fieldName, $method . $by);
}
at DocumentRepository ->__call ('findByCriteriaWith', array(array('name' => 'ho'))) in src/Bundle/Controller/PropertyController.php at line 286
else {
criteria['name'] = $name;
$entities = $repository->findByCriteriaWith($criteria);
}
The Log Messages:
CRITICAL request Uncaught PHP Exception
Doctrine\ODM\MongoDB\MongoDBException: "Invalid find by call
Bundle\Document\Property::$fieldName (criteriaWith)" at
vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/MongoDBException.php
line 38 Context: { "exception":
"Object(Doctrine\ODM\MongoDB\MongoDBException)" }
Let's go through ODM's document repository magic __call method (as that's what is called since findByCriteriaWith does not exist) and highlight interesting parts.
First we see
if (strpos($method, 'findBy') === 0) {
$by = substr($method, 6, strlen($method));
$method = 'findBy';
}
Your method name starts with "findBy" so we will enter this block leaving us later with $method = 'findBy' and $by = 'CriteriaWith' Later on $by gets camelized, as that's standard for field/property name, and leaves us with $fieldName = 'criteriaWith'. Now next thing we see is $this->class->hasField($fieldName) check which results in exception you got because your class does not have a criteriaWith field.
Summing up $repository->findBySomething($value); is equivalent to $repository->findBy(['something' => $value]) and throws exception if field does not exist in your doocument.
Look this example code:
I'm getting the error:
Fatal error: Uncaught Error: Cannot use object of type Closure as array in C:\xampp\htdocs\dermaquality\test.php:11 Stack trace: #0 C:\xampp\htdocs\dermaquality\test.php(20): test(Object(Closure)) #1 {main} thrown in
$array = array(
'hello' => function() {
echo "HEllo world";
}
);
function test( $func )
{
if (is_callable( $func['hello'] )) {
$func['hello']();
}
else {
$func();
}
}
echo "Executing 1 <br/>";
test( $hello = function() {"Hello world";} );
echo "Executing 2 <br/>";
test( $array['hello'] );
exit;
How can I call $func if $func is function or if $func['hello'] is function?
Thanks.
Problem is in if (is_callable( $func['hello'] )) { because you dont know if $func is an array.. btw you dont put array as parameter in test( $array['hello'] ); you put just function...
function test( $func )
{
if (is_callable($func)) {
$func();
}
else if (is_array($func)){
if(isset($func['hello']) && is_callable($func['hello'])){
$func['hello']();
}else{
// unknown what to call
}
}else{
// unknown what to call
}
}