Browse Tag: php

Queue monitoring service

Like many other things in Serbia, Student Center of Novi Sad is in some ways old fashioned, and doesn’t like to change how things work. One of things is the way how students apply for the student accommodation and how they pay for it. It’s very simple: you go to the office, you wait and then you apply/pay for the accommodation. No online mess and trouble. Most of the time, waiting time is short, but as deadlines approach, waiting room becomes a bottleneck and many students have to wait, sometimes even for couple hours.

Luckily, the waiting room is equipped with a ticketing machine, so one you get the number, in theory, you can go and do other things you have to do. The problem is that while you are away, you do not know which number is processed in the office, and you can easily miss your turn.

So after some time spent thinking about how could I make things better for us, students, I decided to create queue monitoring service. It would not abolish the queue you saw in the video above, but it will give students a possibility to move away freely without missing their turn. So it led to some investigation in the beginning and system design after, and at the end to some coding ofc. On the diagram below is initial system design, all together with SMS service which was never implemented due some bureaucratic reasons.

System desing

Excluding that, everything else fit well and is working well since it was installed. Ok, maybe not always; sometimes it does happen that local computer stops working, and that service stays outdated for couple hours/days. Right now, the idea is to change ‘local computer’ on this diagram with Raspberry PI, and leave it somewhere in the dark forever alone to do its mission.

pi-forever-alone

Anyway, you can download an app for Android here, or check status online here, but if you are not a student in Novi Sad, then this information might not be useful to you. The thing that might be useful to you is on my github here, and it’s a source code for the local service and Android app. Maybe you can use it and help someone else not to wait in the line for too long 😉

Using prepared statement in PHP

So this is the result of using prepared statements in PHP on not so comfort way. Each time when I had to perform some query, I had something like this in my code

<?php
	$stmt = $con->prepare("SELECT * FROM table WHERE id = ?");
	$stmt->bind_param("i", $id);
	$stmt->execute();
	$results = $stmt->get_result();
?>

First problem occurred when I wanted to use this code on server where MySQL native driver for PHP was not enabled. When driver is not enabled, using get_result() function is not possible. It was free server, something I was doing just for fun, so it was not possible either to contact support or to install it by myself. Then I had to change the way I get results. And I had to do that on every single line in a code where I had query. A lot of work.

Second problem was a lot of repeating code which I don’t like. And I wanted to catch errors (which I didn’t do from the beginning). Again, I had to change my code on every place where I had query executing.

So I decided to create function, which will do executing queries instead of me.  I Googled a little bit, and found this.

That allowed me to create function like this:

<?php
	function prepareAndExecuteQuery($con, $query, $bindString, $bindArgs) {
		if (!is_array($bindArgs)) 
			exit ("prepareAndExecuteQuery - bindArgs not array");

		$n = strlen($bindString); 
		if ($n != count($bindArgs))
			exit("prepareAndExecuteQuery - bindString params count not equal to the bindArgs count");

		$a_params = array();
		$a_params[] = & $bindString;
		for($i = 0; $i < $n; $i++) 
			$a_params[] = & $bindArgs[$i];

		$stmt = $con->prepare($query);

		call_user_func_array(array($stmt, 'bind_param'), $a_params);

		if (!$stmt)
			exit("prepareAndExecuteQuery - Binding parameters failed");

		if(!$stmt->execute())
			exit("prepareAndExecuteQuery - Execute failed - " . $stmt->error);

		$result = $stmt->get_result();
		$stmt->close();
		if (!$result) 
			exit("prepareAndExecuteQuery - No results - " . $stmt->error);

		return $result;
	}
?>

Now when I have to execute some query I do it this way:

<?php
	$sql = "SELECT * FROM table WHERE id = ?";
	$result = prepareAndExecuteQuery($con, $sql,"i"', array($id));
?>

Less code and much more flexibility. If one day I decide to change something in execution of queries, I’ll have to do it just at one place.

I hope it will be helpful to someone else too 😉