Search Members Help

» Welcome Guest
[ Log In :: Register ]

 

[ Track This Topic :: Email This Topic :: Print this topic ]

reply to topic new topic new poll
Topic: Any PHP guru's????< Next Oldest | Next Newest >
 Post Number: 1
jim Search for posts by this member.
Asshole
Avatar



Group: Members
Posts: 1208
Joined: May 2000
PostIcon Posted on: Jan. 22 2001,11:52  Skip to the next post in this topic. Ignore posts   QUOTE

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.

Offline
Top of Page Profile Contact Info WEB 
 Post Number: 2
whtdrgn_2 Search for posts by this member.
FNG
Avatar



Group: Members
Posts: 155
Joined: Dec. 2000
PostIcon Posted on: Jan. 22 2001,16:19 Skip to the previous post in this topic. Skip to the next post in this topic. Ignore posts   QUOTE

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

Offline
Top of Page Profile Contact Info WEB 
 Post Number: 3
whtdrgn_2 Search for posts by this member.
FNG
Avatar



Group: Members
Posts: 155
Joined: Dec. 2000
PostIcon Posted on: Jan. 23 2001,05:33 Skip to the previous post in this topic. Skip to the next post in this topic. Ignore posts   QUOTE

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

Offline
Top of Page Profile Contact Info WEB 
 Post Number: 4
jim Search for posts by this member.
Asshole
Avatar



Group: Members
Posts: 1208
Joined: May 2000
PostIcon Posted on: Jan. 23 2001,05:37 Skip to the previous post in this topic. Skip to the next post in this topic. Ignore posts   QUOTE

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

Offline
Top of Page Profile Contact Info WEB 
 Post Number: 5
whtdrgn_2 Search for posts by this member.
FNG
Avatar



Group: Members
Posts: 155
Joined: Dec. 2000
PostIcon Posted on: Jan. 23 2001,05:46 Skip to the previous post in this topic. Skip to the next post in this topic. Ignore posts   QUOTE

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

Offline
Top of Page Profile Contact Info WEB 
 Post Number: 6
whtdrgn_2 Search for posts by this member.
FNG
Avatar



Group: Members
Posts: 155
Joined: Dec. 2000
PostIcon Posted on: Jan. 23 2001,13:56 Skip to the previous post in this topic. Skip to the next post in this topic. Ignore posts   QUOTE

Well, how did that work for you? What version of PHP are you running.

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

Offline
Top of Page Profile Contact Info WEB 
 Post Number: 7
jim Search for posts by this member.
Asshole
Avatar



Group: Members
Posts: 1208
Joined: May 2000
PostIcon Posted on: Jan. 23 2001,18:10 Skip to the previous post in this topic. Skip to the next post in this topic. Ignore posts   QUOTE

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

Offline
Top of Page Profile Contact Info WEB 
 Post Number: 8
whtdrgn_2 Search for posts by this member.
FNG
Avatar



Group: Members
Posts: 155
Joined: Dec. 2000
PostIcon Posted on: Jan. 25 2001,10:02 Skip to the previous post in this topic.  Ignore posts   QUOTE

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

Offline
Top of Page Profile Contact Info WEB 
7 replies since Jan. 22 2001,11:52 < Next Oldest | Next Newest >

[ Track This Topic :: Email This Topic :: Print this topic ]


 
reply to topic new topic new poll

» Quick Reply Any PHP guru's????
iB Code Buttons
You are posting as:

Do you wish to enable your signature for this post?
Do you wish to enable emoticons for this post?
Track this topic
View All Emoticons
View iB Code