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"\%> 
<\%
' FileName="Connection_ado_conn_string.htm"
' Type="ADO"
' HTTP="true"
' Catalog=""
' Schema=""
Thoughts_STRING = "Driver={Microsoft Access Driver (*.mdb)};DBQ=d:/pathtodatabase/thoughts.mdb"
\%>
<\%
set ThoughtCount = Server.CreateObject("ADODB.Recordset")
ThoughtCount.ActiveConnection = Thoughts_STRING
ThoughtCount.Source = "SELECT Count(*) as Thoughts FROM jack"
ThoughtCount.CursorType = 0
ThoughtCount.CursorLocation = 2
ThoughtCount.LockType = 3
ThoughtCount.Open()
ThoughtCount_numRows = 0
\%>
<\%
upperlimit = (ThoughtCount.Fields.Item("Thoughts").Value)
lowerlimit = 1
Randomize
RanDeepThought = Int((upperlimit - lowerlimit + 1)*Rnd() + lowerlimit)
\%>
<\%
Dim RandomThought__RanDeepThought
RandomThought__RanDeepThought = "2"
if (RanDeepThought <> "") then RandomThought__RanDeepThought = RanDeepThought
\%>
<\%
set RandomThought = Server.CreateObject("ADODB.Recordset")
RandomThought.ActiveConnection = MM_Thoughts_STRING
RandomThought.Source = "SELECT Thought FROM jack WHERE DT_ID = " + Replace(RandomThought__RanDeepThought, "'", "''") + ""
RandomThought.CursorType = 0
RandomThought.CursorLocation = 2
RandomThought.LockType = 3
RandomThought.Open()
RandomThought_numRows = 0
\%>

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
$random = 0;
$record_count = 0

//Open connection to MySQL DB and select DB
$db_conn = mysql_connect("host", "user", "passwd");
mysql_select_db("<db_name>");

//Execute a select all query to get all records - needed to cound records
$result = mysql_query("SELECT * FROM <tablename>");

//Get the count of records for a given result.
$record_count = mysql_num_rows($result);

//Generate random number with 1 fr min and $record_cound for max
$random = mt_rand(1, $record_count)

//Fetch result data and cast into an object (easy to advance records)
$data = mysql_fetch_object($result);

while($data != $random){

$data++;

}

//Ouput data - Note: since data is an object you reference the $data abject we
//created, and associated fields by $date-><field name>
print($data->Field_Name_01);
print($data->Field_Name_01);

mysql_close($conn);

?>


You can also use mysql_data_seek($result, $random) to get the random row, and then issue $data = mysql_fetch_object($result); This will speed script time. Email me for furthur questions.
------------------
Wine me, dine me, 1000101 me

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)


<?php

//Declare variables
$random = 0;
$record_count = 0

//Open connection to MySQL DB and select DB
$db_conn = mysql_connect("host", "user", "passwd");
mysql_select_db("<db_name>");

//Execute a select all query to get all records - needed to cound records
$result = mysql_query("SELECT * FROM <tablename>");

//Get the count of records for a given result.
$record_count = mysql_num_rows($result);

//Generate random number with 1 fr min and $record_cound for max
$random = mt_rand(1, $record_count)

//Seek the record and fetch the result and cast into an object (easy to advance records)
mysql_data_seek($result, $random);
$data = mysql_fetch_object($result);

//Ouput data - Note: since data is an object you reference the $data abject we
//created, and associated fields by $date-><field name>
print($data->Field_Name_01);
print($data->Field_Name_01);

mysql_close($db_conn);

?>

------------------
Wine me, dine me, 1000101 me


Posted by jim on Jan. 23 2001,05:37
quote:
Originally posted by whtdrgn_2:
//Execute a select all query to get all records - needed to cound records
$result = mysql_query("SELECT * FROM <tablename>");

//Get the count of records for a given result.
$record_count = mysql_num_rows($result);


Can't this be done in PHP by doing a
select count(*) as RecordCount from <tablename>

Wouldn't that be more efficient?

I don't know, I'm asking....

------------------
jim
Beauty is in the eye of the Beer Holder
< Brews and Cues >


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)

------------------
Wine me, dine me, 1000101 me

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.

------------------
Wine me, dine me, 1000101 me


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!

------------------
jim
Beauty is in the eye of the Beer Holder
< Brews and Cues >


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.

------------------
Wine me, dine me, 1000101 me


Powered by Ikonboard 3.1.4 © 2006 Ikonboard