php file isn't recognising my sqlite3 db mydb.sq3 - php

I'm running xampp.
I created a basic db call mydb.sq3 and a basic table called panels. That all seems to work fine because when I call SELECT * FROM panels; from the command line it is all good. But there is an error that keeps getting thrown when i run my php file:
Fatal error: Call to undefined function query() in > C:\xampp\htdocs\example\myphp.php on line 10
<?php
$db = new SQLite3('mydb.sq3');
$result = $db-->query('SELECT * FROM panels');
while ($row = $result->fetchArray(SQLITE3_ASSOC))
{
echo $row['firstname'] . ': $' . $row['surname'] . '<br/>';
}
unset($db);
?>
Cheers

There is an error in line 3
Use this-
$result = $db->query('SELECT * FROM panels');
Instead of this -
$result = $db-->query('SELECT * FROM panels');
Simple typo error you used '--' instead of '-' Enjoy :)

Related

PHP 5.2 app using Codeigniter upgrade to 6.7

I've been giving the task to upgrade an old PHP Web app that is written in php 5.2.17 and using Codeigniter. Doing my upgrade from PHP 5.2 to 5.6 I've been running into to some errors. At a first glance it looks like the problems might be a mix of using old PHP syntax as well as some issues with Codeigniter. I have never worked with Codeigniter which is making this task incredible hard.
The problems occurs on a page where you can edit some already existing data on a patient and then submit the new updated data. Pretty much a CRUD.
The crud has been working completely fine on a server supporting > 5.6 PHP, but now that I have transferred to a server that only supports < 5.6 PHP, I've been getting these problems.
First problem: On the form page I get these two errors and the data inside of those variables are not displayed on the page. If I decide to submit the new entered data the new data are not saved as well.
1) Trying to get property of non-object
2) Undefined variable: rows
The page errors lets me know that my error occurs at around these lines:
$anatomic_code = $rows->NerveCode;
$ProxAccess = $rows->ProxAccess;
$DistAccess = $rows->DistAccess;
From reading documentation from Codeigniter I was suggested to use [''] to access the data, if I do the error message "Trying to get property of non-object" goes away, but I still have the second error and still not seeing the data on the page.
Here is a bigger snippet of the code:
<div id="wrapper">
<?
$exam_id = $this->uri->segment(3);
$struct_id = $this->uri->segment(4);
$struct_type = $this->uri->segment(5);
$tech_id = $this->uri->segment(6);
$test_id = $this->uri->segment(7);
$TechName = $query_tech_type->row();
$side = $query_side->row();
//Update form
$hidden = array(
'Exam' => $exam_id,
'Struct_id' => $struct_id,
'Structure_type' => $struct_type,
'Tech_id' => $tech_id,
'Test' => $test_id
);
?>
<img src="<? echo base_url() . "public/holder/back.png"; ?>" border="0" title="Back to test page">
<?
echo form_open("auh/update_enkel_test_values/","", $hidden);
echo "<h2>" . $TechName->Tech_name . "</h2>";
echo "<div id=\"tech_no\" style=\"display:none\">".$tech_id."</div>";
if ($struct_id == "1"){
foreach($query->result() as $rows):
echo "<h3>M." . $rows->MuscleName . ") (" . $side->SideText.")</h3>";
endforeach;
$anatomic_code = $rows->MuscleCode;
}
elseif ($struct_id == "2") {
foreach($query->result() as $rows):
echo "<h3>N." . $rows->NerveName . ") (" . $side->SideText.")</h3>";
endforeach;
$anatomic_code = $rows->NerveCode;
}
elseif ($struct_id == "3") {
foreach($query->result() as $rows):
if ($rows->NerveCode > 10000 ){
echo "<h3>N." . $rows->NerveName . ") (" . $side->SideText.")</h3>";
}
else {
echo "<h3>N." . $rows->NerveName . " (" . $rows->ProxName . " - ";
if ($rows->Code < 3000) {
echo "m.";
}
echo $rows->DistName . ") (" . $side->SideText.")</h3>";
}
endforeach;
$anatomic_code = $rows->NerveCode;
$ProxAccess = $rows->ProxAccess;
$DistAccess = $rows->DistAccess;
}
elseif ($struct_id == "4"){
foreach($query->result() as $rows):
$anatomic_code = $rows->NerveCode;
if ($rows->NerveCode > 10000 ){
echo "<h3>" . $rows->NerveName . " (". $side->SideText .")</h3>";
}
else{
echo "<h3>N." . $rows->NerveName . " - m." . $rows->MuscleName . " (". $side->SideText .")</h3>";
}
endforeach;
}
Look through the backwards incompatible changes in the PHP documentation:
https://secure.php.net/manual/en/migration53.php
https://secure.php.net/manual/en/migration54.php
https://secure.php.net/manual/en/migration55.php
https://secure.php.net/manual/en/migration56.php
Go through each of those and do a find and replace through your codebase. Then you'll be on 5.6. But don't stop there! ;-)
https://secure.php.net/manual/en/migration70.php
https://secure.php.net/manual/en/migration71.php
https://secure.php.net/manual/en/migration72.php
You will find you have less and less things to change with each subsequent upgrade. 5.2 to 5.3 and to 5.4 probably have the most work involved.
You should try and get on version 7.2, the reason being that 5.6 is currently only developing security fixes, and is no longer going to be supported from Jan 1st 2019. Check this link here:
https://secure.php.net/supported-versions.php
Also, you can upgrade your Codeigniter installation. I don't know which version of codeigniter you are using, but you should download the latest version and follow their guide:
https://www.codeigniter.com/userguide3/installation/upgrading.html
Good luck!

