I have website settings stored in a table like this:
+---------+-------------+
| id | value |
+---------+-------------+
| 1 | title |
| 2 | description |
| 3 | email |
| 4 | keywords |
+---------+-------------+
How can I select them and assign them to variables ($title, $desc, $email, $key)
// Get Website Infos
$result=$db->query("SELECT `value` FROM `settings`");
if($result->num_rows >= 1){
$row=$result->fetch_assoc();
$otitle=$row['value'];
$odescription=$row['value'];
$oemail=$row['value'];
$okeywords=$row['value'];
}
but it only get the first row 'title',any idea how to do it or better way to save settings.
UPDATE:
If you are reading this question now, don't use this structure to store your settings, use the one suggested by Yellow Bird in the answers.
You have to loop with your fetch_assoc() method :
$result = $db->query("SELECT `value` FROM `settings`");
if ($result->num_rows >= 1){
$data = array();
while ($row = $result->fetch_assoc()) {
$data[] = $row['value'];
}
}
var_dump($data);
As DanFromGermany noticed in comments, your db structure is probably not done the right way. You should have a design like this :
+---------+-------------+--------------+
| id | key | value |
+---------+-------------+--------------+
| 1 | title | title value |
| 2 | description | desc value |
| 3 | email | email value |
| 4 | keywords | keyw value |
+---------+-------------+--------------|
And then, your code would look like this :
$result = $db->query("SELECT key, value FROM `settings`");
if ($result->num_rows >= 1) {
$data = array();
while ($row = $result->fetch_assoc()) {
$data[$row['key']] = $row['value'];
}
}
Related
Im trying to get data from one table based upon the date in a column from another table. I've tried using an inner join but I can't quite figure out how to make it do what I want. Below is an example. Basicly the numbers in the rules column will referance the rule_id in the infraction rules table. Then output the rule long name in readable text.
user_infractions table
+--------+-----------------+------------------+----------------+
|ban_id | name | username | rules |
+--------+-----------------+------------------+----------------+
| 2 | bob | Bob | 2,7, |
| 7 | dave | dave1 | 3,5, |
+--------+-----------------+------------------+----------------+
infraction_rules table
+--------+-----------------+------------------+----------------+
|rule_id | rule number | short_name | long_name |
+--------+-----------------+------------------+----------------+
| 2 | 1.2.2 | Rule 2 | Rule 2 Long Na |
| 7 | 1.2.7 | Rule 7 | Rule 7 Long Na |
+--------+-----------------+------------------+----------------+
Below is the code im using to try to achieve this
$string = $row['rules']; //data from table as a string
$strtrim = rtrim($string,","); //removed trailing comma from string
$violations = explode(",", $strtrim); // string to array conversion
foreach($violations as $violation) {
echo $violation;
$sql3 = "SELECT *
FROM user_infractions
INNER JOIN infraction_rules ON user_infractions.rules = infraction_rules.rule_id
WHERE user_infractions.rules = ?;";
$stmt3 = $conn->prepare($sql3);
//$stmt -> mysqli_stmt_prepare($stmt, $sql);
$stmt3->bind_param("s", $violation);
$stmt3->execute();
$result3 = $stmt3->get_result();
if ($result3->num_rows > 0) {
while ($row3 = $result3->fetch_assoc()) {
echo $row3['rule_long_desc'];
}
}
}
I have a tags_ids array 1,3,2 and
data in my table:
+---------+----------+
| user_id | tag_id |
+---------+----------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
+---------+----------+
I want to get the users ids into array, but not working:
foreach ($tags_ids as $i)
{
if ($result = $mysqli->prepare("SELECT `user_id` FROM `mytable` WHERE `tag_id`=?"))
{
$result->bind_param("i",$i);
$result->execute();
$result->bind_result($d);
$result->fetch();
$result->close();
}
if (!in_array($d,$users_ids)) $users_ids[] = $d;
}
My result is always 1. Whats I'm doing wrong, and can I do it in a more simple way?
you need a while loop, you can find it here for full assist:http://www.youtube.com/watch?v=hO0YOOeJrOE
be sure to watch the other video's too, very helpfull.
goodluck,
PHPNoob
I confused how to filter words of some documents. I have to checked out the documents one by one. for example from tb_tokens :
======================================================================
| tokens_id | tokens_word | tokens_freq| sentence_id | document_id |
======================================================================
| 1 | A | 1 | 0 | 1 |
| 2 | B | 1 | 0 | 1 |
| 3 | C | 1 | 1 | 1 |
| 4 | D | 1 | 0 | 2 |
| ... | | | | |
======================================================================
I have to remove all words that appear on a list od common words like “and”, “the”, etc.. The list recorded in table tb_stopword and then remove words that occurr in large number across most documents that appear on a list recorded in tb_term table.
the function cekStopWord :
function cekStopWord ($word) {
$query = mysql_query("SELECT stoplist_word FROM tb_stopword where stoplist_word = '$word' ");
$row = mysql_fetch_row($query);
if($row > 0) {
return true;
} else {
return false;
}
}
And the similar function for the second process (remove words that occurr in large number across most documents)
function cekTerm ($word) {
$query = mysql_query("SELECT term_word FROM tb_term where term_word = '$word' ");
I confused how to process in every documents. I tried to call by doc_id, but it doesnt work. and here's my code :
//$doc_id is a variable that save array of document_id
$query = mysql_query('SELECT tokens_word, sentence_id, document_id FROM tb_tokens WHERE document_id IN (' . implode(",", $doc_id) . ')') or die(mysql_error());
while ($row = mysql_fetch_array($query)) {
$word[$row['document_id']][$row['sentence_id']] = $row['tokens_word'];
}
foreach ($word as $doc_id => $words){
$cekStopWord = cekStopWord($words);
$cekTerm = cekTerm($words);
if((preg_match("/^[A-Z, 0-9]/", $words))&& (!$cekStopWord) && (!$cekTerm) ){
$q = mysql_query("INSERT INTO tb_tagging VALUES ('','$words','','$sentence_id','$doc_id') ");
and also how to use preg_match in array ?
thank you so much :)
I have this scenario.
I input $groupid="1";
main table
----------------------
| groupid | postid |
|---------------------|
| 1 | 1 |
| 2 | 2 |
| 1 | 3 |
$query = "SELECT postid FROM `mainl` WHERE groupid='$groupid'";
$result = mysql_query($query);
// a group of postids belonging to that groupid which should hold [1, 3] for groupid=1
while($row = mysql_fetch_array($result)) {
$postids[] = $row["postid"];
}
second table
-------------------------------------------
| postid | commentid | comment |
-------------------------------------------
| 1 | 1 | testing 1 |
| 1 | 2 | testing 2 |
| 1 | 3 | what? |
| 2 | 1 | hello |
| 2 | 2 | hello world |
| 3 | 1 | test 3 |
| 3 | 2 | begin |
| 3 | 3 | why? |
| 3 | 4 | shows |
$query = "SELECT * FROM `second`";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
if (in_array($row["postid"], $postids)) {
$comments[$row["postid"]] = $row["comment"];
But how should I take care of commented
I want the postid array to be [1,3] and my comment array to be
[commentid: comment] [1:testing1, 2: testing2, 3: what?] for postid=1
and
[1:test3, 2:begin, 3: why? 4:shows] for postid=3
how should be arrange everything such comment are associated with commentid and postid?
First I would follow rokdd suggestion and make 1 query
SELECT m.groupid , s.postid, s.commentid, s.comment FROM `main1` m JOIN `second` s USING (postid) where m.groupid = 1
Then I would make a multi-dimensional array
while ($row = mysql_fetch_array($result))
$groups[$row['groupid'][$row['postid']][$row['commentid']=$row['comment'];
then to iterate through the array
foreach($groups as $group)
foreach($group as $post)
foreach($post as $comment)
echo $comment;
This will keep track of groups also (if you ever want to select by more than 1 group.
If you don't care about groups just drop off the first part of the array.
while ($row = mysql_fetch_array($result))
$posts[$row['postid']][$row['commentid']=$row['comment'];
foreach($posts as $post)
foreach($post as $comment)
echo $comment;
I guess to use the join in sql so that you will have one statement:
SELECT * FROM second as second_tab LEFT join main as main_table ON main_table.post_id=second_table.post_id WHERE main_table.group_id="3"
Well not tested now but thats a way to solve some of your problems!
I have a table that like this.
+-----------+-----------+-----------+-----------+-----------+-----------+
|id | parent_id | name | order | status | date_add |
+-----------+-----------+-----------+-----------+-----------+-----------+
|1 | 0 | shoes | 1 | 1 | 2011-04-02|
+-----------+-----------+-----------+-----------+-----------+-----------+
|2 | 1 | male | 2 | 1 | 2011-04-02|
+-----------+-----------+-----------+-----------+-----------+-----------+
|3 | 1 | female | 3 | 1 | 2011-04-02|
+-----------+-----------+-----------+-----------+-----------+-----------+
|4 | 3 | red shoes | 4 | 1 | 2011-04-02|
+-----------+-----------+-----------+-----------+-----------+-----------+
I want to select only the leaf children, with their path.
I want to come to the result as follows:
+------+-------------------------------------+
| 2 | shoes/male |
+------+-------------------------------------+
| 4 | shoes/female/red shoes |
+------+-------------------------------------+
if this does not only sql can also be php + sql
Please help me.
Very simple solution to print out id and path to all last child nodes using PHP as I am not aware of a way of doing this within MySQL. Hope this helps!
function getChildren($parent= "", $x = 0) {
$sql = "SELECT id, name FROM recurr WHERE parentId = $x";
$rs = mysql_query($sql);
//echo "Name: $parent has ". mysql_num_rows($rs)." children<br/>";
while ($obj = mysql_fetch_object($rs)) {
if (hasChildren($obj->id)) {
getChildren($parent."/".$obj->name, $obj->id);
} else {
echo $obj->id .", ".$parent."/".$obj->name."<br/>";
}
}
}
function hasChildren($x) {
$sql = "SELECT * FROM recurr WHERE parentId = $x";
$rs = mysql_query($sql);
if (mysql_num_rows($rs) > 0) {
return true;
} else {
return false;
}
}
To run just call:
getChildren();
I highly recommend implementing nested set model for hierarchical MySQL data. Here is the link for more info: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/. In the link, you will also find the answer to your question (under "The Adjacency List Model", "Finding all the Leaf Nodes" section)