TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-15-2008, 03:33 PM   #1 (permalink)
The Addict
 
webtuto's Avatar
 
Join Date: Dec 2007
Location: morocco
Posts: 221
Thanks: 19
webtuto is on a distinguished road
Default plz help me here

hi
im trying to add best rated articles so i made this code

PHP Code:
$web "SELECT max(rate) as rate from `rate`";
    
$tuto mysql_query($web) or die('Database error: ' mysql_error());
    
$mas mysql_fetch_array($tuto);
     
$round $mas['tuto_id'];
  
$web2="select * from tuto where id='$mas[tuto_id]'";
  
$res2=mysql_query($web2);
  while(
$mas2 mysql_fetch_array($res2)) {
   echo 
$mas2['title'];
   } 
well it douesnt give any error just blank
__________________
Send a message via MSN to webtuto Send a message via Yahoo to webtuto Send a message via Skype™ to webtuto
webtuto is offline  
Reply With Quote
Old 01-15-2008, 03:45 PM   #2 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Ahh, the joys of debugging :) My first steps in this situation would be to do some basic checks to make sure the queries are running correctly.

For example, try putting:

PHP Code:
var_dump($mas); 
under your first mysql_fetch_array() to see if your query is actually getting any results. If that works ok, put an " or die(mysql_error());" statement after your second mysql_query() ($web2) to see if that produces any errors. If that's all ok, then the next step would be to remove the while() loop and replace it with:

PHP Code:
$mas2 mysql_fetch_array($res2);
var_dump($mas2); 
This would tell you if that array contains anything.

Performing these debugging steps should give you a clue as to where the problem lies

Alan.
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 01-15-2008, 03:50 PM   #3 (permalink)
The Addict
 
webtuto's Avatar
 
Join Date: Dec 2007
Location: morocco
Posts: 221
Thanks: 19
webtuto is on a distinguished road
Default

when i added var_dump the result is =>

array(2) { [0]=> string(1) "5" ["rate"]=> string(1) "5" }




and when i add
PHP Code:
 $mas2 mysql_fetch_array($res2);
var_dump($mas2); 
it gives me this =>

Code:
bool(false)
__________________
Send a message via MSN to webtuto Send a message via Yahoo to webtuto Send a message via Skype™ to webtuto
webtuto is offline  
Reply With Quote
Old 01-15-2008, 03:58 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

On the fourth line you are trying to get the value of $mas['tuto_id'], but in your SQL query you only ask for the rate and no other columns. Therefore, tuto_id will not be available to use! The same problem exists for the query assigned to $web2 since no valid ID number is available.

It is good practice to add in some checks into your scripts so that you can recognise errors or problems. You should always check to see whether MySQL encountered any error when processing a query (mysql_query returns FALSE on error). Also you should check to see if the returned resuls are what you were looking for. For example, if no rows were returned you should output a message saying that nothing was returned, rather than just outputting nothing.
Salathe is offline  
Reply With Quote
Old 01-15-2008, 04:00 PM   #5 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Edit: Dam my slow typing - Salathe beat me to it

Ok, so that means that $mas2 = false - when in reality, you where expecting it to contain an array (the rows from your query).

As such, it's probably a good guess that your query:

PHP Code:
"select * from tuto where id='$mas[tuto_id]'" 
Is not returning any results. Looking at your query and your debugging results posted above, I can see that it's the "..where id='$mas[tuto_id]'" that is the problem.

If you take another look at your first var_dump() results, you will see that your query is only returning ['rate'] - which in this case equals 5. As such, $mas['tuto_id'] doesn't exist as your query doesn't return it.

I would recommend changing your first query so that it fetches tuto_id as well. This should resolve your problem.

Just remember in future, if in doubt - stick var_dump() statements all over your code until you track down the problem

Alan.
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 01-15-2008, 04:11 PM   #6 (permalink)
The Addict
 
webtuto's Avatar
 
Join Date: Dec 2007
Location: morocco
Posts: 221
Thanks: 19
webtuto is on a distinguished road
Default

thanks guys so i changed the query and make it like this

PHP Code:
$web "SELECT max(tuto_id) as rate from `rate`"
but still the blank and iadd var_dump($mas); and it gives this

Code:
array(2) { [0]=> string(2) "29" ["rate"]=> string(2) "29" }
__________________
Send a message via MSN to webtuto Send a message via Yahoo to webtuto Send a message via Skype™ to webtuto
webtuto is offline  
Reply With Quote
Old 01-15-2008, 04:29 PM   #7 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Your new query is now selecting the tuto_id that has the highest number - max(tuto_id) - then assigning it to a variable called 'rate'.

What are you actually trying to retrieve from your table? Am I right in guessing that you want to get the 'tuto_id' for the row that has the highest 'rate' ?

Alan.
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 01-15-2008, 04:33 PM   #8 (permalink)
The Addict
 
webtuto's Avatar
 
Join Date: Dec 2007
Location: morocco
Posts: 221
Thanks: 19
webtuto is on a distinguished road
Default

