好的我对php中的数组感到有些困惑。这是代码
$search = "%" . $search ."%";
// Step 1: Establish a connection
$db = new PDO("mysql:host=localhost;dbname=########", "#########", "###########");
// Step 2: Construct a query
$query = "SELECT * FROM movies WHERE title LIKE " . $db->quote($search);
// Step 3: Send the query
$result = $db->query($query);
// Step 4: Iterate over the results
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
var_dump($row);
echo $row['title'];
}
// Step 5: Free used resources
$result->closeCursor();
$db = null;
现在,当我搜索说“Candyman”时,它返回6个结果,我要做的就是$row['title'][3]
(所以我可以抓住阵列中第3部电影的标题)
但这不起作用$row['title'];
显示所有6个标题如何访问下一个级别?
以下是我正在使用的搜索的链接 http://www.gorrors.com/moviesearch.php?search=candyman
很抱歉新手问题,但我不知道任何帮助将不胜感激。
答案 0 :(得分:0)
更改查询,例如$query = "SELECT * FROM movies WHERE title LIKE " . $db->quote($search);
使用限制,$query = "SELECT * FROM movies WHERE title LIKE " . $db->quote($search) limit $start,$numbers;
http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
答案 1 :(得分:0)
尝试:
$rows = $result->fetchAll();
echo $row[3]['title']'
PDO ::查询:
http://www.php.net/manual/en/pdo.query.php
返回PDOStatement对象:
http://www.php.net/manual/en/class.pdostatement.php
...它有一个名为fetchAll()的方法,它返回一个包含所有行的数组,默认情况下你可以使用数字索引或字符串进行访问。
答案 2 :(得分:0)
您可以尝试 Grouping all values by a single column :
//do not use * in your sql if you only need the imfromation of movie title
$query = "SELECT title FROM movies WHERE title LIKE " . $db->quote($search);
$result = $dbh->prepare($query);
$result->execute();
$row = $result->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP);
var_dump($row);
//$row['title'][2] will give the third result,not $row['title'][3]