functions from mysql to PDO

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

functions from mysql to PDO

Post by nite4000 »

I am trying to convert from MySQL to PDO and when I add the trigger on the page to call the function that page is 500 server error. What am I doing wrong with this

here is the orginal code

Code: Select all


	function generateTicketId ()
	{
		$ret = 0;
		
		// Start a loop that will go on forever (until we exit it with `return` or `break;`)
		while ( true )
		{
			// Generate random number from 11111 to 99999
			$ret = rand ( 11111, 99999 );
			
			// See if this number is being used
			$result = mysql_query ( "SELECT count(*) FROM members WHERE acct_num='$ret' LIMIT 1") or die ( mysql_error () );
			
			// Get the result
			$row = mysql_fetch_row ( $result );
			
			// If the query returned 0 records with this ticket Id, then it doesn't exist; return with the random number
			if ( $row[0] == 0 || !is_array ( $row ) )
			{
				return $ret;
			}
		
		}
		
	}

//use this to call the function
$ticketId = generateTicketId();


Here is what I have now

Code: Select all



	function generateTicketId ()
	{
		$ret = 0;
		
		// Start a loop that will go on forever (until we exit it with `return` or `break;`)
		while ( true )
		{
			// Generate random number from 11111 to 99999
			$ret = rand ( 11111, 99999 );
			
			// See if this number is being used
			$result = $conn->query('SELECT count(*) FROM users WHERE acct_id=$ret LIMIT 1');
			$row = $stmt->fetchAll(PDO::FETCH_ASSOC);


		//$result = mysql_query ( "SELECT count(*) FROM users WHERE acct_id='$ret' LIMIT 1") or die ( mysql_error () );
			
			// Get the result
			$row = mysql_fetch_row ( $result );
			
			// If the query returned 0 records with this ticket Id, then it doesn't exist; return with the random number
			if ( $row[0] == 0 || !is_array ( $row ) )
			{
				return $ret;
			}
		
		}
		
	}



I know I have a lot to learn for pdo but any help and guiding would be great
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: functions from mysql to PDO

Post by Celauran »

Why not just use a UUID?
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: functions from mysql to PDO

Post by nite4000 »

I have never heard of that
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: functions from mysql to PDO

Post by Celauran »

Wiki: UUID
Here's a good UUID generator library: https://packagist.org/packages/rhumsaa/uuid
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: functions from mysql to PDO

Post by nite4000 »

I am afraid I am still lost I understand what it is now but how to use it I am unsure.
Post Reply