Forum: Geek Forum Topic: Any PHP guru's???? started by: jim Posted by jim on Jan. 22 2001,11:52
I'm about to start a redesign of a site as a favor.The thing is, they only have PHP and mySQL available to them. I'm all about ASP!!! Are then any PHP guru's out there that can provide some guidence from time to time??? First problem. Generating a random record from a database. The way I did it in ASP (I know this is a bad way) Was first I'd get a record count. Then I'd generate a random number between 1 and the number of records. Then I'd simply query the database for the record with the ID field = to the random number. This is bad, because if I delete a record then I could get some bad results. The best way to have done it would have been to move the cursor. But since I'm not really a programmer, and I have no intension of deleteing any records, this did the job fine. Now transporting this code to PHP is another story.... Below is my Random record code.... code:<\%@LANGUAGE="VBSCRIPT"\%> Any help or links to help would be greatly appreciated. Posted by whtdrgn_2 on Jan. 22 2001,16:19
If you have the /usr/local/lib/php.ini set up for asp_tags = on then you can use asp tags in PHP, this helps with editors that don't understand <?php ?> tags.Here is a ruff idea of what you wan't, If I can have access to the DB (select only), I can polish the code. <?php //Declare variables //Open connection to MySQL DB and select DB //Execute a select all query to get all records - needed to cound records //Get the count of records for a given result. //Generate random number with 1 fr min and $record_cound for max //Fetch result data and cast into an object (easy to advance records) while($data != $random){ $data++; } //Ouput data - Note: since data is an object you reference the $data abject we mysql_close($conn); ?>
This message has been edited by whtdrgn_2 on January 23, 2001 at 11:25 AM Posted by whtdrgn_2 on Jan. 23 2001,05:33
Boy what a dumbass, I posted a new topic not a new reply. Here is the right place for the code (if you don't want to trust a single bit of my code now I understand)
//Declare variables //Open connection to MySQL DB and select DB //Execute a select all query to get all records - needed to cound records //Get the count of records for a given result. //Generate random number with 1 fr min and $record_cound for max //Seek the record and fetch the result and cast into an object (easy to advance records) //Ouput data - Note: since data is an object you reference the $data abject we mysql_close($db_conn); ?> ------------------ Posted by jim on Jan. 23 2001,05:37
quote: Can't this be done in PHP by doing a Wouldn't that be more efficient? I don't know, I'm asking.... ------------------ Posted by whtdrgn_2 on Jan. 23 2001,05:46
Most of my work is done in PostgreSQL (as of a month ago), and I have forgotten all of the MySQL funstions. In PostgreSQL then yes, ins MySQL I am not 100\%. You would still have to make two queries. Once count query and one select query. With my way (no ego intended), you only make one query. For a busy site that will be important, because MySQL is prone to locking (it has table level locking not row level). EDIT: This might have changed now that 2.23 is stable, I am not sure if the row revel the NuSphere wrote got dropped in yet. Do you know? NOTE: Remeber you are taking one query result, and usign it to count, and to get data. That result can be used for the life of the page. I always trie to keep one query per page (most of my work does not permit it though - with MySQL). Good relational design, and triggers can help in that case but not in MySQL (no I don't hate it, I just prefer others) ------------------ This message has been edited by whtdrgn_2 on January 23, 2001 at 12:52 PM Posted by whtdrgn_2 on Jan. 23 2001,13:56
Well, how did that work for you? What version of PHP are you running.------------------ Posted by jim on Jan. 23 2001,18:10
I'm not to that point yet.Are website planning meeting is tomorrow. But believe me, I'm going to go there with the sole intension of selling them on moving to a Win2k box with halfpricehosting!!! Then I don't have to learn PHP! Woo Hoo! ------------------ Posted by whtdrgn_2 on Jan. 25 2001,10:02
PHP runs on IIS, and it works nice, and PHP is a 'c like' language, and c is a good language. Keep that in mind, you learn PHP you can lear any language.------------------ |