How to call the instance of class?

I am using the setup, I download and setup package from github with link below:
https://github.com/graphaware/reco4php
I have also installed the php7 and neo7j they are running properly on localhost (ubuntu16.04).
I am getting error in browser:
Fatal error: Uncaught Error: Class 'GraphAware\Reco4PHP\Demo\Github\RecommendationEngine' not found in /var/www/html/recommendation_2/example.php:9 Stack trace: #0 {main} thrown in /var/www/html/recommendation_2/example.php on line 9
when hit example.php form browser.example.php file code below:
<?php
// example.php file
require_once __DIR__.'/vendor/autoload.php';
use GraphAware\Reco4PHP\Demo\Github\RecommendationEngine;
use GraphAware\Reco4PHP\RecommenderService;
$rs = RecommenderService::create("http://neo4j:idealindore#localhost:7474");
$rs->registerRecommendationEngine(new RecommendationEngine());
$stopwatch = new \Symfony\Component\Stopwatch\Stopwatch();
$input = $rs->findInputBy('User', 'login', 'jakzal');
$engine = $rs->getRecommender("github_who_to_follow");
$stopwatch->start('reco');
$recommendations = $engine->recommend($input);
$e = $stopwatch->stop('reco');
//echo $recommendations->size() . ' found in ' . $e->getDuration() . 'ms' .PHP_EOL;
foreach ($recommendations->getItems(10) as $reco) {
echo $reco->item()->get('login') . PHP_EOL;
echo $reco->totalScore() . PHP_EOL;
foreach ($reco->getScores() as $name => $score) {
echo "\t" . $name . ':' . $score->score() . PHP_EOL;
}
}
Can anybody help me how can I solve this issue?
You are getting this error because you did not use the right path to the classes.
Change This :
use GraphAware\Reco4PHP\Demo\Github\RecommendationEngine;
use GraphAware\Reco4PHP\RecommenderService;
to where you actually store These files.
RecommendationEngine.php
RecommenderService.php
Beside you also need to use these classes :
use GraphAware\Common\Cypher\Statement;
use GraphAware\Common\Type\Node;
use GraphAware\Reco4PHP\Context\Context;
use GraphAware\Reco4PHP\Engine\SingleDiscoveryEngine;
See This Link and follow the tutorial step by step to learn how to build a graph base recommendation system with Neo4J.
Also Check This Post if you have any problem with the tutorial.