yeah im trying to retrieve the highest number
and in the table tuto
__________________
Send a message via MSN to webtuto Send a message via Yahoo to webtuto Send a message via Skype™ to webtuto
webtuto is offline  
Reply With Quote
Old 01-15-2008, 04:52 PM   #9 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

In that case, I would use a query like the following:

PHP Code:
$web "SELECT tuto_id, rate FROM tuto ORDER BY rate DESC LIMIT 1"
This would select 'tuto_id' and 'rate' from the 'rate' table. It then sorts them by 'rate' in descending order (ie, highest first), then limits the results to 1. That way, you should only end up with 1 row, the one with the highest rating, containing the tuto_id and rate values.

Note: No-one has ever mistaken me for an SQL expert so I don't know if my query above is the "right" way to do it, but if it's wrong I'm sure someone else here can improve it

Alan.
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 01-15-2008, 04:57 PM   #10 (permalink)
The Addict
 
webtuto's Avatar
 
Join Date: Dec 2007
Location: morocco
Posts: 221
Thanks: 19
webtuto is on a distinguished road
Default

i cant use ur quesry so i edit it like that
$web = "SELECT tuto_id, rate FROM rate ORDER BY rate DESC LIMIT 1";

bcz tuto_id and rate are in table rate not on table tuto :s
but still no results
__________________
Send a message via MSN to webtuto Send a message via Yahoo to webtuto Send a message via Skype™ to webtuto
webtuto is offline  
Reply With Quote
Old 01-15-2008, 05:08 PM   #11 (permalink)
The Addict
 
webtuto's Avatar
 
Join Date: Dec 2007
Location: morocco
Posts: 221
Thanks: 19
webtuto is on a distinguished road
Default

thanks now it works the problem was stupid sorry just from the database lol
but it just give 1 result i edit the query to be "LIMIT 4"
and i want to show 4 results
but just give 1 :s
__________________
Send a message via MSN to webtuto Send a message via Yahoo to webtuto Send a message via Skype™ to webtuto
webtuto is offline  
Reply With Quote
Old 01-15-2008, 05:09 PM   #12 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Edit: Didn't see the post above

Yes, just change the "LIMIT 1" to "LIMIT 4" if you want it to return the 4 rows with the highest rating

Don't forget that when you have changed the query, you will need to put your while() loop back in to loop through them

Alan.
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 01-15-2008, 05:16 PM   #13 (permalink)
The Addict
 
webtuto's Avatar
 
Join Date: Dec 2007
Location: morocco
Posts: 221
Thanks: 19
webtuto is on a distinguished road
Default

i did it here is the code

PHP Code:
$web "SELECT tuto_id,rate FROM rate ORDER BY rate DESC LIMIT 2";
    
$tuto mysql_query($web) or die('Database error: ' mysql_error());
    
$mas mysql_fetch_array($tuto)or die(mysql_error());
     
$round $mas['tuto_id'];
  
$web2="select * from tuto where id='$mas[tuto_id]'";
  
$res2=mysql_query($web2);
while(
$mas2 mysql_fetch_array($res2)){ 
   echo 
"<tr><td>Most 4 best rated tutorials</tr><tr><td>".$mas2['title']."</tr>";
   } 
but still give just 1 result :s
__________________
Send a message via MSN to webtuto Send a message via Yahoo to webtuto Send a message via Skype™ to webtuto
webtuto is offline  
Reply With Quote
Old 01-15-2008, 05:36 PM   #14 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

The new problem is because your second query only fetches 1 row.

I've adjusted your code so that it should read multiple rows using the SQL IN() function.

PHP Code:
$web "SELECT tuto_id,rate FROM rate ORDER BY rate DESC LIMIT 2"
$tuto mysql_query($web) or die('Database error: ' mysql_error()); 

// Loop through the query results...
while ($mas mysql_fetch_array($tuto))
{
    
// ...and put each tuto_id into a new array...
    
$inClause_a[] = $mas['tuto_id'];
}

// ...which we then turn into a comma seperated string by using implode()
$inClause implode(','$inClause_a);

$web2="select * from tuto where id IN($inClause)'"
$res2=mysql_query($web2); 

while(
$mas2 mysql_fetch_array($res2))
{  
    echo 
"<tr><td>Most 4 best rated tutorials</tr><tr><td>".$mas2['title']."</tr>"

I haven't tested the code but it should work.

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 01-15-2008, 06:56 PM   #15 (permalink)
The Addict
 
webtuto's Avatar
 
Join Date: Dec 2007
Location: morocco
Posts: 221
Thanks: 19
webtuto is on a distinguished road
Default

it gives an error

PHP Code:
Warningmysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\tuto\index.php on line 52 
__________________
Send a message via MSN to webtuto Send a message via Yahoo to webtuto Send a message via Skype™ to webtuto
webtuto is offline  
Reply With Quote
Old 01-15-2008, 07:04 PM   #16 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Then you need to do some debugging to find out where the problem is. For example, it looks like I mistyped something in the second query, so put an " or die(mysql_error())" after your mysql_query() and that should give us an error message.

You can then use this error message to track down the problem.

Alan.
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:56 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design