I cant get my code to bind the params to the Mysql Query, what am i doing wrong here?  ERROR     Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens' in /Users/warren/Sites/Frame Work/Modules/User Login/classes/db.php:34 Stack trace: #0 /Users/warren/Sites/Frame Work/Modules/User Login/classes/db.php(34): PDOStatement->execute() #1 /Users/warren/Sites/Frame Work/Modules/User Login/classes/db.php(48): dbConnect->get_contents() #2 {main} thrown in /Users/warren/Sites/Frame Work/Modules/User Login/classes/db.php on line 34   CODE  &lt;?php  include '../../../../config.php';  $dbUser = $config['username']; $dbPass = $config['password'];  class dbConnect {      public function connect($dbUser,$dbPass) {         $this-&gt;dbUser = $dbUser;         $this-&gt;dbPass = $dbPass;         try {             $this-&gt;connect = new PDO('mysql:hostname=localhost;dbname=totalrisk', $this-&gt;dbUser, $this-&gt;dbPass);             $this-&gt;connect-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);             return $this-&gt;connect;         } catch (Exception $e) {             echo 'ERROR: ' . $e-&gt;getMessage();         }     }      public function select_table($table) {         $this-&gt;select_table = $table;         echo $this-&gt;select_table;     }      public function select_col($col) {         $this-&gt;select_col = $col;         echo $this-&gt;select_col;     }      public function get_contents() {         $stmt = $this-&gt;connect-&gt;prepare('SELECT * FROM :table');         $stmt-&gt;bindParam(':table', $this-&gt;table , PDO::PARAM_STR);              $stmt-&gt;execute();               $result = $stmt-&gt;fetchAll();               while($row = $stmt-&gt;fetch()) {                 print_r($row);              }     } }  $conn = new dbConnect(); $conn-&gt;connect($dbUser,$dbPass); $conn-&gt;select_table('users'); $conn-&gt;select_col('site_name'); $conn-&gt;get_contents(); ?&gt;
213
Why can't I pass the table name to a prepared PDO statement?  $stmt = $dbh-&gt;prepare('SELECT * FROM :table WHERE 1'); if ($stmt-&gt;execute(array(':table' =&gt; 'users'))) {     var_dump($stmt-&gt;fetchAll()); }   Is there another safe way to insert a table name into a SQL query? With safe, I mean that I don't want to do  $sql = "SELECT * FROM $table WHERE 1"
86157
I was trying to add the column of MYSQL Timestamp to the Database Table, the Query seems to be working correctly in mysql console.But when I execute the same in db_query() the following error is thrown.     Failed: PDOException: SQLSTATE[42000]: Syntax error or access   violation: 1067 Invalid default value for 'last_update_date': ALTER   TABLE stores ADD last_update_date TIMESTAMP NOT NULL DEFAULT   '0000-00-00 00:00:00';; Array ( ) in store_update_7100() (line 254 of   /var/www/slocamo_app/sites/all/modules/store/store.install).   Is the Query.  ALTER TABLE  `stores` ADD  `last_update_date` TIMESTAMP NOT NULL DEFAULT  '0000-00-00 00:00:00');
86789
I don't have this kind of ID in my table, still MySQL is giving me a result.  This same result is giving when i place in my php code.
37422
I'm getting this error every few days. I won't see the error for a few days then I'll get a flurry of 20 or so all with in a minute or so.  I've been very thorough going throw my code so that I'm using this basic setup for my DB access.  try {   myConnection.Open();   mySqlDataAdapter.Fill(myDataTable);   myConnection.Close(); } Catch (Exception err) {   if (myConnection.State != ConnectionState.Closed) myConnection.Close();   throw err; }   The way I understand it this should execute my queries and immediately release the connection back to the pool but if something goes wrong with the query then I catch the excpetion close my connection then throw the error up, which eventually gets trapped at the application level and logs and emails me the error.  Even using this throughout my code I'm still running across the issue. What can I do to diagnose the root cause of the issue?
55771
Anyone know how to combine PHP prepared statements with LIKE? i.e.   "SELECT * FROM table WHERE name LIKE %?%";
44862
I have a problem binding booleans using mysqli_stmt::bind_param in PHP5. The SQL query is the following: insert into `nvp_notes` (subject,messageid,receivedate,read) values (?,?,?,?)  Where 'read' is a tinyint, either 0 or 1, as I've had issues with bit using mysqli. So the types that I list in bind_param are: $stmt-&gt;bind_param('sdsd', ...);  I've also tried 'sdsb' and 'sdss' but nothing seems to work, and I always get the message:  Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement  When I remove the read field in the statement everything works fine. I've run out of ideas with this one. Surely bind_param works with booleans?
94051
I want to do something like this:    create table app_users (     app_user_id smallint(6) not null auto_increment primary key,     api_key     char(36) not null default uuid() );    However this results in a error, is there a way to call a function for a default value in mysql?  thanks.
721
I'm slowly moving all of my LAMP websites from mysql_ functions to PDO functions and I've hit my first brick wall.  I don't know how to loop through results with a parameter.  I am fine with the following:  foreach ($database-&gt;query("SELECT * FROM widgets") as $results) {    echo $results["widget_name"]; }   However if I want to do something like this:  foreach ($database-&gt;query("SELECT * FROM widgets WHERE something='something else'") as $results) {    echo $results["widget_name"]; }   Obviously the 'something else' will be dynamic.
99906
One of the responses to  suggested that I should make sure my database can handle UTF-8 characters correctly. How I can do this with MySQL?
120263
I'm trying to implement optional parameters in a stored procedure that I have but I am running into a problem. Here's a simplified query to illustrate the issue:  SET ANSI_NULLS OFF  DECLARE @MiddleName VARCHAR(20); SET @MiddleName = NULL;  SELECT * FROM [Customer] WHERE [LastName] = 'Torres' AND [MiddleName] = COALESCE(@MiddleName, [MiddleName])   When I run this query I need to get one row back because one Torres has NULL in the [MiddleName] column. But the query returns zero rows. Using IFNULL() produces the same result. From researching COALESCE, I was under the impression that NULL would be returned if all expressions are NULL. As I am not a SQL expert I assume that I am missing something, but what is it.....  Thanks in advance for any help.
15587
I have a query to insert a row into a table, which has a field called ID, which is populated using an AUTO_INCREMENT on the column. I need to get this value for the next bit of functionality, but when I run the following, it always returns 0 even though the actual value is not 0:  MySqlCommand comm = connect.CreateCommand(); comm.CommandText = insertInvoice; comm.CommandText += "\'" + invoiceDate.ToString("yyyy:MM:dd hh:mm:ss") + "\', " + bookFee + ", " + adminFee + ", " + totalFee + ", " + customerID +  ")"; int id = Convert.ToInt32(comm.ExecuteScalar());   According to my understanding, this should return the ID column, but it just returns 0 every time. Any ideas?  EDIT:  When I run:  "INSERT INTO INVOICE (INVOICE_DATE, BOOK_FEE, ADMIN_FEE, TOTAL_FEE, CUSTOMER_ID) VALUES ('2009:01:01 10:21:12', 50, 7, 57, 2134);last_insert_id();"   I get:  {"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'last_insert_id()' at line 1"}
31406
I am planning to replace repeatedly executed Statement objects with PreparedStatement objects to improve performance. I am using arguments like the MySQL function now(), and string variables.   Most of the PreparedStatement queries I have seen contained constant values (like 10, and strings like "New York") as arguments used for the ? in the queries. How would I go about using functions like now(), and variables as arguments? Is it necessary to use the ?s in the queries instead of actual values? I am quite confounded.
59128
I am writing a stored procedure where I have an input parameter called my_size that is an INTEGER. I want to be able to use it in a LIMIT clause in a SELECT statement. Apparently this is not supported, is there a way to work around this?  # I want something like: SELECT * FROM some_table LIMIT my_size;  # Instead of hardcoding a permanent limit: SELECT * FROM some_table LIMIT 100;
72534
I am receiving this warning when I try to run a database query:     Warning: Missing argument 2 for wpdb::prepare()   The offending query is:  $result = $wpdb-&gt;get_var(     $wpdb-&gt;prepare(         "SELECT DISTINCT meta_value FROM $metatable         WHERE meta_key LIKE '%matchme%'         AND meta_value IS NOT NULL         AND meta_value &lt;&gt; ''"      ) );   Is there a way to remove the error by modifying the above query?
114524
How do I generate a range of consecutive numbers (one per line) from a MySQL query so that I can insert them into a table?  For example:  nr 1 2 3 4 5   I would like to use only MySQL for this (not PHP or other languages).
26106
I have webservice which is passed an array of ints. I'd like to do the select statement as follows but keep getting errors. Do I need to change the array to a string?  [WebMethod] public MiniEvent[] getAdminEvents(int buildingID, DateTime startDate) {         command.CommandText = @"SELECT id,                             startDateTime, endDateTime From                             tb_bookings WHERE buildingID IN                             (@buildingIDs) AND startDateTime &lt;=                             @fromDate";      SqlParameter buildID = new SqlParameter("@buildingIDs", buildingIDs); }
94905
How do you build and use dynamic sql in a MySQL stored procedure?