ArrayObject::getIterator error

This is how i am trying to get some apartments name from the database, but i am always getting er error. This is how i am trying to get the apartments names
$userapt = $user->getApartments()->getIterator();
while ($userapt->valid()) {
echo ($userapt->key() . ' => ' . $userapt->current()) . "\n";
$userapt->next();
}
$useraptName = $user->getApartment()->getIterator();
while ($useraptName->valid()) {
($useraptName->key() . ' => ' . var_dump($useraptName->current()) . "\n");
$useraptName->next();
}
Running this, I get following error:
Catchable Fatal Error: Object of class MyBundle\Entity\UserApartment could not be converted to string
500 Internal Server Error - ContextErrorException
How can this error be fixed?
Thanks in advance!
You are trying to echo an object
echo ($userapt->key() . ' => ' . $userapt->current()) . "\n";
you can solve this by implementing the public function __toString() {} method in the UserApartament class. In this way when you will try to echo $user->current() it will use the __toString() method to show the string value of your object.

php scope: class object cant be seen in function

I have a simple mysqli wrapper class I use for my database operations. I am defining (instantiating?) that class at the top of the code presumably where it should be globally accessable, however when I try to use this reference to the db class within a recursive function, xdebug tells me it is out of scope - so as a fix I have had to define the database twice, but this seems like poor practice. Can anyone tell whats up or where I'm going wrong?
The code is recursively printing nested comments from the database FYI.
The code is as follows...
<?php
require 'lib/mysqli.class.php'; // the pretty standard mysqli class
$config = array();$config['host'] = 'localhost';$config['user'] = 'root';$config['pass'] = '';$config['table'] = 'publicate';
$db = new DB($config); // new instance of database
//$db->setFetchMode(2); // fetch data by association (MYSQLI_ASSOC)
// Run a Query:
$db->query('SELECT * FROM comments WHERE parentid = 0');
// Get the data:
$root_sql = $db->get();
recursive_categories($root_sql);
function recursive_categories($results)
{
if(count($results))
{
echo "<ul>";
foreach($results as $res)
{
echo "<li>" . "id=" . $res['id'] . ", pid=" . $res['parentid'] . ", content: " . $res['content'];
//Rest of what ever you want to do with each row
//Check this category for children ************************
//2nd definition of DB ************************
$config = array();$config['host'] = 'localhost';$config['user'] = 'root';$config['pass'] = '';$config['table'] = 'publicate';
$db2 = new DB($config); // new instance of database
$db2->query("SELECT * FROM comments WHERE parentid = " . $res['id']);
$rows = $db2->get();
recursive_categories($rows);
//has to be after the inner loops
echo "</li>";
}
echo "</ul>";
}
}
?>
Thanks.
You need to pass your $db connection to the function like so:
function recursive_categories($results, $db)
Then it will be available inside the function variable scope.
Another issue that comes to mind ... If this file resides inside a publicly accessible web directory you definitely don't want to have your actual database credentials just chilling there out in the open.

Trying to make an function call to an external file with PhP, MySQL using WAMP

