How can I get a single column value from mysqli? The result should be single row with only one column.
This is what I have tried:
$query = "SELECT MAX(`userid`) FROM `user`";
$rlt = mysqli_query($this->db, $query);
echo $rlt['userid'];
You are not fetching the row after executing the query:
$query = "SELECT MAX(`userid`) FROM `user'";
$rlt = mysqli_query($this->db,$query);
$row = mysqli_fetch_row($this->db, $rlt);
echo $row[0];
The alternative would be to use an alias for the computed field and use fetch_assoc:
$query = "SELECT MAX(`userid`) as `maxid` FROM `user'";
$rlt = mysqli_query($this->db,$query);
$row = mysqli_fetch_assoc($this->db, $rlt);
echo $row['maxid'];
try with create alias and fetch result after query execution also your quote of user' looking wrong
$query = "SELECT MAX(`userid`) as userid FROM `user`";
$rlt = mysqli_query($this->db,$query);
$r = mysqli_fetch_assoc($rlt);
echo $r['userid'];
or fetch only one row like:-
$r = mysqli_fetch_row($this->db, $rlt);
echo $r[0];
You should create alias here.
Use this one:
$query = "SELECT MAX(`userid`) as Max_Userid FROM `user'";
$rlt = mysqli_query($this->db,$query);
$row = mysqli_fetch_assoc($this->db, $rlt);
echo $row['Max_Userid'];
Note: Always use alias when you use mysql function in query with fields.
Related
I'm having trouble with a mysqli_query from inside a foreach loop, I'm getting a string from a table first, then separating that into an array. Then I try looping through the array and calling a query inside the loop.
$langs_result = mysqli_query($con, "SELECT Languages FROM users WHERE Username = '$username'");
$row = mysqli_fetch_assoc($langs_result);
$langs = $row['Languages'];
$userLangs = str_replace(" ","",$langs);
$userLangs = explode(",",$langs);
print_r($userLangs);
$posts = array();
foreach($userLangs as $lang){
echo "$lang <br>";
$sql = "SELECT * FROM posts WHERE Language = '$lang'";
$getLangPosts = mysqli_query($con, $sql);
array_push($posts, mysqli_fetch_assoc($getLangPosts));
}
print_r($posts);
for this user the langusges are German, Italian, Danish, and English, but the $posts array only contains the first post found from the first language (German), can anybody help? I am trying to get all of the posts for each language in the $userLangs array.
It's going through the foreach loop okay as the $lang variable that's echoed changes each time but the query still isn't working properly.
Thanks for the help!
select posts.* from posts
left join users on users.language=posts.language
where users.username='your_desiredusername'
group by users.language;
Just try to run this as a single query by filling the username
no need of multiple queries
You an avoid multiple queries by doing a JOIN, using FIND_IN_SET to match on your comma separated list. You probably need to use REPLACE to get rid of the extra spaces in the comma separated list as well.
Then you can just loop around to display the data, displaying the language on change of language:-
<?php
$sql = "SELECT a.Languages AS user_languages,
b.*
FROM users a
LEFT OUTER JOIN posts b
ON FIND_IN_SET(b.Language, REPLACE(a.Languages, ' ', ''))
WHERE a.Username = '$username'
ORDER BY b.Languages";
$langs_result = mysqli_query($con, $sql);
if($row = mysqli_fetch_assoc($langs_result))
{
print_r(explode(', ', $row['user_languages']));
$prev_langauge = '';
while($row = mysqli_fetch_assoc($langs_result))
{
if ($prev_langauge != $row['Languages'])
{
if ($prev_langauge != '')
{
print_r($posts);
}
$posts = array();
echo $row['Languages']."<br>";
$prev_langauge = $row['Languages'];
}
array_push($posts, mysqli_fetch_assoc($row));
}
if ($prev_langauge != '')
{
print_r($posts);
}
}
UPDATE
See this code:
<?php
$langs_result = mysqli_query($con, "SELECT Languages FROM users WHERE Username = '$username'");
$row = mysqli_fetch_assoc($langs_result);
$langs = $row['Languages'];
// $langs = 'German, Italian, Danish, French'; added this to run the test
// $userLangs = str_replace(" ","",$langs); this is not needed, see the explode below
$userLangs = explode(", ",$langs);
foreach($userLangs as $lang){
echo $lang;
$sql = "SELECT * FROM posts WHERE Language = '$lang'";
$getLangPosts = mysqli_query($con, $sql); // this is the result of the select *
while($post = mysqli_fetch_assoc($getLangPosts)){ // iterate over all the results
$postField = $post["yourChoiceField..say..Title"]; // get something from each row
array_push($posts, $title); // push into array
}
}
print_r($posts);
Since the initial select is based on username I don't believe the first loop is needed so your code was on the right track.
A second loop was needed to iterate over the posts though and a field to populate the $posts array.
You need to perform mysqli_fetch_assoc in a loop
$langs_result = mysqli_query($con, "SELECT Languages FROM users WHERE Username = '$username'");
while($row = mysqli_fetch_assoc($langs_result)){
$langs = $row['Languages'];
$userLangs = str_replace(" ","",$langs); // i don't get why you are doing this though
$userLangs = explode(",",$langs);
print_r($userLangs);
$posts = array();
foreach($userLangs as $lang){
echo "$lang <br>";
$sql = "SELECT * FROM posts WHERE Language = '$lang'";
$getLangPosts = mysqli_query($con, $sql);
array_push($posts, mysqli_fetch_assoc($getLangPosts));
}
print_r($posts);
}
It would help to know how what the select query actually returns.
mysqli_fetch_assoc only fetches one row on each call you need to use it like this:
while ($row_new = mysqli_fetch_assoc($getLangPosts)){
array_push($posts, $row_new);
}
You need to loop the inner query to get the column data
foreach($userLangs as $lang){
echo "$lang <br>";
$sql = "SELECT * FROM posts WHERE Language = '$lang'";
$getLangPosts = mysqli_query($con, $sql);
while($row1 = mysqli_fetch_assoc($getLangPosts))
array_push($posts, $row1['YOUR_COLUMN_NAME']);
}
OR you should use IN clause instead of loop
$langs_result = mysqli_query($con, "SELECT Languages FROM users WHERE Username = '$username'");
while($row = mysqli_fetch_assoc($langs_result)){
$langs = $row['Languages'];
$userLangs = str_replace(" ","",$langs);
$userLangs = explode(",",$langs);
print_r($userLangs);
$posts = array();
$sql = "SELECT * FROM posts WHERE Language IN ('".implode(',',$userLangs)."')";
$getLangPosts = mysqli_query($con, $sql);
while($row1 = mysqli_fetch_assoc($getLangPosts))
array_push($posts, $row1['YOUR_COLUMN_NAME']);
}
I need to do a SELECT * FROM table_X , the problem is table_X is the result of another query, I don't know how to do it, perhaps with two loop, something like this :
<?php
$query1 = mysql_query("SELECT * FROM table_ref");
while ($row = mysql_fetch_array($query1))
{
$name = $row['table_name'];
$query2 = mysql_query(" SELECT * FROM '$name' ");
while ($row = mysql_fetch_array($query2))
{
$time = $data['itime'];
echo $time;
}
}
?>
The tables are all similar & I can't do joint there's no keys. So what I want is to show only the results of the second query from each results of the first query !
So, what's your structure? I don't understand. You have column table_name where are listed a lot of tables? If so, just use backquotes on your $name:
$query2 = mysql_query(" SELECT * FROM `$name` ");
Apart from the obvious that has been pointed out in the comments, you're overwriting $row in the second loop.
Also, you're trying to read an array ($data) that is not defined.
The following will work much better (but still isn't ideal):
$query1 = mysql_query("SELECT `table_name` FROM `table_ref`");
while ($row = mysql_fetch_array($query1))
{
$name = $row['table_name'];
$query2 = mysql_query("SELECT `itime` FROM `$name`");
while ($data = mysql_fetch_array($query2))
{
$time = $data['itime'];
echo $time;
}
}
just change your quotes to have the query ready to be started
change
$query2 = mysql_query(" SELECT * FROM '$name' ");
to
$query2 = mysql_query(" SELECT * FROM `".$name."` ");
i would also rather sugest to check this part
while ($row = mysql_fetch_array($query2))
{
$time = $data['itime'];
echo $time;
}
you already used variable $row to fetching previus query so better to change to something else, look like $data is matching your needs because you already used but you did not declare it
while ($data = mysql_fetch_array($query2))
{
$time = $data['itime'];
echo $time;
}
Try this query:
select x.* from ( SELECT table_name FROM table_ref) as x
I got a table named "Serials" with 5 comumns
Serial, Code, Name, Redeemed, Redeem_date
i am selecting some fields from that table with this query:
$query = "SELECT `Name`,`Redeemed`,`Redeem_date` FROM `Serials` WHERE `Serial` = '$serial' AND `Code` = '$code'";
$db->setQuery($query);
$db->query();
But i dont know how to pass these values in the following variables so i can use them in if statements later
$name= //retured value from column Name
$redeemed= //retured value from column Redeemed
$redeem_date= //retured value from column Redeem_date
just like this..
// Your query here..
$query = "SELECT `Name`,`Redeemed`,`Redeem_date` FROM `Serials` WHERE `Serial` = '$serial' AND `Code` = '$code'";
$db->setQuery($query);
$results = $db->query();
//fetch data and stored into variables
while($row = fetch_array($results)){
$name = $row['Name'];
$redeemed = $row['Redeemed'];
$redeem_date = $row['Redeem_date'];
}
try something like this :
<?php
$result = $db->query("SELECT `Name`,`Redeemed`,`Redeem_date` FROM `Serials` WHERE `Serial` = '$serial' AND `Code` = '$code'");
while (list($name, $redeemed, $redeem_date) = $result->fetch(PDO::FETCH_NUM)) {
// DO SOMETHING
}
?>
while ($row = $db->fetch()) {
$name= $row['name'];
$redeemed= $row['redeemed'];
$redeem_date= $row['redeem_date'];
}
this one might fetch your results and assign to vars
PHP/MySQL (CodeIgniter)
I would like to add new interest_keywords in the exist database value.
here is my code
$query = 'SELECT u_interest_keyword FROM '.T_USER_ACCOUNT.' WHERE u_id = "'.$u_id.'"';
$result = $this->db->query($query);
$result_keyword = $result.','.$personal_keyword;
$query = 'UPDATE '.T_USER_ACCOUNT.' SET u_interest_keyword = "'.$result_keyword.'" WHERE u_id = "'.$u_id.'"';
$this->db->query($query);
It just replaces a new keyword in the database.
Can you tell me why it doesn't work?
$this->db->query returns object when read type queries are run.
So, you have to do something like this after $result = $this->db->query($query);
$result_row = $result->row();
Then Rectify this:
$result_keyword = $result_row->u_interest_keyword. ',' .$personal_keyword;
$row = $result->row();
$result_keyword = $row->u_interest_keyword.','.$personal_keyword;
I am trying to store a mysql value into a php variable. I have the following query which I know works. However, I the value for $count is always 0. Can someone explain what I need to do to get the count value? The count should be the count of x's w here name_x=.$id.
$query = "SELECT COUNT(name_x) FROM Status where name_x=.$id.";
$result = mysql_query($query);
$count = $result;
Is first letter in table name is really capital. Please check it first.
or Try :
$query = "SELECT COUNT(*) as totalno FROM Status where name_x=".$id;
$result = mysql_query($query);
while($data=mysql_fetch_array($result)){
$count = $data['totalno'];
}
echo $count;
$query = "SELECT COUNT(*) FROM `Status` where `name_x`= $id";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$count = $row[0];
please try it
$query = "SELECT COUNT(*) FROM Status where name_x=$id";
$result = mysql_query($query);
$count = mysql_result($result, 0);
You are missing single quotes around $id. Should be
name_x = '" . $id . "'";