- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经被 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))."'><<</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);
我还有一个类来处理数据库,但它工作正常,我也可以发布它,但我没有从中得到任何错误,并且可以从数据库加载文件。
最佳答案
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/
我有三张 table 。表 A 有选项名称(即颜色、尺寸)。表 B 有选项值名称(即蓝色、红色、黑色等)。表C通过将选项名称id和选项名称值id放在一起来建立关系。 我的查询需要显示值和选项的名称,而
在mysql中,如何计算一行中的非空单元格?我只想计算某些列之间的单元格,比如第 3-10 列之间的单元格。不是所有的列...同样,仅在该行中。 最佳答案 如果你想这样做,只能在 sql 中使用名称而
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
我正在为版本7.6进行Elasticsearch查询 我的查询是这样的: { "query": { "bool": { "should": [ {
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
是否可以编写一个查询来检查任一子查询(而不是一个子查询)是否正确? SELECT * FROM employees e WHERE NOT EXISTS (
我找到了很多关于我的问题的答案,但问题没有解决 我有表格,有数据,例如: Data 1 Data 2 Data 3
以下查询返回错误: 查询: SELECT Id, FirstName, LastName, OwnerId, PersonEmail FROM Account WHERE lower(PersonEm
以下查询返回错误: 查询: SELECT Id, FirstName, LastName, OwnerId, PersonEmail FROM Account WHERE lower(PersonEm
我从 EditText 中获取了 String 值。以及提交查询的按钮。 String sql=editQuery.getText().toString();// SELECT * FROM empl
我有一个或多或少有效的查询(关于结果),但处理大约需要 45 秒。这对于在 GUI 中呈现数据来说肯定太长了。 所以我的需求是找到一个更快/更高效的查询(几毫秒左右会很好)我的数据表大约有 3000
这是我第一次使用 Stack Overflow,所以我希望我以正确的方式提出这个问题。 我有 2 个 SQL 查询,我正在尝试比较和识别缺失值,尽管我无法将 NULL 字段添加到第二个查询中以识别缺失
什么是动态 SQL 查询?何时需要使用动态 SQL 查询?我使用的是 SQL Server 2005。 最佳答案 这里有几篇文章: Introduction to Dynamic SQL Dynami
include "mysql.php"; $query= "SELECT ID,name,displayname,established,summary,searchlink,im
我有一个查询要“转换”为 mysql。这是查询: select top 5 * from (select id, firstName, lastName, sum(fileSize) as To
通过我的研究,我发现至少从 EF 4.1 开始,EF 查询上的 .ToString() 方法将返回要运行的 SQL。事实上,这对我来说非常有用,使用 Entity Framework 5 和 6。 但
我在构造查询来执行以下操作时遇到问题: 按activity_type_id过滤联系人,仅显示最近事件具有所需activity_type_id或为NULL(无事件)的联系人 表格结构如下: 一个联系人可
如何让我输入数据库的信息在输入数据 5 分钟后自行更新? 假设我有一张 table : +--+--+-----+ |id|ip|count| +--+--+-----+ |
我正在尝试搜索正好是 4 位数字的 ID,我知道我需要使用 LENGTH() 字符串函数,但找不到如何使用它的示例。我正在尝试以下(和其他变体)但它们不起作用。 SELECT max(car_id)
我有一个在 mysql 上运行良好的 sql 查询(查询 + 连接): select sum(pa.price) from user u , purchase pu , pack pa where (
我是一名优秀的程序员,十分优秀!