Solve Call to a member function combinestring() on a non-object - php

I met a problem when Call to a member function combinestring() on a non-object.
**Index.php**
inlcude("string.php");
calldata('usa');
**string.php**
$a=new a();
funciton calldata($val){
$st1="select a from table 1 where country=".$a->combinestring($val);
return $st1;
}
**Class A**
function combinestring($abc){
Return "'".$abc."'";
}
Unknow $a->combinestring($val);
How to solve this problem.
Best Regards

You're getting error
Call to a member function combinestring() on a non-object
because you are calling a member function on the variable that is not an object. That means $a is not an object.
In string.php , you cannot use $a inside function definition because variable has local scope. You cannot access that object instance like that. You can however do this by using global variable.
Your string.php file should be like this:
$a=new a();
funciton calldata($val){
global $a;
$st1="select a from table 1 where country=".$a->combinestring($val);
return $st1;
}
Head to this link for more information on variable scope: http://php.net/manual/en/language.variables.scope.php

Use PDO.
funciton calldata($val){
$st1="select a from table 1 where country = ?";
$pdo = new PDO('host', 'user', 'pass');
$result = $pdo->prepare($st1)->execute($val);
return $result;
}
This is a lot different from what you are doing, but your a class does not escape the input to the query and that's bad.

Related

how to get num_rows() on a non-object in CI

