aragon_202_ Posted May 12, 2008 Share Posted May 12, 2008 Hi, I’m reasonable new to php coding, done my fair bit of "hacking" scripts together before hand but I have recently took up doing things from scratch. I'm attempting to build a simple app to count the rows in a MySQL database, randomise between the returned number and 1 and display the result. simple. <?php $host= "localhost"; $user="username"; $password="password"; $dbname="dbname"; $entries="SELECT COUNT(*) FROM Review"; $cxn = mysqli_connect($host,$user,$password,$dbname) or die ("connection failed lol"); $result = mysqli_query($cxn,$entries) or die ("couldent do it"); $result = $result + 1; echo "$result"; ?> the results of which was a fatal error in trying to dispaly the result, which was understandable. so i added a +1 and then displayed the result, the answer of which always displays 2 meaning it is returning 1. where as phpmyAdmin returns 3 (correct answer) meaning the script should display 4 (due to the +1) any ideas on how to fix this? Link to comment https://forums.phpfreaks.com/topic/105261-php-acting-odd/ Share on other sites More sharing options...
ToonMariner Posted May 12, 2008 Share Posted May 12, 2008 you don't need to do this in php... just use ORDER BY RAND() LIMIT 0, 1 in your select clause.... Link to comment https://forums.phpfreaks.com/topic/105261-php-acting-odd/#findComment-538975 Share on other sites More sharing options...
vurentjie Posted May 12, 2008 Share Posted May 12, 2008 how about trying mysql_num_rows to return how many entries in the database, very simply put: $entries="SELECT * FROM Review"; $result = mysql_query($entries) or die ("couldn't do it"); $numresult = mysql_num_rows($result); echo rand(1,$numresult); i noticed you say mysqli instead of mysql in above code...is that a typo or some other form of coding? later vurentjie BUt the above solution is way better, Link to comment https://forums.phpfreaks.com/topic/105261-php-acting-odd/#findComment-538977 Share on other sites More sharing options...
DJTim666 Posted May 12, 2008 Share Posted May 12, 2008 MySQLi is a different platform of MySQL. -- DJ Link to comment https://forums.phpfreaks.com/topic/105261-php-acting-odd/#findComment-538979 Share on other sites More sharing options...
aragon_202_ Posted May 12, 2008 Author Share Posted May 12, 2008 i noticed you say mysqli instead of mysql in above code...is that a typo or some other form of coding? mysqli is the new version of database connection for PHP 5 or 6 with MySQL 5.0 or 5.1 + Link to comment https://forums.phpfreaks.com/topic/105261-php-acting-odd/#findComment-538981 Share on other sites More sharing options...
vurentjie Posted May 12, 2008 Share Posted May 12, 2008 cool, learn something everyday! Link to comment https://forums.phpfreaks.com/topic/105261-php-acting-odd/#findComment-538989 Share on other sites More sharing options...
nikefido Posted May 12, 2008 Share Posted May 12, 2008 cool, learn something everyday! has a slightly diff. syntax also, so watch out (look up for mysqli functions on php.net) Link to comment https://forums.phpfreaks.com/topic/105261-php-acting-odd/#findComment-538996 Share on other sites More sharing options...
aragon_202_ Posted May 12, 2008 Author Share Posted May 12, 2008 after using the second example iv got the random number work but iv got a question as to how to use a variable with an SQL query? (code below) to then get that record and add it to an array $name="SELECT * FROM 'Review` WHERE `id` = $numresult"; $nameresult[] = mysqli_query($cxn,$name) or die ("couldent get name"); echo $nameresult[0]; Link to comment https://forums.phpfreaks.com/topic/105261-php-acting-odd/#findComment-539039 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.