我正在尝试将文章ID添加到标题,摘要和内容中,但我不知道怎么做可以帮助我解决这个问题。
这是给我问题的代码。
while($row = mysqli_fetch_assoc($run)) {
$id[] = $row['id'];
$title[] = $row['id']['title'];
$summary[] = $row['id']['summary'];
$content[] = $row['id']['article_content'];
}
以下是我的PHP和MySQL代码。
$x = 0;
$con = null;
$search = $_REQUEST['search'];
$id = array();
$title = array();
$summary = array();
$content = array();
$search_explode = mysqli_real_escape_string($dbc, $search);
$search_explode = explode(' ', $search_explode);
foreach($search_explode as $search_each) {
$x++;
if($x == 1){
$con .= " article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
} else {
$con .= " OR article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
}
}
$con = "SELECT users.*, users_articles.* FROM users_articles
INNER JOIN users ON users_articles.user_id = users.user_id
WHERE ($con)
AND users.active IS NULL
AND users.deletion = 0";
$run = mysqli_query($dbc, $con);
$search_term = mysqli_num_rows($run);
while($row = mysqli_fetch_assoc($run)) {
$id[] = $row['id'];
$title[] = $row['id']['title'];
$summary[] = $row['id']['summary'];
$content[] = $row['id']['article_content'];
}
答案 0 :(得分:1)
while($row = mysqli_fetch_assoc($run)) {
// $id[] = $row['id']; you probably do not need this anymore
$title[$row['id']] = $row['title'];
$summary[$row['id']] = $row['summary'];
$content[$row['id']] = $row['article_content'];
}
// at this point, each array will contain rows
// with keys matching the corresponding id
var_dump($title);
var_dump($summary);
var_dump($content);
答案 1 :(得分:0)
$title[] = $row['id']['title'];
$summary[] = $row['id']['summary'];
$content[] = $row['id']['article_content'];
mysqli_fetch_assoc
将获取关联的数组。像$row['summary']
那样直接访问数据应该可以解决您的问题。
修改强>
你在谈论字符串连接吗?
$title[] = $row['id'] . ' ' . $row['title'];
$summary[] = $row['id'] . ' ' . $row['summary'];
$content[] = $row['id'] . ' ' . $row['article_content'];
上面的代码基本上会将标题/摘要/内容附加到您的文章ID并将其添加到相应的数组中。
答案 2 :(得分:0)
我不确定你想要实现什么,但我会建议以下解决方案。请看一下。
CREATE TABLE IF NOT EXISTSusers
(user_id
int(11) NOT NULL AUTO_INCREMENT,name
varchar(100) NOT NULL,deletion
int(11) NOT NULL DEFAULT '0',active
int(11) NOT NULL DEFAULT '1', PRIMARY KEY (user_id
) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;--
-- Dumping data for table
users
INSERT INTO
users
(user_id
,name
,deletion
,active
) VALUES (1, 'John', 0, 1), (2, 'George', 0, 1);
--
-- Table structure for table
users_articles
CREATE TABLE IF NOT EXISTS
users_articles
(id
int(11) NOT NULL AUTO_INCREMENT,user_id
int(11) NOT NULL,article_content
varchar(255) NOT NULL,title
varchar(255) NOT NULL,summary
text NOT NULL, PRIMARY KEY (id
) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;--
-- Dumping data for table
users_articles
INSERT INTO
users_articles
(id
,user_id
,article_content
,title
,summary
) VALUES (1, 1, 'test', 'test', 'test test test test'), (2, 2, 'test 2', 'test 2', 'test test'), (3, 1, 'test 3', 'test 3 ', 'test test test test 3'), (4, 2, 'test 3', 'test 3', 'test test 3');<?php error_reporting(E_ALL); ini_set('display_errors', 1); $mysqli = new mysqli("localhost", "root", "", "articles"); /* check connection */ if ($mysqli->connect_errno) { printf("Connect failed: %s\n", $mysqli->connect_error); exit(); } /* Select queries return a resultset */ $con = 1; $sql = "SELECT users.*, users_articles.* FROM users_articles INNER JOIN users ON users_articles.user_id = users.user_id WHERE ($con) AND users.active = 1 AND users.deletion = 0"; $results = array(); if ($result = $mysqli->query($sql)) { printf("Select returned %d rows.\n", $result->num_rows); print '<pre>'; while ($row = $result->fetch_assoc()) { $results[] = $row; } print_r($results); print '</pre>'; /* free result set */ $result->close(); } $mysqli->close(); ?>
当你迭代$ results时你可以连接id和其他字段,在这种情况下你也可以在其他区域使用结果数组。