Im trying to take an array of titles from the column 'list' and then get details for every title on said list.
//Get list
$r=$link->prepare('SELECT list FROM lists WHERE user=? LIMIT 1');
$r->bindValue(1,$user,PDO::PARAM_STR);
$r->execute();
$list=$r->fetchAll(PDO::FETCH_ASSOC);
//See how many titles the list has
$list_length=count($list);
//Get details for every title
for($i=0;$i<$list_length;$i++)
{
$r2=$link->prepare('SELECT * FROM details WHERE title=?');
$r2->bindValue(1,$list[$i],PDO::PARAM_STR);
$r2->execute();
$details=$r2->fetchAll(PDO::FETCH_ASSOC);
}
return json_encode($details);
As it is I get echoed just [].
I also get Notice: Array to string conversion in $r2->bindValue(1,$list[$i],PDO::PARAM_STR);
You have to learn to read your code, instead of asking others to do it for you.
SELECT list FROM lists WHERE user=? LIMIT 1
Look at this query. Does it looks like one that can return an array of rows?
Learn SQL. And JOINS particularly
And here goes the code
$sql = 'SELECT details.* FROM lists JOIN details on title=list WHERE user=?';
$stm = $link->prepare($sql);
$stm->execute([$user]);
json_encode($stm->fetchAll(PDO::FETCH_ASSOC));
Just FOUR lines
Related
I have a users table in mysql with userid, username, password etc
Using mysql-otp i query like
select * from users where username = ?
Its returns ColumnNames and Rows.
Rows are like
[[38, <<"joe">>, <<"passwordhash">>..]]
Suppose i have a hash to compare
Hash = "passhash".
As its my 3rd day coding in erlang, what i am currently doing/testing is
[[_, _, UserPass, _,..]] = Rows.
Which stores password in UserPass.
Pass = binary_to_list(UserPass).
Which i can then compare like
Hash == Pass.
Is this approach correct or i am doing it all wrong?
There must be a proper way of getting data out of what is supposedly list inside a list.
There must be a proper way of getting data out of what is supposedly
list inside a list
The proper way to get at your target data is to use pattern matching to deconstruct whatever type of collection contains your data. Because mysql-otp returns a list of rows matching the query, where each row itself is a list, the data is in the form of a list of lists. Therefore, in order to match a list of lists your pattern also has to be a list of lists.
As its my 3rd day coding in erlang, what i am currently doing/testing
is
[[_, _, UserPass, _,..]] = Rows.
Bravo.
If you know mysql-otp will only return one row, or you are only interested in the first row, you could simplify the pattern like this:
[_, _, UserPass, _, ...] = hd(Rows).
hd is shorthand for head, i.e. the head of the list. Or, you could accomplish the same thing by hand like this:
[FirstRow | _Tail] = Rows,
[_, _, UserPass, _, ...] = FirstRow.
=======
Another way to extract the password:
UserPass = lists:nth(3, hd(Rows)).
I am currently working on a school project and am trying to retrieve values from a successfully ran query. The query is as so:
$airportQuery = Airport::where('id', '=', $airport)->get(); //Returns all columns of users selected airportId
I am trying to retrieve a column "extendedcenterlineLong" and "extendedcenterlineLat". I am doing this by running
array[] = $airportQuery->extendedcenterLong;
array[] = $airportQuery->extendedcenterLat;
(The array isn't named array[] either) When I try to run myQuery I get this error
I have not been able to fix this issue, what am I doing wrong?
Many Thanks!
EDIT: I also have these queries to get the previous row or next row from waht the user selects
$previous = Airport::where('id', '<', $airport)->max('id'); //Returns the previous rows values from users current selected airportId
$next = Airport::where('id', '>', $airport)->min('id'); //Returns the next rows values from users current selected airportId
EDIT: I solved the problem by doing $airportQuery->first()->column_name. For some reason when I printed out $airportQuery there were two items that were arrays of the column info that were identical to each other, a copy basically.
You can just do a
$airportQuery = Airport::where('id', '=', $airport)->first();
i guess above should work.
I am getting an array of category id eg. ['1','2','3']
and in my database table it is stored as 1_abc eg : category : 1_Sameer
Now it is easy if i get one value
ie. WHERE category like 1_%;
But since this is an array how can i use like or regex to compare with the database values
In this case you can do as many queries as you have array elements and merge these results together if needed to get single collection of elements.
$results = new Collection();
foreach($ids as $id) {
$categories=Category::where("id","like","$id_%")->get();
$results=$results->merge($categories);
}
you have to do looping for that
for($i=0;$i<=count($array_result)-1;$i++)
{
$result=mysql_query("select * from table_name where category like '{$array_result[$i]}_%' ");
}
I've been out of the mysql and perl game for quite a few years and can't seem to get this right. I have a table with just 3 columns. 'cnt' is one of them. All I want to do is query the table on 'name' and see if name exists. If it does, I want to capture the value of 'cnt'. The table has a record of testName with a value of 2 I added manually. When this script is run it returns empty.
my $count;
my $pop = qq(SELECT cnt FROM popular WHERE name="testName");
my $sth = $dbh->prepare($pop);
$sth->execute() or die $dbh->errstr;
my #return;
while (#return = $sth->fetchrow_array()) {
$count = $return[1];
}
print "our return count is $count";
Is it obvious to anyone what I did wrong?
You probably mean
$count = $return[0];
According to perl doc on mysql
An alternative to fetchrow_arrayref. Fetches the next row of data and returns it as a list containing the field values.
Since you select cnt as the return value ,so , the size of #return is 1,but you misunderstand it as the number of results which meets your query condition.No, it is not so!Please have a more careful reading of perl doc.
So, I'm trying to return a collection of People whose ID is contained within a locally created collection of ids ( IQueryable)
When I specify "locally created collection", I mean that the Ids collection hasnt come from a LinqToSql query and has been programatically created (based upon user input).
My query looks like this:
var qry = from p in DBContext.People
where Ids.Contains(p.ID)
select p.ID;
This causes the following exception...
"queries with local collections are not supported"
How can I find all the People with an id that is contained within my locally created Ids collection?
Is it possible using LinqToSql?
If Ids is a List, array or similar, L2S will translate into a contains.
If Ids is a IQueryable, just turn it into a list before using it in the query. E.g.:
List<int> listOfIDs = IDs.ToList();
var query =
from st in dc.SomeTable
where listOfIDs.Contains(st.ID)
select .....
I was struggling with this problem also. Solved my problem with using Any() instead
people.Where(x => ids.Any(id => id == x.ID))
As the guys mentioned above, converting the ids, which is of type IQueryable to List or Array will solve the issue, this will be translated to "IN" operator in SQL.But be careful because if the count of ids >= 2100 this will cause another issue which is "The server supports a maximum of 2100 parameters" and that is the maximum number of parameters(values) you can pass to "IN" in SQL server.
Another alternative would be keeping ids as IQueryable and using LINQ "Any" operator instead of "Contains", this will be translated to "EXISTS" in SQL server.
I'm sorry but the answers here didn't work for me as I'm doing dynamic types further along.
What I did was to use "UNION" in a loop which works great. Here's how:
var firstID = cityList.First().id;
var cities = dc.zs_Cities.Where(c => c.id == firstID);
foreach(var c in cityList)
{
var tempCity = c;
cities = cities.Union(dc.zs_Cities.Where(cty => cty.id == tempCity.id));
}