Page 1 of 1

functions from mysql to PDO

Posted: Wed Apr 02, 2014 7:08 am
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

Re: functions from mysql to PDO

Posted: Wed Apr 02, 2014 8:14 am
by Celauran
Why not just use a UUID?

Re: functions from mysql to PDO

Posted: Wed Apr 02, 2014 8:22 am
by nite4000
I have never heard of that

Re: functions from mysql to PDO

Posted: Wed Apr 02, 2014 8:25 am
by Celauran
Wiki: UUID
Here's a good UUID generator library: https://packagist.org/packages/rhumsaa/uuid

Re: functions from mysql to PDO

Posted: Wed Apr 02, 2014 8:28 am
by nite4000
I am afraid I am still lost I understand what it is now but how to use it I am unsure.