Error: Call to undefined method mysqli_result::fetch_both() - mysqli

I'm trying to create a dummy login page as a practice. I keep getting this error:
Call to undefined method mysqli_result::fetch_both()
Code:
<?php
if (isset($_POST['LOGIN'])){
$EMAIL = $_POST['loginusernameinput'];
$PASS = $_POST['loginpasswordinput'];
$result = $conn->query("SELECT * FROM userinformationtbl WHERE Email ='$EMAIL' AND password ='$PASS'");
$row = $result -> fetch_both(MYSQLI_BOTH);
session_start();
$_SESSION["userID"] = $row["userID"];
header ('location: account.php');
}
?>

Unless you defined it in your code, fetch_both is not a PHP function.
That's why you get:
Call to undefined method mysqli_result::fetch_both()
PDO::FETCH_BOTH is a constant in the PDO class, which means it can only be used with a PDO connection and PDO functions.
MYSQLI_BOTHis a MySQLi constant.
Fix your code:
$row = $result->fetch_array(MYSQLI_BOTH);
Documentation: mysqli_result::fetch_array

Related

PostgreSQL query works but not with php

I want to make a php query to a PostgreSQL database. I tested the query in the server and returns, but when I try it in php:
<?php
//Check the input
if (!isset($_POST['variable']))
echo "Address not selected";
$input = $_POST['variable'];
//$input = ucfirst(trim($_POST['variable']));
//echo $input;
$conn = pg_connect("host=localhost port=5432 dbname=geocoder user=postgres");
if (!$conn)
echo "Could not connect to server..";
$sql = "SELECT (addy).stateabbrev FROM geocode($input);";
$result = pg_query($conn, $sql);
if (!$result)
echo "Query did not executed..";
?>
I get that "Query did not executed..";
The string for the QUERY is taken from a html page using javascript.
In the error.log of Apache2 i get:
PHP Warning: pg_query(): Query failed: ERROR: syntax error at or near "Penn"\nLINE 1: SELECT (addy).stateabbrev FROM geocode(O
What can be the point here?
Sanitize the user supplied data:
$input = pg_escape_literal($conn, $input);
$sql = "SELECT (addy).stateabbrev FROM geocode($input);";
I managed to find the problem, which is the fact that $input variable from geocode() function, must be surrounded by single quotes:
geocode('$input')
:)

Joomla 3.0 component $params = $app->getParams();

I'm using Joomla 3.0 but at this point I can not use component because of a small problem.
This is the error, all other errors I could easily solve by adding Legacy to the class like this JView became JViewLegacy
However for the error beneath I couldn't find a solution:
Any help would be great!
The error:
Fatal error: Call to a member function getParams() on a non-object in
/var/www/g35003/mywebsite.nl/HTML/administrator/components/
com_taxonomy/taxonomy.php on line 16
The code line 16 is marked.
defined( '_JEXEC' ) or die( 'Restricted access' );
global $mainframe;
$params = $app->getParams(); /** <-- Line 16 */
require_once (JPATH_COMPONENT.DS.'controller.php');
$controller = new TaxonomyController();
$controller->execute(JRequest::getCmd('task'));
$controller->redirect();
global $mainframe; has been deprecated since Joomla 2.5 I believe. To get the parameters, you can use the following code:
$params = JComponentHelper::getParams('com_taxonomy');
$test = $params->get('param_name');
Try this
defined( '_JEXEC' ) or die( 'Restricted access' );
$app = &JFactory::getApplication();
$params = $app->getParams(); /** <-- Line 16 */
require_once (JPATH_COMPONENT.DS.'controller.php');
$controller = new TaxonomyController();
$controller->execute(JRequest::getCmd('task'));
$controller->redirect();
For getting the menu item's params in my view I used the following:
$menu = JFactory::getApplication('site')->getMenu()->getActive();
$this->params = $menu->params;

$this->input->xss_clean($data) giving fatal error with codeigniter

