September 16, 201213 yr Hi guys, hope you could help with the following code: /** Check users Tickets. */ public function checkUserTicket() { $sql = "SELECT * FROM tickets WHERE member_email = '$this->email'"; if ($sth = $this->db->query($sql)) { $count = $sth->fetchColumn(); if ($count >= 1) { while ($i1 = mysql_fetch_array($sth)){ echo 'Subject: $i1->ticket_subject - $i1->ticket_date - Status: $i1->ticket_responded - View Ticket.<br>'; } } else { echo '<h5 class="subheader centered">You currently do not have any existing tickets.</h5> <h1 class="centered"><a class="uibutton loading" title="Creating New Ticket" rel="1" onclick="location.href=newticket.php;">New Ticket</a></h1>'; } } } The top half is working correctly ($count) it picks up I have a support ticket under my email address and moves onto the while loop, which should display the all tickets I have. Issue I'm having is: Warning: mysql_fetch_array() expects parameter 1 to be resource, object given No idea what that means :/ URL http://linkthemup.com/ltusupport/ Login is: admin password Thank you! Much appreciated.
September 16, 201213 yr looks a little complicated to me. If it were me I'd just use a variable to say whether anything has been found by the SQL statement and once the loop is over if it's still 0 then echo your not found message.
September 16, 201213 yr do a var_dump on $sth and see what it outputs. You seem to be using an object and a standard function for databases simultaneously which shouldn't be done. I would imagine the values in $sth are an array of results, and that you just need to use a foreach for those
September 16, 201213 yr Author I have added the Var Dump. I have no idea what that means.. object(PDOStatement)#4 (1) { ["queryString"]=> string(64) "SELECT * FROM tickets WHERE member_email = 'dean@linkthemup.com'" } What would be my object? the $this? or the $sth? My standard function being mysql_fetch_array? Soory for being a noob, but like I said only PHP I know is what I got from Google haha! Thank you for trying to help me..
September 16, 201213 yr You cant use mysql library functions on PDOStatement objects. Its like trying to play a SNES game in an PS3. You need to use PDO to fetch the result while($i1 = $sth->fetch(PDO::FETCH_OBJ)) { echo 'Subject: $i1->ticket_subject - $i1->ticket_date - Status: $i1->ticket_responded - View Ticket.<br>'; } Edited September 16, 201213 yr by Jock
September 16, 201213 yr Author Hmm, OK..first time I've ever heard of PDO. Thanks Jock, I put that in: if ($count >= 1) { while($i1 = $sth->fetch(PDO::FETCH_OBJ)) {echo 'Subject: $i1->ticket_subject - $i1->ticket_date - Status: $i1->ticket_responded - View Ticket.<br>';} echo 'test'; } It has fixed the error ,but does not display anything else I'm afraid. Added the test echo to make sure the if was being called.
September 16, 201213 yr Was it being called? You should dump fetchAll and columnCount to see if the query is working correctly.
September 17, 201213 yr Author Thank you Jock, I put two var_dumps in: public function checkUserTicket() { $sql = "SELECT * FROM tickets WHERE member_email = '$this->email'"; if ($sth = $this->db->query($sql)) { $count = $sth->fetchColumn(); if ($count >= 1) { echo 'Var-Dump Count:<br>'; var_dump($count); while($i1 = $sth->fetch(PDO::FETCH_OBJ)) {echo 'Subject: $i1->ticket_subject - $i1->ticket_date - Status: $i1->ticket_responded - View Ticket.<br>';} echo '<br><br>Var-dump $sth->fetch(PDO::FETCH_OBJ):<br>'; var_dump($sth->fetch(PDO::FETCH_OBJ)); echo '<br><br>Picked up a Ticket, should var_dump $count and $sth->fetch.'; } Result is: Var-Dump Count: string(1) "1" Var-dump $sth->fetch(PDO::FETCH_OBJ): bool(false) So the $count does pick up the the one ticket. Its just the while loop :/
September 17, 201213 yr Ok so do this to see what is being pulled out. public function checkUserTicket() { $sql = "SELECT * FROM tickets WHERE member_email = '$this->email'"; if ($sth = $this->db->query($sql)) { print_r($sth->fetchAll()); } }
September 18, 201213 yr Author Hi Jock, thank you for your'e reply. Sorry to drag this out, but the code you offered has created an error: /** Check users Tickets. */ public function checkUserTicket() { $sql = "SELECT * FROM tickets WHERE member_email = '$this->email'"; if ($sth = $this->db->query($sql)){ print_r($sth->fetchAll()); } } Parse error: syntax error, unexpected T_STRING in /home/ltusupport/public_html/ltusupport/lib/core.php on line 109 Line 109 is the print_r I'm going to Google it, attempt to fix it myself as I don't wish to just leach from you. Thanks for your help so far. Edited September 18, 201213 yr by Bravo81
Create an account or sign in to comment