Issues (146)

examples/database_import.php (6 issues)

Labels
1
<?php
2
/**
3
 * Use QueryPath's database extension to import XML data into a database.
4
 *
5
 * 
6
 * @author M Butcher <[email protected]>
7
 * @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license.
8
 */
9
10
require_once '../src/QueryPath/QueryPath.php';
11
require_once '../src/QueryPath/Extension/QPDB.php';
12
13
// Set the default database.
14
QPDB::baseDB('sqlite:../test/db/qpTest.db');
0 ignored issues
show
The type QPDB was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
// To begin, let's create a new database. We can do this outside
17
// of QueryPath:
18
$db = QPDB::getBaseDB();
19
$db->exec('CREATE TABLE IF NOT EXISTS qpdb_article (title, author, body)');
20
21
// Here's our sample article:
22
$article = '<?xml version="1.0"?>
23
<article>
24
  <title>Use QueryPath for Fun and Profit</title>
25
  <author>
26
    <first>Matt</first>
27
    <last>Butcher</last>
28
  </author>
29
  <body>
30
  <![CDATA[
31
  <p>QueryPath is a great tool.</p>
32
  <p>Use it in many ways.</p>
33
  ]]>
34
  </body>
35
</article>';
36
37
// Now let's take this article and insert it into the database:
38
$qp = qp($article);
39
40
// We are going to store our insert params in here.
41
$params = array();
42
43
// First, let's get the title
44
$params[':title'] = $qp->find('title')->text();
45
46
// Next, let's get the name:
47
$params[':name'] = $qp->top()->find('author>last')->text() . ', ' . $qp->prev('first')->text();
0 ignored issues
show
Are you sure $qp->prev('first')->text() of type QueryPath\DOMQuery|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
$params[':name'] = $qp->top()->find('author>last')->text() . ', ' . /** @scrutinizer ignore-type */ $qp->prev('first')->text();
Loading history...
Are you sure $qp->top()->find('author>last')->text() of type QueryPath\DOMQuery|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
$params[':name'] = /** @scrutinizer ignore-type */ $qp->top()->find('author>last')->text() . ', ' . $qp->prev('first')->text();
Loading history...
48
49
// Finally, let's get the article content:
50
$params[':body'] = $qp->top()->find('body')->text();
51
52
// Here's the query we are going to run:
53
$sql = 'INSERT INTO qpdb_article (title, author, body) VALUES (:title, :name, :body)';
54
55
// Now we can insert this:
56
$qp->query($sql, $params);
0 ignored issues
show
The method query() does not exist on QueryPath\DOMQuery. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
$qp->/** @scrutinizer ignore-call */ 
57
     query($sql, $params);
Loading history...
57
58
// Finally, we can now read this information back out into an HTML document
59
qp(QueryPath::HTML_STUB, 'body')->queryInto('SELECT * FROM qpdb_article')->writeHTML();
0 ignored issues
show
The method queryInto() does not exist on QueryPath\DOMQuery. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
qp(QueryPath::HTML_STUB, 'body')->/** @scrutinizer ignore-call */ queryInto('SELECT * FROM qpdb_article')->writeHTML();
Loading history...
60
61
// Finally, we clean up:
62
$qp->exec('DROP TABLE qpdb_article');
0 ignored issues
show
The method exec() does not exist on QueryPath\DOMQuery. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
$qp->/** @scrutinizer ignore-call */ 
63
     exec('DROP TABLE qpdb_article');
Loading history...