I am getting an error
Fatal error: Call to undefined method CI_Input::xss_clean()
following is the setting and code that I've written.
config.php
$config['global_xss_filtering'] = TRUE;
$data=array();
$data['fname']=$this->input->post("fname");
$this->input->xss_clean($data)
xss_clean() method does not exist in input, but in security.
$data = array();
$data['fname'] = $this->input->post("fname");
$this->security->xss_clean($data);

Fatal error: Call to a member function query() on a non-object in [duplicate]

This question already has answers here:
Call to a member function on a non-object [duplicate]
(8 answers)
Closed 10 years ago.
Fatal error: Call to a member function query() on a non-object on line:
$result = $conn->query($sql) or die(mysqli_error());
Who knows whats wrong and how to fix it?
<?php
function dbConnect($usertype, $connectionType = 'mysqli') {
$host = 'localhost';
$db = 'phpsols';
if ($usertype == 'read') {
$user = 'psread';
$pwd = '123';
} elseif ($usertype == 'write') {
$user = 'pswrite';
$pwd = '123';
} else {
exit('Unrecognized connection type');
}
if ($connectionType == 'mysqli') {
return new mysqli($host, $user, $pwd, $db) or die ('Cannot open database');
} else {
try {
return new PDO("mysql:host=$host;dbname=$db", $user, $pwd);
} catch (PDOException $e) {
echo 'Cannot connect to database';
exit;
}
}
}
// connect to MySQL
$conn = dbConnect('read');
// prepare the SQL query
$sql = 'SELECT * FROM images';
// submit the query and capture the result
**$result = $conn->query($sql) or die(mysqli_error());**
// find out how many records were retrieved
$numRows = $result->num_rows;
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Connecting with MySQLi</title>
</head>
<body>
<p>A total of <?php echo $numRows; ?> records were found.</p>
</body>
</html>
The culprit is most likely this line:
return new mysqli($host, $user, $pwd, $db) or die ('Cannot open database');
The do xyz or die() construct leads to funny behaviour in conjuction with the return statement (i.e. the whole thing is interpreted as an OR expression and because new mysqli will never be false, the "die" is never processed.). See a similar case here.
Do this instead:
$result = new mysqli($host, $user, $pwd, $db) ;
if (!$result) die (....);
return $result;
Also, slightly related, I think you will never catch a PDO connection error because this:
return new PDO("mysql:host=$host;dbname=$db", $user, $pwd);
will always exit the function, and never reach the catch block. As with your actual problem, the solution is to pass the object to a $result variable first.
I know this has been answered, but just wanted to add my own 2 cents worth....
I was googling "return new mysqli" to fix this very problem. The code is a snippet from an e-book (PHP Solutions. Dynamic Web Design Made Easy.) - 2nd Edition if I'm not too mistaken! Ironically the book is supposed to be teaching PHP basics (in this case DB connection methods) to poor souls like myself and easyrider. I have very limited understanding, as this book is my own introduction to PHP coding but I managed to suss out that creating or rather instantiating an actual object, passing it to a temporary variable and then returning that variable was the solution.
Annoyingly, this was demonstrated correctly in the first edition of said publication.
I dont know if Google will cache or index my post, but it might help others who are learning from the book, to discover that the code example is flawed, and as the time of writing, the error is not listed in the errata on the publishers website.

Get current logged user is throwing exception in joomla

I am trying this in Joomla
, but i getting error
Catchable fatal error: Object of class JUser could not be converted to string in C:\xampp\htdocs\Joomla\components\com_jumi\jumi.php(25) : eval()'d code on line 3
Try below instead of your code.
<?php
$user =& JFactory::getUser();
print_r($user);
?>
This will work because the first line will return the object and you are trying to print the object using echo.
If you want to get user name the try below code.
<?php
global $mainframe;
$User_name = $mainframe->getuser()->get('name');
?>
above code will return name of the logged in user.