ajoo Posted January 8, 2015 Share Posted January 8, 2015 Hi all ! The following code has two similar queries. Query 1 and Query 2. While Query 1 works just fine, Query 2 simply fails persistently. I have not been able to figure out why? I have checked every single variable and field names but found no discrepancy of any sort or any mismatch of spelling with the fields in the database. I would be grateful if anybody can point out what is preventing the second query from returning a valid mysqli object as the first one does. Here is the code for the two. <?php $db_host = "localhost"; $db_user ="root"; $db_pass =""; $db_database ="allscores"; //////////////// CHECKING VARIABLE FILEDS IN A UPDATE QUERY ////////////////////////////////// //////////////// QUERY 1 ///////////////////////////////////////////////////////////////////// /* $db_database ="test"; $db_table ="users"; $RecNo = 1; $field1 = 'name'; $field2 = 'password'; $field3 = 'email'; $field4 = 'id'; $val1 = "Ajay"; $val2 = "howzatt"; $val3 = "me@mymail.com"; $con = mysqli_connect($db_host,$db_user,$db_pass,$db_database) or die('Unable to establish a DB connection'); $query = "UPDATE $db_table SET $field1 = ?, $field2 = ?, $field3 = ? WHERE $field4 = ? "; // session terminated by setting the sessionstatus as 1 $stmt = $con->prepare($query); var_dump($stmt); $stmt->bind_param('sssi',$val1,$val2,$val3,$RecNo); if($stmt->execute()) { $count = $stmt->affected_rows; echo $count; } //////////////// QUERY 1 END ///////////////////////////////////////////////////////////////////// */ //////////////// QUERY 2 ///////////////////////////////////////////////////////////////////// $con = mysqli_connect($db_host,$db_user,$db_pass,$db_database) or die('Unable to establish a DB connection'); $table = 'scores'; $date = date('Y-m-d H:i:s'); /* $prestr = "Mrt_M_"; $STATUS = $prestr."Status"; $S_R1 = $prestr."Scr_1"; $S_R2 = $prestr."Scr_2"; $PCT = $prestr."PPT"; $DPM = $prestr."DSP"; $TIMETAKEN = $prestr."TimeTaken"; */ $STATUS = "Mrt_M_Status"; $S_R1 = "Mrt_M_Scr_1"; $S_R2 = "Mrt_M_Scr_2"; $PPT = "Mrt_M_PPT"; $DSP = "Mrt_M_DSP"; $TIMETAKEN = "Mrt_M_TimeTaken"; $TimeOfLogin = $date; $no_of_logins = 10; $time_of_nLogin = $date; $m_scr_row1 = 5; $m_scr_row2 = 5; $m_ppt = 20; $m_dsp = 60; $m_time = 120; $date = $date; $RecNo = 24; $query = "UPDATE $table SET TimeOfLogin = ?, no_of_logins = ?, time_of_nLogin = ?, $S_R1 = ?, $S_R2 = ?, $PPT = ?, $DSP = ?, $TIMETAKEN = ?, $STATUS = '1', TimeOfLogout = ?, WHERE RecNo = ?"; $stmt = $con->prepare($query); var_dump($stmt); $stmt->bind_param('sisiiddssi',$TimeOfLogin,$no_of_logins,$time_of_nLogin,$m_scr_row1 $m_scr_row2,$m_ppt,$m_dsp,$m_time,$date,$RecNo); if($stmt->execute()) echo " DONE !"; ?> Thanks to all Quote Link to comment Share on other sites More sharing options...
CroNiX Posted January 8, 2015 Share Posted January 8, 2015 Why don't you test for an error and display it if it does so you can get some idea of what mysql is complaining about? See the example in the documentation. Quote Link to comment Share on other sites More sharing options...
ajoo Posted January 9, 2015 Author Share Posted January 9, 2015 HI CroNIX, Thanks for the reply. I think I found what the problem is anyways. Thanks ! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.