Page 1 of 1
need some help
Posted: Mon May 19, 2008 11:26 am
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
Re: need some help
Posted: Mon May 19, 2008 2:20 pm
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.
Re: need some help
Posted: Tue May 20, 2008 8:55 am
by Toktam
Thanks emmbec,
I tried that,That's a pointer,right?
But how I can fetch the result?
Thanks again

Re: need some help
Posted: Tue May 20, 2008 9:11 am
by emmbec
Re: need some help
Posted: Tue May 20, 2008 9:40 am
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
Re: need some help
Posted: Tue May 20, 2008 9:48 am
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.
Re: need some help
Posted: Tue May 20, 2008 10:18 am
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
Re: need some help
Posted: Tue May 20, 2008 10:28 am
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.
Re: need some help
Posted: Tue May 20, 2008 10:59 am
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 ?

Re: need some help
Posted: Tue May 20, 2008 11:10 am
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...
Re: need some help
Posted: Tue May 20, 2008 11:19 am
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...
Re: need some help
Posted: Tue May 20, 2008 11:25 am
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.
Re: need some help
Posted: Tue May 20, 2008 12:38 pm
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.