Click to See Complete Forum and Search --> : need some help with phph & mysql


izlik
10-10-2007, 05:48 AM
i have this code above that goes trou my database and lists the 10 latests added things by date, and i wonder how i can add a link to those items it searches and picks out. the link should be like http://mydomain.com/[ID of the DB entry] but the name that it picks from the database should still be the link text.

i hope someone can help me out.

<?php
$con = mysql_connect("localhost","login","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("database", $con);

$result = mysql_query("SELECT * FROM games WHERE id > 0 ORDER BY `added` LIMIT 10");

while($row = mysql_fetch_array($result))
{
echo $row['name'];
echo "<br />";
}

mysql_close($con);
?>

<?for ($i=0;$i<$resultsCount;$i++){
$num = $i+1;
echo $num.":".$results[$i];
<?}?>

Webnerd
10-10-2007, 07:41 AM
echo '<a href="http://yoursite.com/'.$row['id'].'">'.$row['name'].'</a>';

izlik
10-10-2007, 11:12 AM
i did that and get > warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /public_html/plugins/site/themes/awesome/test.php on line 12

JoeHoldcroft
10-10-2007, 12:03 PM
Could you post your new code so we can inspect line 12? Check that the resource you used in mysql_fetch_array is a query result.

izlik
10-10-2007, 04:39 PM
it is working now! :D

however i have a little problem. when it picks games from the database there is 2000 games in total, and 1999 dont have a date set due to a database problem, i added a game today for test with got the date "10:10:2007" set to it and the other date lines for the other 1999 games are just blank and it picks those blank date lines insteed of the game that has a date set to it, why? the full code is bellow.



<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("database", $con);

$result = mysql_query("SELECT * FROM games WHERE id > 0 ORDER BY `name` LIMIT 10");

while($row = mysql_fetch_array($result))
{
echo '<a href="http://mysite.com/index.php?params=game/'.$row['id'].'">'.$row['name'].'</a>';
echo "<br />";
}

mysql_close($con);
?>

<?for ($i=0;$i<$resultsCount;$i++){
$num = $i+1;
echo $num.":".$results[$i];
}
?>

izlik
10-11-2007, 09:32 PM
anyone please?

MrCoder
10-12-2007, 07:14 AM
$result = mysql_query("SELECT * FROM games WHERE id > 0 ORDER BY `date` DESC, `name` LIMIT 10");

Webnerd
10-12-2007, 10:28 AM
An empty date field can yield results like '0000-00-00 00:00:00' which is in fact less than the date you specify. If your field IS NOT a true date field, you are doing a string comparison where '' is also less than a date value. So, first make sure your MySQL date is a 'date' field. If it is stored like '10:10:2007' I don't believe you have a valid date field

izlik
10-13-2007, 03:15 PM
$result = mysql_query("SELECT * FROM games WHERE id > 0 ORDER BY `date` DESC, `name` LIMIT 10");


adding that gave me an error :/

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /public_html/plugins/site/themes/awesome/test.php on line 16

izlik
10-13-2007, 03:29 PM
An empty date field can yield results like '0000-00-00 00:00:00' which is in fact less than the date you specify. If your field IS NOT a true date field, you are doing a string comparison where '' is also less than a date value. So, first make sure your MySQL date is a 'date' field. If it is stored like '10:10:2007' I don't believe you have a valid date field

i made the collumn myself and update the date manually becuase it's not in the script for the page to add it and it cant be added becuase it's a locked script, if the collumn is not a valid date field, how do i make it to be ?

\\.\
10-13-2007, 03:41 PM
Try : http://uk.php.net/manual/en/function.mysql-fetch-assoc.php