Jump to content

[SOLVED] PHP mistake? or SQL? (Go figure)


Twister1004

Recommended Posts

Hey everyone! How are you doing? Well, I'm glad you ok, or doing ok. But I have an Issue I would like to ask of your help.

 

 

Objective: Figure out why this script isn't executing correctly.

 

Problem: Well, like earlier, I'm making a ticketing system, but I can't seem where I went wrong.

 

Issue: It will show the previous tickets EXCEPT the last ticket created by the user.

 

PHP Partial with the problem.

$gettickets = mysql_query("SELECT * FROM `cype_tickets` WHERE `name` = '{$_SESSION['pname']}' ORDER BY `ticketid` DESC")or die(mysql_error());
		$getnumber = mysql_num_rows($gettickets);
		$opentick = mysql_fetch_array($gettickets);
		$NumberTicket = 0;
		while($tickets = mysql_fetch_array($gettickets)){
			echo "
				<tr>
					<th>
						" . ++$NumberTicket . "
					</th>
					<td>
						<a href =\"?cype=ucp&page=ticket&a=$tickets[ticketid]&ticket=Yes\">
							" . $tickets['title'] . "
						</a>
					</td>
					<td>
						" . $tickets['date'] . "
					</td>
					<th>
						" . $tickets['status'] . "
					</th>
				</tr>
			";
		}
	echo "
	</table>";
	//Check to see if the user has more than five tickets open.
	if($getnumber >= 5 && $opentick['status'] == "Open"){
		echo "<b><center>Please wait until one of your tickets is solved before creating another one.</center></b>";
	} else{
	echo "
		<center>
			<input type=\"button\" value=\"Create Ticket\" onclick='parent.window.location = \"?cype=ucp&page=ticket&ticket=create\"' />
		</center>
		";
	}

 

I've been on this for about 1 hour and I've not figure it out, with multiple testing. It is more than likely a stupid mistake, but I can't seem to find it.

 

 

Thank you for your help and for reading this.

Link to comment
Share on other sites

i believe you have ran into the mysql resource "pointer" problem. where you have a result set, $getticket, and you then use mysql_fetch_Array, which grabs 1 array and moves the "pointer" across 1 row (or down 1 row).

 

so, you alwas miss that "top" result. you need to use this function if you doing stuff like that twice with one resource:

 

mysql_Data_Seek();

http://uk.php.net/manual/en/function.mysql-data-seek.php

 

use it after your first fetch function, this will reset the "pointer caret" to the first row again.

Link to comment
Share on other sites

Well, I've never had a problem when I've used it like this, but I will see if that will fix it. I'm still open to suggestions though =).

 

Thank you uniflare.

 

Edit: I do not need this function, uniflare. From what I've read this will tell you where your pointer is, or tell it to pick that row.

Link to comment
Share on other sites

Edit: I do not need this function, uniflare. From what I've read this will tell you where your pointer is.

 

Use it if you want, its up to you, it physically moves the data pointer, this is a quote from the page (u dint read this part obviously)

mysql_data_seek() moves the internal row pointer of the MySQL result associated with the specified result identifier to point to the specified row number. The next call to a MySQL fetch function, such as mysql_fetch_assoc(), would return that row.

 

I know what it does and why it does it. If you dont want to try my ideas then dont, but dont expect help if you wont listen.

 

Did it work? or are you not going to try it?

 

Link to comment
Share on other sites

Ok, you dont quite understand the way php gets results from mysql.

 

PHP stores all of its rows returned by a query to an array, eg:

 

Array(
   [0] => "row1",
   [1] => "row2",
   [2] => "row3"
);

 

Now, each time you call mysql_fetch_array(), you pick the "Next" array item, or row. (which is why you need a WHILE loop to get them all easily).

 

eg;

 

mysql_Fetch_Array() will give you array[0], or row 1.

then next, mysql_Fetch_Array() will give you array[1], or row 2.

 

if you were to use mysql_Data_seek($result,0);

 

the next mysql_fetch_array would give you array[0], or row 1.

then next, mysql_Fetch_Array() will give you array[1], or row 2.

...

 

i hope this clears things up, its better to try things than to assume it wont work, cuz u never know. especially if you dont know.

Link to comment
Share on other sites

In your code:

 

This is giving you only the first row returned. Then moving the internal data pointer to row 2.

	

$opentick = mysql_fetch_array($gettickets);

 

so this part will "start" on row 2, so it will start from the second result as the data pointer is on the 2nd row at this time.

	

while($tickets = mysql_fetch_array($gettickets)){

 

PS: if i sound aggrevated, its only because u dint try it, u just shot it down. i know it will work! lol.

Link to comment
Share on other sites

Ok, I just tested it, and I can't really move the pointer where it needs to go due to other people are also using this as well. Meaning that the pointer will select the incorrect place to check for the tickets, and check if they have made more than five; which will always be true.

Link to comment
Share on other sites

I dont get how "other people" can affect your unique internal data pointer unique to that variable in that script execution for the scripts executions query.

 

 

Basically, to me uou are missing the "openticket" row on the list of rows you want to display. to display this do this:

Reset the internal data pointer between mysql_fetch functions with mysql_Data_Seek.

 

if you have code, give us; once youve tried my idea, and have code to support that youve tried my idea, i personally will move onto another idea. Until you try my idea, and show youve tried my idea, im not going to respond.

Link to comment
Share on other sites

Ahh, I see your point wise one! I also see why I seemed for it to not work right. The SQL was partly messed up from where I altered it.

 

Although, I didn't think I could reset the pointer until your last post sparked my idea to reset it to 0.

 

Thank you very much uniflare. <3.

 

Fixed script.

 

$gettickets = mysql_query("SELECT * FROM `cype_tickets` WHERE `name` = '{$_SESSION['pname']}' AND `status` = 'Open' ORDER BY `ticketid` DESC")or die(mysql_error());
		$getnumber = mysql_num_rows($gettickets);
		$NumberTicket = 0;
		while($tickets = mysql_fetch_array($gettickets)){
			echo "
				<tr>
					<th>
						" . ++$NumberTicket . "
					</th>
					<td>
						<a href =\"?cype=ucp&page=ticket&a=$tickets[ticketid]&ticket=Yes\">
							" . $tickets['title'] . "
						</a>
					</td>
					<td>
						" . $tickets['date'] . "
					</td>
					<th>
						" . $tickets['status'] . "
					</th>
				</tr>
			";
		}
	echo "
	</table>";
	//Check to see if the user has more than five tickets open.
	$pointerReset = mysql_data_seek($gettickets, 0);
	$opentick = mysql_fetch_array($gettickets);
	if($getnumber >= 5 && $opentick['status'] == "Open"){
		echo "<b><center>Please wait until one of your tickets is solved before creating another one.</center></b>";
	} else{
	echo "
		<center>
			<input type=\"button\" value=\"Create Ticket\" onclick='parent.window.location = \"?cype=ucp&page=ticket&ticket=create\"' />
		</center>
		";
	}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.