我已经在MySQL上遇到了PHP / PDO问题几天了,我只是无法弄清楚为什么它返回一个空数组...
我收到的错误消息如下所示:
注意:第24行的/home/saxon/students/20141/jolf14/www/oophp/kmom04/webroot/sqltest3.php中未定义的偏移量为0
注意:尝试在第24行的/home/saxon/students/20141/jolf14/www/oophp/kmom04/webroot/sqltest3.php中获取非对象的属性
第一堂课如下:
<?php
/**
* ctMovies is a class that handles movies and database
*
*/
class ctMovies{
/**
* Constructor
*
*/
public function __construct() {
}
/**
* Use the current querystring as base, modify it according to $options and return the modified query string.
*
* @param array $options to set/change.
* @param string $prepend this to the resulting query string
* @return string with an updated query string.
*/
function getQueryString($options = array(), $prepend = '?') {
// parse query string into array
$query = array();
parse_str($_SERVER['QUERY_STRING'], $query);
// Modify the existing query string with new options
$query = array_merge($query, $options);
// Return the modified querystring
return $prepend . htmlentities(http_build_query($query));
}
/**
* Create links for hits per page.
*
* @param array $hits a list of hits-options to display.
* @param array $current value.
* @return string as a link to this page.
*/
function getHitsPerPage($hits, $current = null) {
$nav = "Träffar per sida: ";
foreach ($hits AS $val) {
if ($current == $val) {
$nav .= "$val ";
} else {
$nav .= "<a href='" . $this->getQueryString(array('hits' => $val)) . "'>$val</a> ";
}
}
return $nav;
}
/**
* Create navigation among pages.
*
* @param integer $hits per page.
* @param integer $page current page.
* @param integer $max number of pages.
* @param integer $min is the first page number, usually 0 or 1.
* @return string as a link to this page.
*/
function getPageNavigation($hits, $page, $max, $min = 1) {
$nav = ($page != $min) ? "<a href='".$this->getQueryString(array('page' => $min))."'><<</a>" : '<<';
$nav .= ($page > $min) ? "<a href='".$this->getQueryString(array('page' => ($page > $min ? $page - 1 : $min)))."'><</a>" : '<';
for ($i = $min; $i <= $max; $i++) {
if ($page == $i) {
$nav .= "$i ";
} else {
$nav .= "<a href='".$this->getQueryString(array('page' => $i)) ."'>$i</a> ";
}
}
$nav .= ($page < $max) ? "<a href='".$this->getQueryString(array('page' => ($page < $max ? $page + 1 : $max)))."'>></a>" : '>';
$nav .= ($page != $max) ? "<a href='".$this->getQueryString(array('page' => $max))."'>>></a> " : '>> ';
return $nav;
}
/**
* Function to create links for sorting
*
* @param string $column the name of the database column to sort by
* @return string with links to order by column.
*/
function orderby($column) {
$nav = "<a href='" . $this->getQueryString(array('orderby' => $column, 'order' => 'asc')) . "'>↓</a>";
$nav .= "<a href='" . $this->getQueryString(array('orderby' => $column, 'order' => 'desc')) . "'>↑</a>";
return "<span class='orderby'>" .$nav . "</span>";
}
}
扩展第一堂课的第二堂课如下:
<?php
/**
* cMovies is a class that handles movies and database
*
*/
class cMovies extends ctMovies {
/**
* Properties
*
*/
private $db;
private $sqlOrig;
private $where;
private $groupby;
private $params;
/**
* Constructor
*
*/
public function __construct($db) {
parent::__construct();
$this->db=$db;
}
/**
* Function to get movie table
*
*/
function getTable(){
// Get parameters
$title = htmlentities(isset($_GET['title']) ? $_GET['title'] : null);
$title= str_replace('*', '%', $title);
$hits = isset($_GET['hits']) ? $_GET['hits'] : 8;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$year1 = isset($_GET['year1']) && !empty($_GET['year1']) ? $_GET['year1'] : null;
$year2 = isset($_GET['year2']) && !empty($_GET['year2']) ? $_GET['year2'] : null;
$orderby = isset($_GET['orderby']) ? strtolower($_GET['orderby']) : 'id';
$order = isset($_GET['order']) ? strtolower($_GET['order']) : 'asc';
// Check that incoming parameters are valid
is_numeric($hits) or die('Check: Hits must be numeric.');
is_numeric($page) or die('Check: Page must be numeric.');
is_numeric($year1) || !isset($year1) or die('Check: Year must be numeric or not set.');
is_numeric($year2) || !isset($year2) or die('Check: Year must be numeric or not set.');
// Prepare the query based on incoming arguments
$this->sqlOrig = '
SELECT
M.*,
GROUP_CONCAT(G.name) AS genre
FROM Movie AS M
LEFT OUTER JOIN Movie2Genre AS M2G
ON M.id = M2G.idMovie
INNER JOIN Genre AS G
ON M2G.idGenre = G.id
';
$this->where = null;
$this->groupby = ' GROUP BY M.id';
$limit = null;
$sort = " ORDER BY $orderby $order";
$this->params= array();
// Select by title
if ($title) {
$this->where .= ' AND title LIKE ?';
$this->params[] = $title;
}
// Select by year
if ($year1) {
$this->where .= ' AND year >= ?';
$this->params[] = $year1;
}
if ($year2) {
$this->where .= ' AND year <= ?';
$this->params[] = $year2;
}
// Pagination
if ($hits && $page) {
$limit = " LIMIT $hits OFFSET " . (($page - 1) * $hits);
}
// Complete the sql statement
$this->where = $this->where ? "WHERE 1 {$this->where}" : null;
$sql = $this->sqlOrig . $this->where . $this->groupby . $sort . $limit;
$res = $this->db->ExecuteSelectQueryAndFetchAll($sql, $this->params);
//// Put results into a HTML-table
$tr = "<tr><th>Rad</th><th>Id " . $this->orderby('id') . "</th><th>Bild</th><th>Titel " . $this->orderby('title') . "</th><th>År " . $this->orderby('year') . "</th><th>Genre</th></tr>";
foreach ($res AS $key => $val) {
$tr .= "<tr><td>{$key}</td><td>{$val->id}</td><td><img width='80' height='40' src='{$val->image}' alt='{$val->title}' /></td><td>{$val->title}</td><td>{$val->YEAR}</td><td>{$val->genre}</td></tr>";
}
return $tr;
}
/**
* Function to get a result which can be used to calculate maximal number
* of pages.
*/
function getResMaxPages() {
//// Get max pages for current query, for navigation
$sql = 'SELECT COUNT(id) AS rows FROM ($this->sqlOrig $this->where $this->groupby) AS Movie';
echo "$this->sqlOrig";
echo "$this->where";
echo "$this->groupby";
$res = $this->db->ExecuteSelectQueryAndFetchAll($sql, $this->params);
return $res;
}
}
这一切都是由这个小家伙经营的:
<?php
/**
* This is a joax pagecontroller.
* It handles movie information from a database
*/
// Include the essential config-file which also creates the $joax variable with its defaults.
include(__DIR__ . '/config.php');
$joax['stylesheets'][] = 'css/table.css';
// Connect to a MySQL database using PHP PDO
$db = new cDatabase($joax['database']);
$movies = new cMovies($db);
$tr=$movies->getTable();
$hits = isset($_GET['hits']) ? $_GET['hits'] : 8;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$title = htmlentities(isset($_GET['title']) ? $_GET['title'] : null);
$year1 = isset($_GET['year1']) && !empty($_GET['year1']) ? $_GET['year1'] : null;
$year2 = isset($_GET['year2']) && !empty($_GET['year2']) ? $_GET['year2'] : null;
$res=$movies->getResMaxPages();
$rows = $res[0]->rows;
$max = ceil($rows / $hits);
// Do it and store it all in variables in the joax container.
$joax['title'] = "Visa filmer med olika sökalternativ";
$hitsPerPage = $movies->getHitsPerPage(array(2, 4, 8), $hits);
$navigatePage = $movies->getPageNavigation($hits, $page, $max);
//$sqlDebug = $db->Dump();
$joax['main'] = <<<EOD
<h1>{$joax['title']}</h1>
<form>
<fieldset>
<legend>Sök</legend>
<input type=hidden name=hits value='{$hits}'/>
<input type=hidden name=page value='1'/>
<p><label>Titel (delsträng, använd * eller % ): <input type='search' name='title' value='{$title}'/></label></p>
<p><label>Skapad mellan åren:
<input type='text' name='year1' value='{$year1}'/></label>
-
<input type='text' name='year2' value='{$year2}'/>
</p>
<p><input type='submit' name='submit' value='Sök'/></p>
<p><a href='?'>Visa alla</a></p>
</fieldset>
</form>
<div>
<div>{$rows} träffar. {$hitsPerPage}</div>
<table>
{$tr}
</table>
<div class='center'>{$navigatePage}</div>
</div>
EOD;
// Finally, leave it all to the rendering phase of joax.
include(JOAX_THEME_PATH);
我还有一个类来处理数据库,但它工作正常,我也可以发布它,但我没有从中获取任何错误,并且可以从数据库加载文件。
答案 0 :(得分:0)
未定义的偏移量:0表示您正在引用不存在的数组键。试图获取非对象的属性支持这一点,并且很可能你在sqltest3.php的第24行引用它之前未能正确地实例化你的对象,如果你不发帖我就无法帮助你。该文件的代码。在引用对象之前,您可能需要执行类似的操作:
$some_variable = new whatever_class_is_on_line_24_of_sqltest3_dot_php();
答案 1 :(得分:0)
假设您的db::ExecuteSelectQueryAndFetchAll()
函数返回包含查询结果的对象,请尝试请求$res->rows
而不是$res[0]->rows
。
答案 2 :(得分:0)
就个人而言,我在第23行和第23行之间添加以下内容: 24:
die( var_dump( $res ) );
然后至少你能够看到你正在处理的事情......