3 Replies - 182 Views - Last Post: 30 March 2019 - 07:15 AM Rate Topic: -----

#1 yvalson   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-March 19

will is code work?

Posted 30 March 2019 - 01:17 AM

So I'm currently using this code but I'm not sure if it will result in an infinity loop and since the server that I'm using is not mine I don't have a way to terminate the code if it does.

		$Voorstellingsnummer = 5;
		
		$randnum = rand(1, 50);
		$query = mysql_query("SELECT Stoelnummer FROM reserveringen WHERE Voorstellingsnummer = '$Voorstellingsnummer' AND Stoelnummer = '$randnum'", $db);
		i = 0;
		while(mysql_num_rows($query) == 1 OR i < 5){
			i++;
			$randnum = rand(1, 50);
			$query = mysql_query("SELECT Stoelnummer FROM reserveringen WHERE Voorstellingsnummer = '$Voorstellingsnummer' AND Stoelnummer = '$randnum'", $db);
		}



now for the explanation:
I first put a random number in a variable.
afterwards I run a query with that variable.
this the rows will then either equal 1 or 0 as it's selecting only one record and if it can't find that record it won't output anything
so for what the while loop does + it checks if there is a row for the query performed 2 lines ago. if there is it should pick a new number and do the process again until the query returns nothing (which indicates that the Stoel(seat in english) is free and can be occupied. Can anyone tell me if I made any errors.
I tried implementing a i increment system to fight a infinity loop because after 50 tries I wan't to stop for now. Will this code work or are there some things I need to fix first?

forgot thw $ sign in front of the i variable please excuse. the question still stands tho

This post has been edited by CTphpnwb: 30 March 2019 - 07:16 AM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: will is code work?

#2 yvalson   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-March 19

Re: will is code work?

Posted 30 March 2019 - 01:31 AM

I jumped the gun and ran the code and it works.
Was This Post Helpful? 0
  • +
  • -

#3 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 976
  • View blog
  • Posts: 3,849
  • Joined: 13-June 14

Re: will is code work?

Posted 30 March 2019 - 04:44 AM

You should always be running your code and seeing whether it works or not - that feedback loop will be much quicker than waiting for someone to tell you whether what you're doing makes sense. You'll also learn more that way.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3814
  • View blog
  • Posts: 13,870
  • Joined: 08-August 08

Re: will is code work?

Posted 30 March 2019 - 07:15 AM

Deprecated MySQL functions are simpler to use (not counting all the sanitizing you need to add) than PDO or MySQLi prepared statements, but far less secure. That's why they've been deprecated. If you're just learning PHP you should start with
http://www.dreaminco...duction-to-pdo/
or
http://php.net/manua...-statements.php
When dealing with old code that uses deprecated MySQL functions you should replace them if possible. If it's not possible, your experience with PDO or MySQLi will benefit you when dealing with MySQL functions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1