mysql-query

How to create sql insert query dynamically in mysql

I am creating an application where I am generating pins dynamically based on user's input and storing them into mySql database. $sql = "INSERT INTO tblpin ('pinId', 'ownerId', 'usedby', 'status') VALUES for($i=0;$i<$npin;$i++) { ('$pin[$i]','$ownerid', 'Free', '1'); } ;"; how can I do that...

Error during data INSERT in php

here my code- $sql = "INSERT INTO tblpin ('pinId', 'ownerId', 'usedby', 'status') VALUES "; for($i=0; $i<sizeof($pin); $i++) { if ($i>0) { $sql .= ", "; } $sql .= "('$pin[$i]', '$ownerid', 'Free', '1')"; } $sql .= ";"; echo $sql; mysql_query($sql); if(mysql_affected_rows() > 0) { echo "done"; } else { echo "Fail"...

Checking for reciprocal relationships in mysql. A trivial one table problem.

I have a mysql table that stores relationships. Items can be related to another item in one direction, or both items can be related to each other. I want to return all items related to my primary item - but I also want to check to see if the related item has a 'reverse relationship' to the current item and show this as a boolean |----...

Counting multiple rows in MySQL in one query

I currently have a table which stores a load of statistics such as views, downloads, purchases etc. for a multiple number of items. To get a single operation count on each item I can use the following query: SELECT *, COUNT(*) FROM stats WHERE operation = 'view' GROUP BY item_id This gives me all the items and a count of their views. ...

mySQL query: How to insert with UNION?

Hi everybody, I am kind of new to mySQL:s union functions, at least when doing inserts with them. I have gotten the following to work based upon a example found on the net: INSERT INTO tableOne(a, b) SELECT a, $var FROM tableOne WHERE b = $var2 UNION ALL SELECT $var,$var Ok, nothing strange about that. But what happens when I want ...

How to get count of another table in a left join

I have multiple tables post id Name 1 post-name1 2 post-name2 user id username 1 user1 2 user2 post_user post_id user_id 1 1 2 1 post_comments post_id comment_id 1 1 1 2 1 3 I am using a query like this: SELECT post.id, post....

Get insert statement query

Hi, Is there a way to get the insert statements for a table via some query in MySql? for ex: if the table name is Cards,which has 5 rows, i need to get the insert statements for that particular table. Thanks. ...

MySQLi - Should every statement be prepared?

I know its supposed to improve performance and clean strings, but lets say there are no variables? Might just be a SELECT COUNT( `column` ) AS count FROM `table` Should that be prepared? Is there any case that a SELECT statement should not be prepared? ...

Having problem in sql query execution

Hi there, I have a problem in sql query execution.I am using this sql query: $userid = 1; $sql = mysql_query(" SELECT ID, Nm, Address, date_format(DateOfBirth, '%d%M%Y') as DateOfBirth FROM PersonalDetails where UserMasterID = $userid ") or die (mysql_error()); The result appears as: You have an error in your SQL syntax; che...

Mysql eliminate user based on conditions

I asked this last week over the weekend and it got buried in the archives before anyone could answer. So forgive me if you've already seen this. I teach classes and want to be able to select those students who have taken one class, but not another class. I have two tables: lessons_slots which is the table for every class such as: ---...

mySQL: Deleting with JOIN ?

Hi everybody, This problem is giving me a real headache. I have two tables in my database that solves sorting of categories with the help of a design called "Closure table". The first table is called categories, and the other one is named categoriesPaths. Categories is simply a language-based table: id | name ---------------- 0 | C...

SQL select all items of an owner from an item-to-owner table

I have a table bike_to_owner. I would like to select current items owned by a specific user. Table structure is CREATE TABLE IF NOT EXISTS `bike_to_owner` ( `bike_id` int(10) unsigned NOT NULL, `user_id` int(10) unsigned NOT NULL, `last_change_date` date NOT NULL, PRIMARY KEY (`bike_id`,`user_id`,`last_change_date`) ) ENGINE=...

Hard mySQL query problem

Hey all, I am up against a big problem with trying to figure out how to go about this query I am needing. I need to only display the companies that the user has in their account and also only display the company if they are on the calendar for that day. My userinfo database is this (only showing needed table info): ID | UserID | i...

Weird problems with mysql outfile under FreeBSD

See my answer below. Leaving this up in case it helps someone else. Hi guys, What follows is a series of attempts to dump a query to an outfile on a new FreeBSD box that my site has moved to. The results are the same if I log in as me or if I log in as root. I hope the style isn't too annoying. I have my comments commented out arou...

How to copy a Database from one server to another server in PHP?

I have a Database in Local server named 'empData' and i want that all the data from that database to be copied to another Database, which is in the another server(web server) with the name 'empDataBackup'. I have tried this code mysql_query("INSERT INTO empData.backup_emp SELECT * FROM empData.emp"); But it did not work as both the dat...

MySQL: Getting results present in one time frame but not in another time frame.

I have generated a dataset that contains data spanning thirty days. Im trying to issolate new data elements that have appeared in the last 2 days but not in the previous 28 days before that. I run a PHP script that generates the test data. (PHP and MYSQL return the same time when tested) I run the following query against it. Results a...

Inserting data using PHP into mysql when it contains a '

Hi there, I am writing lots of info from an XML file into a database. Everything works fine until I come across a field with the ' in the description, that insertion fails with an error Error 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use ...

SQL - How to batch update given the results of a select

I've got a select statement that joins a couple of tables and grabs some information. I'd like to updated all of the records on one of those tables (found in the select) with information contained in the select. The select looks like this: SELECT account.id document.id FROM customer INNER JOIN account ON (custome...

mysql query and index

Does this query will be faster with a index on "t.type1" and "x.type1" or only index on "x.type1" is enought? SELECT t.id, x.id FROM t INNER JOIN x ON x.type1=t.type1 WHERE t.id=1 ...

sql question Select * From * where one=two order by

Hi, I need some help with a query. i want to select from a table, some values, but the values depend on the value of an other cell. after i select i need to sort them. Can i use ELECT column FROM table WHERE one=two ORDER BY ...? thanks, Sebastian ...