need some help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Toktam
Forum Newbie
Posts: 6
Joined: Mon May 19, 2008 10:55 am

need some help

Post by Toktam »

Hi all,

Can we use php variable as a table in mysql_query?
I did this :

$count_bor =mysql_query("select movie_id,count(borrow_date) as count_borrow from borrow_history group by movie_id" );
$max_count =mysql_query("select max(count_borrow),movie_id from $count_bor group by count_borrow" );
$var=mysql_result($max_count,1,"movie_id");

it gives me this error:Warning: mysql_result(): supplied argument is not a valid MySQL result resource
User avatar
emmbec
Forum Contributor
Posts: 112
Joined: Thu Sep 21, 2006 12:19 pm
Location: Queretaro, Mexico

Re: need some help

Post by emmbec »

Yes, that error is correct, you are not storing the results itself in your variable $count_bor you are storing a resultset (I think that is what it is), you need to fetch the results information from your variable $count_bor. Just echo $count_bor and you will see what I mean.
Toktam
Forum Newbie
Posts: 6
Joined: Mon May 19, 2008 10:55 am

Re: need some help

Post by Toktam »

Thanks emmbec,

I tried that,That's a pointer,right?
But how I can fetch the result?

Thanks again :)
User avatar
emmbec
Forum Contributor
Posts: 112
Joined: Thu Sep 21, 2006 12:19 pm
Location: Queretaro, Mexico

Re: need some help

Post by emmbec »

Take a look at the following tutorial:

http://www.php-mysql-tutorial.com/php-mysql-select.php
Toktam
Forum Newbie
Posts: 6
Joined: Mon May 19, 2008 10:55 am

Re: need some help

Post by Toktam »

Thanks for the tutorial,

I looked at it but i couldn't find how we can store those information in the table
as I need a table for the second query.

Thanks a lot
User avatar
emmbec
Forum Contributor
Posts: 112
Joined: Thu Sep 21, 2006 12:19 pm
Location: Queretaro, Mexico

Re: need some help

Post by emmbec »

Toktam wrote: $count_bor =mysql_query("select movie_id,count(borrow_date) as count_borrow from borrow_history group by movie_id" );
That query will give you the ID of a MOVIE right??? is that the name of your table?? What are you trying to accomplish with that query??? will that query give you a table name??? if so, just do what the tutorial says, get the result from that query, store it in another variable and then put it in a second query, is that simple.
Toktam
Forum Newbie
Posts: 6
Joined: Mon May 19, 2008 10:55 am

Re: need some help

Post by Toktam »

I'm asking too many questions,sorry.
if I do $row=mysql_fetch_array($count_bor,MYSQL_BOTH),it means that I store the table in row variable?
It still gives me error when I change the count_bor to row in the second query
User avatar
emmbec
Forum Contributor
Posts: 112
Joined: Thu Sep 21, 2006 12:19 pm
Location: Queretaro, Mexico

Re: need some help

Post by emmbec »

Toktam wrote: if I do $row=mysql_fetch_array($count_bor,MYSQL_BOTH),it means that I store the table in row variable?
You have an array there, if your query just returned ONE result, you could also use mysql_fetch_row (You will still get an array) and use the array content in the index 0. "$row[0]" that should have the table name, ONLY if your query returned the table name in the first column, otherwise just point to the right column index.
Toktam
Forum Newbie
Posts: 6
Joined: Mon May 19, 2008 10:55 am

Re: need some help

Post by Toktam »

I tried that way but it just printed movie_ids not the name of the table
How I can find the name of table ? :(
User avatar
emmbec
Forum Contributor
Posts: 112
Joined: Thu Sep 21, 2006 12:19 pm
Location: Queretaro, Mexico

Re: need some help

Post by emmbec »

What do you mean the name of the table??? isn't it borrow_history???? It would help if you post your database information and the expected results. If you want to get the name of the TABLES inside the databse you use the SHOW TABLES query. It seems like you're really lost...
Toktam
Forum Newbie
Posts: 6
Joined: Mon May 19, 2008 10:55 am

Re: need some help

Post by Toktam »

yes,i'm really lost.
Actually this is my first php try.
Anyway I have a table with movie_ids and the dates they have been borrowed as multi_value attributes.
I tried to make a table of movie_ids and the number of times they have been borrowed by the first query
and I need the movie_id with the maximum borrowed times.(I need ten maximum movies)
That is what I tried to do with second query...
User avatar
emmbec
Forum Contributor
Posts: 112
Joined: Thu Sep 21, 2006 12:19 pm
Location: Queretaro, Mexico

Re: need some help

Post by emmbec »

That looks like a database question, you should have posted your question in that board. Please post your table structure if possible, post the SQL queries to create the tables so I can see what you have and what you really need. I think you can only use one query for that, you don't need to use two separate queries to get your results.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: need some help

Post by califdon »

Toktam, you can probably tell that emmbec had trouble understanding what you're trying to do, and so did I. You really have to begin with a basic understanding of databases and PHP, to even express what you want to do. I strongly suggest that you learn more about these subjects by reading tutorials such as this one: http://www.w3schools.com/PHP/php_mysql_intro.asp and then return here with your questions. It is extremely difficult to help someone who doesn't yet grasp the basics. Please understand, everyone starts out with no knowledge, but you really need to do some of the homework before asking detailed questions.
Post Reply