TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   plz help me here (http://www.talkphp.com/absolute-beginners/1964-plz-help-me-here.html)

webtuto 01-15-2008 03:33 PM

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

Alan @ CIT 01-15-2008 03:45 PM

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.

webtuto 01-15-2008 03:50 PM

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)

Salathe 01-15-2008 03:58 PM

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.

Alan @ CIT 01-15-2008 04:00 PM

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

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 :-D

Alan.

webtuto 01-15-2008 04:11 PM

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" }

Alan @ CIT 01-15-2008 04:29 PM

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.

webtuto 01-15-2008 04:33 PM

yeah im trying to retrieve the highest number
and in the table tuto

Alan @ CIT 01-15-2008 04:52 PM

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 :-D

Alan.

webtuto 01-15-2008 04:57 PM

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

webtuto 01-15-2008 05:08 PM

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

Alan @ CIT 01-15-2008 05:09 PM

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.

webtuto 01-15-2008 05:16 PM

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

Alan @ CIT 01-15-2008 05:36 PM

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

webtuto 01-15-2008 06:56 PM

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 


Alan @ CIT 01-15-2008 07:04 PM

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.


All times are GMT. The time now is 07:30 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0