I know the solution to this question is super simple, but I'm missing something.
I have a database filled with people's names. Most will be displayed # URL's like this:
MySite/People/Abraham_Lincoln
However, people who are only important in Washington State will be displayed # URL's like this:
MySite/People/Washington/Gary_Locke
Abraham Lincoln is simple because the value in the database table, field URL (Abraham_Lincoln), matches the URL (Abraham_Lincoln). However, the database value Gary_Locke does NOT match the URL Washington/Gary_Locke.
So I'm trying to modify my query so that if a person is associated with Washington (WHERE Site = 'WA'), the webpage URL ($MyURL) won't equal the database table field URL but Washington/ + URL.
But how do I append 'Washington/' and URL? The following query returns 0 results when I test it in SQL, and it doesn't work at all in my PHP page.
$result = mysql_result(mysql_query("SELECT COUNT(URL)
FROM people
WHERE URL = '$MyURL' AND Site = 'PX'
OR '$MyURL' = 'Washington/'URL AND Site = 'WA'"),0);
So, again, if I'm focusing on Gary Locke (Gary_Locke in field URL), then I want to be able to display information about him at MySite/People/Washington/Gary_Locke, NOT MySite/People/Gary_Locke.
Can anyone tell me what I'm doing wrong? Thanks.
Concat will join the Washington to URL see below
$result = mysql_result(mysql_query("SELECT COUNT(URL)
FROM people
WHERE URL = '$MyURL' AND Site = 'PX'
OR '$MyURL' = CONCAT('Washington/', URL) AND Site = 'WA'"),0);
Can I also suggest doing it this way, so that if you have an sql error, it will display the error, so you can tell what is wrong
$res = mysql_query("SELECT COUNT(URL)
FROM people
WHERE URL = '$MyURL' AND Site = 'PX'
OR '$MyURL' = CONCAT('Washington/', URL) AND Site = 'WA'");
if (!$res) {
die('Invalid query: ' . mysql_error());
}
$result = mysql_result($res, 0);
I also hope you are escaping your parameters, this will do the trick. (search for sql injection if you are not sure why that is a good idea)
$MyURL = mysql_real_escape_string($MyURL);
And finally, the mysql_ set of functions will be removed from the latest version of php very soon. You should look at mysqli or pdo if this is a long term project.
Related
I have a query that works fine when randomly selecting a new product from the database. But to prevent duplication, in my view I pass a JSON object of products already for sale on the page. I want my query to essentially query my DB for products for sale but are also not in the collection of products already for sale.
This part works absolutely fine
$nproduct = Product::whereIn('seller_id', Auth::user()->samegroup())->inRandomOrder()->first();
$nprice = $nproduct->price;
$nquantity = $nproduct->quantity_available;
$nid = $nproduct->id;
$nseller = $nproduct->seller_id;
But I struggle when trying to make sure my query doesn't include anything inside $products_already_for_sale
$products_already_for_sale = $request->current_product_ids;
try this
$products_already_for_sale = $request->current_product_ids; //it must be array []
$nproduct = Product::whereIn('seller_id', Auth::user()->samegroup())->whereNotIn('product_id',
$products_already_for_sale)->inRandomOrder()->first();
totally untested, but I think this would work
I need to get the name of each like on each post on a group page. Since I first have to get some data from the original post, I'm trying to make a connection then iterate through the likes and get a list of the users who liked each post. Here's what I have:
Connection<Likes> feedLikes = postFeed.fetchConnection(id+"/likes", Likes.class, Parameter.with("fields","from,actions,type"));
// Get the iterator
Iterator<List<Likes>> likeIt = feedLikes.iterator();
while(likeIt.hasNext()) {
List<Likes> likeFeed = likeIt.next();
for (Likes currLike: likeFeed) {
String ObjectId = id;
String LikeUserId = currLike.getId();
String LikeUserName = currLike.getName();
like_data.add(new String[] {ObjectId, LikeUserId, LikeUserName});
}
}
This doesn't work and I'm a little stuck on why. I know the username is stored in Likes.LikeItem but I can't even get to that step so far. Does anyone have any idea what I'm missing?
According to the Facebook reference this is not possible (https://developers.facebook.com/docs/graph-api/reference/v3.2/object/likes):
A User or Page can only query their own likes. Other Users'or Pages' likes are unavailable due to privacy concerns.
Only aggregated counts using total_count with the summary parameter are available for Post likes.
I have a simple Django App having database interactions. I need to make the functionality of Video Visiting counter. So that I need to update- increment the counter each time when user visit the video.
I have a video object on template page(video-details.html).
This is how I access the video_file_name.
<h1 id="video1">{{video_obj.video_file_name}}</h1>
I have video model as:
class Video_Mapping(models.Model):
video_file_name = models.CharField(max_length=100)
video_description = models.CharField(max_length=2000, default='Video Description')
created_at = models.DateTimeField(auto_now_add=True)
last_modified = models.DateTimeField(auto_now=True)
video_category_id = models.IntegerField(default=1)
video_seen_count = models.IntegerField(default=0)
I want to update the video_seen_count model value on template page.
More info: As I can do similar thing in View like following.
video = Video_Mapping.objects.get(pk=video_id);
video.video_description = description; video.save();
Please suggest me the best way to do it in the video-detail.html template page.
The simplest thing which I have done here is as per #birophilo'suggestion.
I was actually finding the way to increment the video_seen_count value from template(html page).
But after doing more research and try as suggested in comment, I decided to make this increment operation before the template renders(So in the view).
So it worked like:
video.video_screen_count += 1
video.save();
Thanks.
I'm trying to print a few fields in a block on each profile page.
The block needs to display the fields of the user being viewed, not the logged in user.
$account = user_load($node->uid); - doesn't work. user->uid doesn't either.
Globals user will return the logged in users info.
Not exactly sure how I'm supposed to load anything into a block. Any idea?
Assuming you're viewing a user profile with a URL path like this
drupal/user/USER_ID
You can do as follows :
// Option 1
$user = explode('/', current_path());
$user_id = end($user);
// Option 2
// $path = explode('/', current_path());
// $user_id = $path[count($path)-1];
$account = user_load($user_id, true);
Documentation current_path()
This way you get the last part of the URL and pass it to the user_load function. We set true as second parameter so it loads from the DB and not the cache.
Another way to do the same without breaking the URL into pieces is to use arg() to get the parameters
In this case the code would look like this
// Option 3
// drupal/user/USER_ID
// drupal = arg(0)
// user = arg(1)
// USER_ID = arg(2)
$account = user_load(arg(2), true);
In both cases check that $user_id is an integer, do not let it pass at least is an integer, then you can check the result of user_load.
Update
If you are using the user's name in the URL you can load a full user object by using user_load_by_name()
So the code would change a little bit. Try this :
$account = user_load_by_name(arg(2));
I used arg for simplicity, you can get the user's name from URL as you want.
Hope it helps.
I have been trying to run this query:
my_post = db.GqlQuery("SELECT * FROM Post where __key__ = KEY('Post', 1)")
self.render("showpost.html", my_post = my_post)
and my template showpost.html looks like this:
<div class="post">
<h3 class="post-subject">{{ my_post.subject }}</h3>
<div class="post-content">{{ my_post.content }}</div>
</div
Instead of getting the details of the Post of id=1 I'm getting a blank page. Although there exists a post with id=1 i'm not sure how to confirm if the GQLquery is actually returning something. Is there a way to check?
On trying the following:
my_post = db.GqlQuery("SELECT * FROM Post where subject = 'hello_world')
self.render("showpost.html", my_post = my_post)
I get the same issue (blank page), even though there is post who's subject is hello_world
Thanks a lot
GqlQuery is a class.
Your code has created an instance of the GqlQuery class.
To return something from the datastore you need to use either of the following instance methods:
.fetch() # requires number of entities as first argument
.get()
Example:
my_query = db.GqlQuery("SELECT * FROM Post WHERE subject = :subject",
subject="hello_world")
my_post = my_query.get() # the magic happens here
self.render("showpost.html", my_post = my_post)
Bernie is right in that you need to use .fetch or .get to actually retrieve results from the GqlQuery that you created, but for the first version, where you have a key or an id, you don't want to create a query at all. Rather, use the methods that are provided for using them directly.
If you have an id, use:
my_post = Post.get_by_id(id)
this will be much faster than using a query object.
If you have an entire key, use:
my_post = Post.get(key)
That is the fastest and most efficient way to get an entity out of the database.