so i'd like to get the response from my model:
function get_list_sales_kit(){
$brispot = $this->load->database('brispot',TRUE);
$brispot->select('id,title,imgurl,description');
$qrydata = $brispot->get('saleskit');
$brispot->close();
return $qrydata->result();
}
and this is the function to call that model:
function salesKit2($request){
$result = new stdClass;
$user='';
$CI =& get_instance();
$CI->load->library('libs_bearer');
$CI->load->library('libs_brispot');
$CI->load->model('service_model');
$datapost = json_decode($request);
if(isset($datapost->user)){
$user = substr('00000000'.$CI->security->xss_clean(trim($datapost->user)),-8);
if($CI->libs_bearer->cekToken($user)==true){
$getdata = $CI->service_model->get_list_sales_kit();
if($getdata->num_rows()>0){
$result->responseCode='00';
$result->responseDesc='Inquiry berhasil.';
$result->responseData=$getdata->result();
}
}
but i got eror result like fatal eror Call to a member function num_rows() on a non-object.
i confused how to call non object on num_row, or is there anything to replace num_row to get the response?
If you really want to return the result from your get_list_sales_kit() function the only way to get the number of rows is to count($getdata) as Lawrence said in the comments.
With that being said, I sometimes return the database object itself like so:
function get_list_sales_kit(){
$brispot = $this->load->database('brispot',TRUE);
$brispot->select('id,title,imgurl,description');
$qrydata = $brispot->get('saleskit');
$brispot->close();
return $qrydata; // this line changed
}
as such you can run any CI database functions on it like row() result() num_rows() and so on. Nothing has to be changed in the second code snippet as for some reason you are already accessing the result() object even though you are returning it in the first function.
Note: I'm not sure if close() will affect the objects being called after the fact. Why are you closing the database after doing one thing?

Referencing the same variable across two different classes

<?php
class Foo{
public $basket;
public function __construct()
{
$this->basket = 1;
}
public function getBasket(){
return $this->basket;
}
}
class Bar{
public function __construct(&$basket)
{
$basket++;
}
}
$newFoo = new Foo();
$newBar = new Bar($newFoo->getBasket());
echo $newFoo->getBasket();
?>
I am hoping to initialise the $basket value in one class and manipulate the same variable via another class. Unfortunately, I keep getting the "Notice: Only variables should be passed by reference in " error message.
Question: How can I change the code to make this happen? Thank you.
Change
$newBar = new Bar($newFoo->getBasket());
To
$basket = $newFoo->getBasket();
$newBar = new Bar($basket);
The first way doesn't work because PHP doesn't have any variable with which to hold the value you're passing to new Bar() As a consequence, nothing can be passed by reference.
The second way works because the $basket var is a fixed reference in memory, so it can be passed by reference to new Bar()
You asked in comments:
have changed my code to yours. echo $newFoo->getBasket(); produces 1
(I was hoping for 2).
1 is produced because each call to getBasket() gives you a fresh copy of the class variable. The $basket that I passed to new Bar() equals 2, but that's not what you're echoing.
If you want the result of getBasket() and the variable $basket to refer to the same reference in memory, you need to make two changes:
1 Change the function declaration to:
public function &getBasket()
2 Change how you store the function result to:
$basket = &$newFoo->getBasket();
Now your echo will return 2 because you would have a unique basket reference throughout your code
See the docs

Get time difference between db time values and today's time in mysql [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
So I'm refactoring my code to implement more OOP. I set up a class to hold page attributes.
class PageAtrributes
{
private $db_connection;
private $page_title;
public function __construct($db_connection)
{
$this->db_connection = $db_connection;
$this->page_title = '';
}
public function get_page_title()
{
return $this->page_title;
}
public function set_page_title($page_title)
{
$this->page_title = $page_title;
}
}
Later on I call the set_page_title() function like so
function page_properties($objPortal) {
$objPage->set_page_title($myrow['title']);
}
When I do I receive the error message:
Call to a member function set_page_title() on a non-object
So what am I missing?
It means that $objPage is not an instance of an object. Can we see the code you used to initialize the variable?
As you expect a specific object type, you can also make use of PHPs type-hinting featureDocs to get the error when your logic is violated:
function page_properties(PageAtrributes $objPortal) {
...
$objPage->set_page_title($myrow['title']);
}
This function will only accept PageAtrributes for the first parameter.
There's an easy way to produce this error:
$joe = null;
$joe->anything();
Will render the error:
Fatal error: Call to a member function anything() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/casMail/dao/server.php on line 23
It would be a lot better if PHP would just say,
Fatal error: Call from Joe is not defined because (a) joe is null or (b) joe does not define anything() in on line <##>.
Usually you have build your class so that $joe is not defined in the constructor or
Either $objPage is not an instance variable OR your are overwriting $objPage with something that is not an instance of class PageAttributes.
It could also mean that when you initialized your object, you may have re-used the object name in another part of your code. Therefore changing it's aspect from an object to a standard variable.
IE
$game = new game;
$game->doGameStuff($gameReturn);
foreach($gameArray as $game)
{
$game['STUFF']; // No longer an object and is now a standard variable pointer for $game.
}
$game->doGameStuff($gameReturn); // Wont work because $game is declared as a standard variable. You need to be careful when using common variable names and were they are declared in your code.
function page_properties($objPortal) {
$objPage->set_page_title($myrow['title']);
}
looks like different names of variables $objPortal vs $objPage
I recommend the accepted answer above. If you are in a pinch, however, you could declare the object as a global within the page_properties function.
$objPage = new PageAtrributes;
function page_properties() {
global $objPage;
$objPage->set_page_title($myrow['title']);
}
I realized that I wasn't passing $objPage into page_properties(). It works fine now.
you can use 'use' in function like bellow example
function page_properties($objPortal) use($objPage){
$objPage->set_page_title($myrow['title']);
}

ERROR in PDO : Call to a member function prepare() on null

I have a problem with prepare function ==> Call to a member function prepare() on null
i have tow pages classo.php and index.php
classo.php :
<?php
class classo
{
function connection(){
$db=new pdo ('mysql:host=localhost;dbname=pronostic','root','');
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
}
function insererDonne($pseudo,$password)
{
global $db;
classo::connection();
$donne=array(
'user' =>$pseudo,
'pass' =>$password
);
$req="INSERT INTO users (user,pass) VALUES (:user,:pass)";
$sql=$db->prepare($req);
$sql->execute($donne);
}
}
?>
index.php:
<?php
require('classo.php');
$data=new classo();
$data->insererDonne('dsds','tosdsta');
?>
Do you have an idea as to how I can resolve this? This is the first time I've received this error from PHP and I'm kind of new coding in PHP with objects & classes. Could someone help me fix this problem please?
There are 2 big issues in your code:
Variable visibility
Static call
In detail:
In oop you should forget about global variables. They are against the principle of encapsulation. Moreover, you do not even have any global variable in your code, so global $db; line is meaningless. Declare a private $db variable on class level (property) initialise it in the connection() method and access it in the insert method.
You are calling the connection method as classo::connection();. However, you would need to declare connection method as static. Either declare your connection method as static (but then change $db into static as well), or call it as a regular method using $this.

PHP Change of object class

i use classes for some typical MySQL queries, like getting specific users, news etc. And afterwards I call function query(), which calls mysqli class and query and returns the query like this:
class Users {
[...]
public function query() {
$mysql = new mysqli([...]);
$mysql->query($this->query);
return $mysql;
}
}
When I use it for ex. in $variable like:
$variable = new Users();
$variable->setNew($params)->query();
I have still stored object of Users in $variable. Is there any way to automatically 'update' the variable to be object of mysqli_result (in this case or different class in simillar situation) without using $variable = $variable->setNew($params)->query();?
Thank you for your advice.
You can do this in a one liner:
$variable = (new Users())->setNew($params)->query();
$variable now contains the return of query()

Categories