function __construct(mysqli $db, $country = NULL, $sport = NULL) {
$this->db = $db;
$this->country = base64_decode($country);
$this->sport = base64_decode($sport);
}
public function GetColor($colorcode) {
$query = 'SELECT naam_nl FROM colors WHERE code = $colorcode';
$result = $this->db->query($query);
while ($row = $result->fetch_assoc()) { // Line 21
echo $row['naam_nl'];
}
$result->close();
}
Gives me:
Fatal error: Call to a member function fetch_assoc() on a non-object in /home/cloud/public/td/teamdresser.class.php on line 21
So I tried:
$result = $this->db->query($query);
while ($row = $this->db->fetch_assoc($result)) {
echo $row['naam_nl'];
}
And then...
Fatal error: Call to undefined method mysqli::fetch_assoc() in /home/cloud/public/td/teamdresser.class.php on line 21
I'm doing something wrong.. Can someone point me in the right direction?
Like the comments say, you need to do some error checking:
public function GetColor($colorcode) {
$query = 'SELECT naam_nl FROM colors WHERE code = $colorcode';
$result = $this->db->query($query);
if ($result === false) {
// Throw or handle an error here
} else {
while ($row = $result->fetch_assoc()) { // Line 21
echo $row['naam_nl'];
}
$result->close();
}
}
Additionally, you need the mysqlnd drivers: Fatal error: Call to undefined method mysqli_result::fetch_all()
This part is wrong:
public function GetColor($colorcode) {
$query = 'SELECT naam_nl FROM colors WHERE code = $colorcode';
PHP won't interpolate $colorcode when using single quotes. Use double quotes intead:
$query = "SELECT naam_nl FROM colors WHERE code = $colorcode";
Comments:
Always check for the return value! That makes spotting errors much easier.
Why don't you use prepared statements?
Related
I currently working on dashboard using PHP. I need to fetch data from database inside SQLyog, and transfer it into a graph. The problem is I keep getting this error; Fatal error: Call to undefined method MDB2_Error::execute() on line 30 in the class coding. The error is on $res = $query->execute($data); Can someone help me with my coding?
Here is the code for class.
<?php
class fetch_ranking extends MDB2{
var $con;
var $tbl_user;
var $tbl_isi_scopus_webo;
function fetch_ranking ($dsn) {
$this->con =& MDB2::singleton($dsn, $options=null);
//echo $this->con->getMessage();
if (PEAR::isError($this->con)) {
die($this->con->getMessage());
}
$this->tbl_user = 'user';
$this->tbl_isi_scopus_webo = 'ISI_SCOPUS_WEBO';
}
function fetch_webojuly16(){
$types = array();
$sql = "select university, criteria_rank as presence
from isi_scopus_webo
where month='july'
and year='2016'
group by university" ;
$query = $this->con->prepare($sql,$types,MDB2_PREPARE_RESULT);
$data = array();
$res = $query->execute($data);
if (PEAR::isError($res)) {
die($res->getMessage());
}
$this->con->setFetchMode(MDB2_FETCHMODE_ASSOC);
$valrec = $res->fetchAll();
return $valrec;
}
}?>
And this is for the graph.
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/config/siteconf.php');
require_once($basehome.'/config/default.php');
require_once($basedir.'/mod/ppsg/dsn.php');
require_once($basehome.'/mod/ppsg/class/fetch_data_from_db_isi_scopus_webo.php');
$current_year = date('Y', strtotime(date('Y')));
//$fhObj = new fetch_ranking($ranking);
$hostname="192.168.111.55";
$username="devopt";
$password="G0devopt$";
$dbname="ranking";
//Connect to the database
$connection = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
mysql_select_db($dbname, $connection)
or die("Could not select examples");
$xdata=array();
$ydata=array();
$xdata['name'] = 'PRESENCE';
$ydata['name'] = 'UNIVERSITY';
$ydata['color'] = '#ABEBC6';
//Call to a member function fetch_webojuly16() on a non-object
$webojuly16 = $fhObj->fetch_webojuly16();
foreach($webojuly16 as $wj16){
$xdata['data'][] = $wj16['presence'];
$ydata['data'][] = $wj16['university'];
}
$json_arr = array($xdata,$ydata);
print json_encode($json_arr, JSON_NUMERIC_CHECK);
?>
This is the coding for graph
The $this->con->prepare($sql,$types,MDB2_PREPARE_RESULT); call might return a MDB2_Error object in case the prepare() statement fails, as documented on http://pear.php.net/package/MDB2/docs/latest/MDB2/MDB2_Driver_Common.html#methodprepare. You have to check with PEAR::isError if that is the case or not. In your case it is, so you cannot call $res = $query->execute($data); on a MDB2_Error object, and that's exactly what the error message is telling you.
Add the if statement to check for errors on the $query variable and check the error messages you get.
I have a problem with this code:
if($sql = $this->database->query("SELECT * FROM warenkorb_artikel WHERE WarenkorbID = '".$erg['WarenkorbID']."'"))
{
while($erg = $sql->fetch_assoc())
{
[...]
}
}
I am getting the error:
Fatal error: Call to a member function fetch_assoc() on a non-object
in [...] on the lin where the "while($eg = $sql->fetch_assoc())" is.
Can anyone help me?
You need to break things down so you can capture any errors you make in a query that are returned by the mysqli API.
$sql = "SELECT *
FROM warenkorb_artikel
WHERE WarenkorbID = '{$erg['WarenkorbID']}'";
$result = $this->database->query($sql);
if ( $result === false ) {
echo $this->database->error;
exit;
}
// do you really need a while loop, as it look like you might be
// selecting a row using a unique key?
while($row = $result->fetch_assoc()){
[...]
}
I get this error when trying to run this, what do it mean?
if(isset($_POST['submit']))
{
$date = $_POST['date'];
$partySize = $_POST['partysize'];
$catering = $_POST['catering'];
print_r($date);
print_r($partySize);
print_r($catering);
include "/diska/www/include/coa123-13-connect.php";
$host='co-project.lboro.ac.uk';
$dbName='coa123wdb';
$dsn = "mysql://$username1:$password1#$host/$dbName"; //Data Source Name
require_once('MDB2.php'); //Just include this line into your program - you do not have to have the source in your directory
$db =& MDB2::connect($dsn); //Try to make a connection
if (PEAR::isError($db)) {
die($db->getMessage());
}
//step 1 - query
$sql = "SELECT * FROM venue
WHERE capacity >= $partySize";
//step 2 - executing the query
$result =& $db->query($sql);
if (PEAR::isError($sql)) {
die($result->getMessage());
}
$valueIDArray = array();
while($row = $result -> fetchrow()){
$valueIDArray[] = $row[0];
}
$values = implode(',', $valueIDArray);
$query = "SELECT * FROM venue_booking
WHERE venue_id IN ($values)";
//step 2 - executing the query
$result1 =& $db->query($query);
if (PEAR::isError($query)) {
die($result1->getMessage());
}
while($row = $result1 -> fetchrow()){
$idValue[] = $row[0];
$dateValues[] = $row[1];
}
availableDate($dateValues,$date,$idValue, $db); //Line error points to
function availableDate($bookedDates, $date, $idValue, $db){
I have commeted on the line the error points to, this file works when its in its own PHP file but when inside the if(isset($_POST['submit'])) statement it does not work. What am I doing wrong?
Move the function definition outside the if statement. There's almost never a good reason to do that -- the only excuse might be if you wanted different definitions of the function depending on a condition, but that doesn't seem to be what you're doing. If you define a function inside an if, you have to define it before you call it; functions defined at top-level can be called from anywhere.
call availableDate after it's defined, if you already have to define it inside of if statement.
Ex.
function availableDate($bookedDates, $date, $idValue, $db){
...
}
//and then call it...
availableDate($dateValues,$date,$idValue, $db); //Line error points to
EDIT:
Example of non-working function defined inside of conditional statement
if(1){
func('a');
function func($a){
echo $a;
}
}
This wont work, but this will:
if(1){
function func($a){
echo $a;
}
func('a');
}
I have the below snippet of code. When I run the whole program, it fails at this section with the error Fatal error: Call to a member function fetch() on a non-object in <filename> on line 26. I'm really not sure what I should do to fix the problem. If it matters, I ran the SQL query against my database and it returns exactly what it should.
if(isset($_POST['submit'])) {
$query = 'SELECT email FROM users WHERE email=:email';
$query_params = array(':email' => $_POST['email']);
try {
$stmt = $conn->prepare($query);
$result = $stmt->execute($query_params);
} catch(PDOException $ex) {
echo $ex->getMessage();
}
$row = $result->fetch(); //fails on this line
if(empty($results)) {
$passset = 1;
}
}
PDOStatement::execute returns TRUE or FALSE, not a result object.
Change this:
$row = $result->fetch();
To this:
$row = $stmt->fetch();
I'm trying to call my function, but she's wrong.
I believe it is in connection variable.
Connection:
$conn = mysqli_connect('','','', '');
if(mysqli_connect_errno()) {
header("Location: error.php");
exit();
}
Function:
function t_car($id) {
global $conn;
$s_t_car = "SELECT *
FROM t_car
WHERE session='$id'";
$s_t_car_return = mysqli_query($conn, $s_t_car) or die("Erro SQL.".mysqli_error());
return $s_t_car_return;
}
Call Function:
$s_t_car_return = t_car($conn, $_SESSION['session_client']);
if(mysqli_num_rows($s_t_car_return )!=0) {
while($r_t_car = mysqli_fetch_array($s_t_car_return )) {
}
}
Error:
Catchable fatal error: Object of class mysqli could not be converted to string
At first you need to enter your settings from your MySQL server (mysql db).
$connection = mysqli_connect("HOSTNAME","USERNAME", "PASSWORD","DATABASE");
You can then use an if statement to check if the connection to the server has been made, if so, continue execution of following code, otherwise die();
If you want to fetch the data see below here:
$res = $connection->query("SELECT finger FROM hand WHERE index = 3");
while($row = $res->fetch_array())
{
print_r($row);
}
mysqli_query() returns a result. You have to fetch the result to do something with it.
$res = mysqli_query($conn, $s_t_car) or die("...");
$s_t_car_return = mysqli_fetch_row($res);