PHP - Pdo::execute Returning False
private function _authenticate() { // if there's already an auth error if ( $this->_checkForMessageType('auth') ) { $this->_addMessage('auth', 3); self::__destruct(); return false; } $stmt = $this->_dbh->prepare("SELECT shopID FROM api_users WHERE shopID = ? AND API_key = ? LIMIT 1"); var_dump($stmt->execute(array($this->_shopID, $this->_key))); echo $stmt->rowCount(); // authenticate key / shop id if ( !$stmt->rowCount() ) { $this->_addMessage('auth', 3); self::__destruct(); return false; } $this->_addMessage('auth', 4); } I am using PDO with MySQL driver and ATTR_EMULATE_PREPARES => true, however, when I run this code I get the output: Code: [Select] bool(false) 0{"auth":{"3":""}} Any ideas why PDOStatement::execute is returning false? I get no connection errors, no PDOExceptions, the db structure is correct, and there is valid data in the database. Any help appreciated, thanks. Similar TutorialsHi 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 I am trying to take all the information from $results and input it into a text file, sometimes we get duplicates in $results so I want to check the text file for the entry before writing it in so we don't get duplicates in the text file. However inarray keeps returning false, even when I echo out the two arrays and they match? Code: [Select] $myfile = "complete.txt"; foreach ($results['info'] as $data) { $output=$fields['firstname']."|".$record['lastname']."|".$record['address']."|".$record['number']."\n"; $handle = fopen($myfile, "r"); $contents = fread($handle, filesize($myfile)); fclose($handle); $list = array(); $list = explode("\n",$contents); $list = array_map("trim", $list); $current = $output; echo in_array($current,$list) ? $current.' exists' : $current.' does not exist'; if (in_array($current,$list)) { print "duplicate"; } else { if($file=fopen($myfile, "a")) { //open file for writing fwrite($file, $output); //write to file } } } I find it a bit buggy that strpos returns false instead of -1.
The equivalent C implementation returns -1:
int strpos(char s[], char t[]) { int i, j, k; for (i = 0; s[i] != '\0'; i++) { for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++) ; if (k > 0 && t[k] == '\0') return i; } return -1; }The problem with returning false is that false can also evaluate to 0 which can also happen to be the first index of a match. Therefore, you have a hard to find bug. I know that you can use the identity operator to check for falseness: ===. But wouldn't it be a better design for the function to just return -1? Does anyone see anything wrong with the SQL? This function keeps returning false. Thank you for your time. Code: [Select] function insert_survey1($survey1_anwers) { // extract order_details out as variables extract($survey1_anwers); $username = $_SESSION['valid_user']; $conn = db_connect(); // select user_id based on the entered data $query = "SELECT user_id FROM user WHERE gender = '".$gender."', birth_range = '".$birth_range."', degree_year = '".$degree_year."' username = '".$username."'"; $result = $conn->query($query); //if the resulting user_id exists (AND has the POST data just entered, since the query must match those to return a row) , if($result->num_rows>0) { //return the current row of the result set as an object and place it into user variable $user = $result->fetch_object(); //set variable for user_id to the fetched user object data representing the user_id int he db( because it was fetched from the sql select result) $user_id = $user->user_id; //update the fields since the user exists already. $query = "UPDATE user SET gender = '".$gender."', birth_range = '".$birth_range."', degree_year = '".$degree_year."' WHERE user_id = '".$user_id."'"; // $query = "UPDATE `alumni_survey`.`user` // SET `gender` = '".$gender."', // `birth_range` = '".$birth_range."', // `degree_year` = '".$degree_year."' // WHERE `user`.`user_id` = '".$user_id"'"; } else { //otherwise, if the user doesn't exist already insert this new data. $query = "INSERT INTO user (gender, birth_range, degree_year) VALUES('".$gender."','".$birth_range."','".$degree_year."') WHERE user_id = '".$user_id."'"; $result = $conn->query($query); if (!$result) { return false; } } return $user_id; } Hi I am new to php, I am trying to capture the url and place into a variable but I only get the 1st digit to show, I just cant see what I am doing wrong. Sorry to ask such a basic question but I just can't work it out, I have attached a screen shot of all me code, your help would be very very much appreciated. hi there, is there a difference between if($var == false) and if(false == $var) thankyou! Hey all, I'm trying to learn more about PHP but I keep having all these problems with Aptana Studio. I'll write out a bunch of code and it will flag my code as having an error, normally one of the curly brackets, even though I can't see anything wrong with the code at all. Sometimes if I delete it all and retype it in the error will go away even though I typed it out EXACTLY as I did the first time. Here is some code that it's flagging as having an error: Code: (php) [Select] <?php //Classes// class Dog{ public $hungry = 'Hell yeah!'; public function($food){ $this->hungry = 'not so much.'; } } //Program Variables// $dog = new Dog; //Program Code// echo $dog->hungry; ?> It is saying that the curly brace after "public function ($food)" is wrong as well as the closing curly brace on line 9 and the parentasis just before "$food". I can't see an error, no matter how long I look at it. What am I doing wrong? Just a silly question. I've been using 1 and 0 to tell if something is true or false, such as if a feature is enabled or not. I see that some use the words true and false. Is there a proper way or is either way correct? Thanks! Hello! Just started getting into PHP! I'd like to set a variable to false, and then check if it is set to false ( set to false = true ), but I'm confused about assignment operators vs. comparison operators, no pun intended. Code: [Select] = false or Code: [Select] == false ? Hi guys! I'm new to PHP and I'm using Drupal and there's a function that one block will be called if PHP scripts returns value TRUE. Now I want my "Search" to only show at ALL THE other pages but not index. With the code: if (Url::exists('http://www.example.com')) I get my Search function to return TRUE so it will be showed on index page only, but not on any other pages!! So I kinda need this on reverse - how do I get a TRUE value for something that excists? In this case it would be www.example.com, or if anyone else has any other ideas I would be very thankful! Hi, I am trying to verify if the given url exists or not, by using file_exists() function. It always returns 'FALSE' , according to my understanding it happening because the file to be checked on the given url is located in safe mode. Could anyone please suggest as to how this could be overcome, by similar function or by using alternative method. Regards Abhishek Madhani I am trying to unserialize this string: a:3:{s:3:\"zip\";s:5:\"55068\";s:4:\"city\";s:9:\"Rosemount\";s:5:\"state\";s:2:\"MN\";} but I unserialize is returning false. Why is it doing that? $info = unserialize($_COOKIE['zipcode']); var_dump($info); strange i want to update ProductDescription i get the parsed $pdescription it is echoed but it's regarded as empty by var_dump Code: [Select] <?php // example of how to use basic selector to retrieve HTML contents include('../simple_html_dom.php'); $links=array("http://www.example.com/apartments/198.htm"); foreach($links as $page) { $phtml = file_get_html($page); // find all td tags with attribite align=center foreach($phtml->find('span[id=lblCodiceAppartamento]') as $name){ echo $name->plaintext.'<br><br>'; } foreach($phtml->find('span[id=lblDescrizione]') as $pdescription){ echo $pdescription.'<br><br>'; } $con = mysql_connect("localhost","root","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("international", $con); $query=mysql_query("UPDATE products_NEW SET ProductDescription='$pdescription' WHERE ProductName = '$name->plaintext' AND SupplierID = '38'"); var_dump($query); //mysql_query("INSERT INTO apartments (name) //VALUES ('$name->plaintext')"); mysql_close($con); } I'm having trouble with a script and I just can't figure out what's wrong with it. The script is located at http://www.qlhosting.com/ham/check.php Here's the code for it <?php if (isset($_POST['submit'])) { $domain = $_POST['domain']; $password = md5($_POST['password']); include 'db.php'; mysql_query("SELECT * FROM apps WHERE domain='$domain' AND WHERE cpassmd5='$password' LIMIT 1"); $stat = $row['status']; } else { } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Quotaless Web Hosting | Check Status</title> <style type="text/css"> body,td,th { font-family: Arial, Helvetica, sans-serif; font-size: 12px; } </style> </head> <body> <h1>Check Application/Account Status</h1> <?php if ($stat="PENDING") { echo "<hr />"; echo "Your application is currently listed as PENDING. Our staff have not viewed your application yet. Please be patient, and watch your email for a response. Thank you!"; } elseif ($stat="NMI") { echo "<hr />"; echo "We need more information from you in order to take action on your application. Please check your e-mail inbox for a message from our staff specifically stating what we need. If you did not get this message, please post a message on our support forum. Thank you!"; } else { } ?> <hr /> <h2>To check the status of your application or account, login using the form below.</h2> <form id="check" name="check" method="post" action="<?php echo $PHP_SELF;?>"> <p>Domain: <input type="text" name="domain" id="domain" /> <br /> Password: <input type="password" name="password" id="password" /> <br /> <input type="submit" name="button" id="button" value="Check" /> </p> </form> <hr /> <p> </p> </body> </html> The part under if ($stat="PENDING") is performed upon page load, even when the if condition relating to it is false. I can't seem to figure out what exactly is wrong here. Please help me out. I would really appreciate the help. Thanks! Anthony How can I check if a function has returned false.. I know theirs the following way: if (!checkdate($month, $day, $year)) { //the function has returned false } But is their another way (beside using the ! operator) perhaps === FALSE (not sure if that'd work so asking)? Hi, I'm pretty new to PHP and I am working on a form that sends and email. I want to display a message if the mail can't be sent (i.e. if mail() returns false) and a different message if mail() returns true, except it is returning false even though the emails are sending. Basically I have used: $to = 'myemail@email.com'; $subject = "Subject Line Goes here"; $headers = "From: $email"; $message = 'My message'; $mailSent = mail($to, $subject, $message, $headers); In testing it I have used the following to determine what is going on: if (isset($mailSent)) { echo '$mailSent ='.$mailSent; } - echo's nothing if ($mailSent == FALSE) { echo '$mailSent = FALSE'; } - echo's '$mailSent = FALSE' if ($mailSent == TRUE) { echo '$mailSent = TRUE'; } - echo's nothing if (is_null($mailSent)) { echo '$mailSent = NULL'; } - echo's '$mailSent = NULL' echo '$mailSent ='.$mailSent; - echo's '$mailSent =' Any help would be greatly appreciated! Thanks! I'm working on a WordPress theme and I'm trying to build in a simple if statement which will check if the user has add his own logo into the images folder if he doesn't then the name of the blog will appear as normal text in place of the graphic logo. This is how it looks like: <?php $logo_dir = get_template_directory_uri() . "/images/logo.png"; if (file_exists($logo_dir)) { ?> <li><img src="<?php bloginfo ('template_directory'); ?>/images/logo.png" alt="<?php bloginfo('description'); ?>" /></li> <?php } else { ?> <li id='blog_name'><a href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?><font>*</font></a></li> <?php } ?> I've echo'd out $logo_dir, the URL is correct, but still for some reason it's seeing it as if there would no file exist. If I turn it around into !file_exists then the graphic logo WILL show up. So it's always seeing it as non-existent. Any ideas why this could be or am I using this function in a wrong fashion? As far as I've understood the PHP manual, this is the correct way of using it. i keep seeing people use this kind of code but i cant find any document or help web pages on it. im sure there is but i done know what to call it. i would really like to learn more about it for example Code: [Select] $lang = isset($_GET['lang']) ? (int)$_GET['lang'] : 1; or Code: [Select] return !empty($result_array) ? array_shift($result_array) : false; |