I'm sure there is a simple answer to this, but I have been fumbling with everything for almost a week now and surrender. I am trying to build a shopping cart app and every coding solution I build will work when I include the code on the same page, but when I try to use an external page to run the function it does not seem to return the data. I have tried various monitoring techniques to determine what it is happening.
Here is the code for the main page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Cart Connection</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<p>Will this display?</p>
<p><a href='<?php echo "showCart.php?PHPSESSID=" . session_id() ?>'>View Cart</a></p>
<?php
$Database = "L3TtL2B5DdY";
$Table = "tblProds";
if (isset($_SESSION['curCart']))
$Cart = unserialize($_SESSION['curCart']);
else
{
if (class_exists("shoppingCart"))
{
$Cart = new shoppingCart();
$Cart->setDatabase($Database);
echo ("<p>If statement ran successfully</p>");
}
else
exit("<p>The shoppingCart class is not available!");
}
$Cart->setTable($Table);
$Cart->getProductList();
$_SESSION['curCart'] = serialize($Cart);
?>
<p><a href='<?php echo "showCart.php?PHPSESSID=" . session_id() ?>'>View Cart</a></p>
</body>
</html>
Here is the relevant code on the "shoppingCart.php" page:
<?php
class shoppingCart
{
private $dbConn = "";
private $dbName = "";
private $tableName = "";
private $orders = array();
private $orderTable = array();
function _construct()
{
$this->dbConn = #new mysqli("localhost", "root", "");
if (mysqli_connect_errno())
die("<p>Unable to connect to the database server.</p>" . "<p>Error Code " .
mysqli_connect_errno() . ": " . mysqli_connect_error() . "</p>");
}
public function setDatabase($Database)
{
$this->dbName = $Database;
#$this->dbConn->select_db($this->dbName)
Or die("<p>Unable to select the database.</p>" . "<p>Error code " . mysqli_errno($this->dbConn) .
": " . mysqli_error($this->dbConn) . "</p>");
}
public function setTable($Table)
{
$this->tableName = $Table;
}
public function getProductList()
{
$sqlString = "SELECT prodID, prodName, prodPrice FROM $this->tableName";
#$qryResult = $this->dbConn->query($sqlString)
Or die("<p>Unable to perform the query.</p>" . "<p>Error code " . mysqli_errno($this->dbConn) .
": " . mysqli_error($this->dbConn) . "</p>");
echo "<table width='100%' border='1'>";
echo "<tr><th>Product ID</th><th>Product Name</th><th>Product Price</th><th>Select Item</th></tr>";
$row = $qryResult->fetch_row();
do
{
echo "<tr><td>{$row[0]}</td>";
echo "<td>{$row[1]}</td>";
echo "<td>{$row[2]}</td>";
echo "<td><a href='showCart.php?PHPSESSID=" . session_id() . "&operation=addItem&productID=" . $row[0] .
"'>Add</a></td></tr>";
$row = $qryResult->fetch_row();
} while ($row);
echo "</table>";
}
......
?>
When I try to load the main page it will display the two lines and that is all. I debugged all the errors when I first created the code and thought it would work. When I wrote the original version of this page I put the "connection" code on the same page and the table displayed fine, so I don't know what else it could be.
I installed WAMP on my Windows XP box and it seems to work fine. I haven't touched the configuration files for any of the programs and all my other test code seems to work fine. It is just when I try to contact an external file.
Any help would be greatly appreciated as I think my brain is turning to mush.
Thanks
You probably need to include the ShoppingCart.php file in your main page, so it has the definition of the ShoppingCart class. Try putting at the top of your main page:
<?php require('ShoppingCart.php'); ?>
What I think might be happening is that the cart object is getting unserialised from the Session, but there is no class definition, so it becomes an instance of an incomplete class. When you then call a method on it you are getting a fatal error. What probably doesn't help is that you may not be displaying errors, so the script will just end. You could also try putting at the top of the main page:
<?php ini_set('display_errors', true); ?>
This should make PHP errors get shown.
Edit
It might be worth pointing out that you can't store a database connection in the session. You need to connect to the server / select the database etc. on every request. I don't think your script is currently doing that.
Also, you can store objects in the session without worrying about the serialisation yourself, here is a quick example:
<?php
//include class definition before starting session.
require('ShoppingCart.php');
session_start();
if (!isset($session['cart'])) {
$session['cart'] = new ShoppingCart();
}
$cart = $session['cart'];
//do stuff to cart
$cart->doSomething();
//changes are now saved back to the session when the script is terminated, without you having to do anything.
You need to
include_once("ShoppingCart.php");
Read up on the different ways to include files
http://www.w3schools.com/PHP/php_includes.asp

Categories