gpt4 book ai didi

PHP、MySQL查询返回空数组,为什么?

转载 作者:行者123 更新时间:2023-11-30 00:32:05 26 4
gpt4 key购买 nike

我已经被 MySQL 的 PHP/PDO 问题困扰了几天,我只是不明白为什么它返回一个空数组......

我收到的错误消息如下所示:

Notice: Undefined offset: 0 in /home/saxon/students/20141/jolf14/www/oophp/kmom04/webroot/sqltest3.php on line 24

Notice: Trying to get property of non-object in /home/saxon/students/20141/jolf14/www/oophp/kmom04/webroot/sqltest3.php on line 24

第一类看起来像这样:

<?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))."'>&lt;&lt;</a>" : '&lt;&lt;';
$nav .= ($page > $min) ? "<a href='".$this->getQueryString(array('page' => ($page > $min ? $page - 1 : $min)))."'>&lt;</a>" : '&lt;';

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)))."'>&gt;</a>" : '&gt;';
$nav .= ($page != $max) ? "<a href='".$this->getQueryString(array('page' => $max))."'>&gt;&gt;</a> " : '&gt;&gt; ';
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')) . "'>&darr;</a>";
$nav .= "<a href='" . $this->getQueryString(array('orderby' => $column, 'order' => 'desc')) . "'>&uarr;</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);

我还有一个类来处理数据库,但它工作正常,我也可以发布它,但我没有从中得到任何错误,并且可以从数据库加载文件。

最佳答案

undefined offset :0 表示您引用的数组键不存在。尝试获取非对象的属性支持这一点,并且在 sqltest3.php 的第 24 行引用它之前,您可能无法正确实例化对象,如果您不发布代码,我无法真正帮助您对于该文件。在引用对象之前,您可能需要执行类似的操作:

$some_variable = new whatever_class_is_on_line_24_of_sqltest3_dot_php();

关于PHP、MySQL查询返回空数组,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22430848/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com