MySQL concat() not working properly - php

In my database table, I have a field where I am storing userid's as comma separated values like 1,2,3 etc. Each user id's are added to the field through updation at a certain point. So if field contain value 1, then I want to append new userid as ,2 on next updation so that all values will be separated by comma.
I am using the following mysql for it:
"UPDATE videorating
SET total_votes='".$added."',
total_value='".$sum."',
userid=CONCAT(userid,',$userid'),
used_ips='".$insertip."'
WHERE videoid='$id_sent'";
Here $userid will contain the userid. But this is not working properly. The field is not getting updated. What is wrong with this query.
Can anyone help me to fix this. Thanks in advance.

I am not sure if your userid column really is varchar or not? If it is varchar (or some other text based) then your code should work. This is the same as your but with too many quotes:
UPDATE videorating
SET total_votes='$added',
total_value='$sum',
userid=CONCAT(userid,',$userid'),
used_ips='$insertip'
WHERE videoid='$id_sent'";
If it in fact is an int you cannot do it like this and you need to rethink what you want to archieve.

Related

automatically insert random value in column

I want to generate random number of 6 digit in mysql.
For example,
I have a table named as DATA and columns are job_name and job_id.
So, when user insert value in job_name and the value of job_id which would be random and unique number which will be stored in database automatically.
I have been searched for this but can not get anything out of it. So, Please specify your answer in brief.
Give job_id the primary key and use Auto Increment in your database.
This way you just have to insert job_name and it will auto_increment the name by itself. Auto Increment
P.S. this is not random, but there shouldn't really be a reason for it to be random? otherwise you'd have to make a script that keeps making random numbers and comparing them to the database until one doesn't exist yet.
If you really want it to be random check this post
I really suggest that you don't do this, there might also be a day where you run out of ids and it will get stuck in an endless loop if you limit it to 6 characters.
try this query
INSERT INTO `DATA`(`job_id`, `job_name`) VALUES (ROUND((RAND() * (999999-100000))+100000),'php developer')
Use 2 fields in your table. One is your AUTO_INCREMENT job_id and another new field: job_unique_id.
You can insert unique random values in that column using mysql UUID function. From your tags it seems like you're using codeigniter. In codeigniter use the following code for generating unique tokens:
$data = array(
'name' => 'full name'
);
$this->db->set('job_unique_id', 'UUID()', FALSE);
$this->db->insert('my_table',$data);
Check this link for more info: http://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_uuid

How to run a MYSQL INSERT query that doesn't replace duplicates based on multiple columns where auto-increment id is present?

I have searched around for the answer to this and have come across IGNORE and WHERE NOT EXISTS however they both seem to do slightly differently than what I am trying to accomplish. My MYSQL table appears as follows:
id(auto increment INT), charactername(VARCHAR), characterregion(VARCHAR), characterrealm(VARCHAR)
My data is retrieved from a website that returns all of the characters of a game, even the ones I already have in my database.
I wish to keep a list of all of the characters but no duplicates. My issue seems to be that I need to compare the name, realm and region of the character before deciding if it is a duplicate as the same name can appear on different region/realm combinations.
I have tried comparing all of the values of the 3 non-auto incrementing columns as follows:
REPLACE INTO characters (charactername, characterregion, characterrealm) VALUES ('Peter','AMERICA','Realm1') WHERE NOT EXISTS(SELECT * FROM characters WHERE charactername='Peter' AND characterregion='AMERICA' AND characterrealm='Realm1')
This however returns a MYSQL error as the syntax is incorrect. I have also tried INSERT IGNORE INTO... however that only seems to be checking the id value. I don't believe I need to check the id at all as I have it set to auto increment.
Any help would be greatly appreciated, I am using PHP for the other parts of this if it helps. Thanks again.
Just add a composite index on all 3 columns.
alter ignore table mytable add unique index(charactername, characterregion, characterrealm);
then do
INSERT INTO characters (charactername, characterregion, characterrealm)
VALUES ('Peter','AMERICA','Realm1')
ON DUPLICATE KEY
UPDATE
charactername='Peter1',characterregion='AMERICA1',characterrealm='Realm11'
The update will trigger only if all 3 columns are identical.Or you could do just an INSERT and it will fail if all 3 columns are identical.

How to insert strings with commas in MySQL?

I have a MySQL field called user_tags, and I want to insert a string with commas like this:
string1
string1,string2
string1,string2,string3,......
How do I update the field dynamically using a query?
Please read about Mysql set DataType http://dev.mysql.com/tech-resources/articles/mysql-set-datatype.html
But this is the concept:
UPDATE set_test SET myset = CONCAT(myset,",Travel")
WHERE rowid = 3;
No you don't, that violates first normal form which says "Every row-and-column intersection contains exactly one value from the applicable domain". Also, it's not the right way of doing it; it's not how SQL databases are designed to work.
MySQL's sets don't solve it either (they also violate 1NF) because the set permitted values are effectively fixed (can only be changed by ALTER TABLE).
What you really want, is another table associating tags with users. It's dead easy.
Once you redesign your table this way, you can add a new tag with a simple INSERT.

PHP/MySQL how can I select a value from a MySQL database?

I was wondering how can I select a value from a database that a user just entered it into and then add it to another mysql table all in the same script before the script is finished running.
You're probably looking for an insert ... select statement.
If you're talking about adding a value that a user just entered into a form, to something, and then putting that into the database, you should do the addition while in PHP. There's no point in going to the database after you've just inserted the value for this purpose.
If I'm misunderstanding something, please elaborate your question and let us know WHY you would want to figure out a just-inserted database value and do an operation on it, rather than trying to do it before you insert in the first place.
Also, if it's a fairly simple modification consider using an UPDATE statement, not a select --> insert.
Like nash said, you perform a select.
But to get the data from the row that the user just entered, you'll need:
mysql_insert_id()
Which grabs the last ID inserted (this is assuming you have an increment id column)
So assuming just entered his first and last name in a form, you'd insert his first and last name in the database(which i assume you know how since the title of this question is "SELECT a value from MySQL database"), you can get what he just entered by:
$last_id = mysql_insert_id();
If there are no rows on that table yet, then this will return 1. $last_id is now 1 (one).
To select:
SELECT * FROM users WHERE userID = "$last_id"
this will grab what the user just inserted....however, this seems pointless as you can use the variables from the form he just filled
enter code here
In the PHP MySQL module, you normally perform a mysql_select_db() to switch database.
You can insert your data into tables in different databases by switching between them with that function.
However, you can insert data into any table of any database (which the user has access to) by prefixing the database name to the table like so:
INSERT INTO test_db.test_table (`column1`,`column2`) VALUES ('abc',123);
You can use that also to insert data from one table into another using:
INSERT INTO `db1`.`myTable` (`column1`,`column2`) SELECT `column1`,`column2` FROM `db2`.`myTable` WHERE `id`= 5
The WHERE id part should obviously match the id of a row in db2.myTable
If you use doctrine you have the inserted data in the object representing the table and in addition you have primary key assigned for the record inside the object.
Con is doctrine is huge database abstraction layer, so if your application is not big doctrine is hammer for mosquito.
what is the structure of your database? The names of your tables, columns?
Some tutorial that you may want to look at: (grabbed from google)
http://www.phpf1.com/tutorial/php-mysql-tutorial.html
In theory you perform a select, take the data you need and perform an insert.

I have an issue in mysql that i have a field id which is auto increment and some other fields

I have an issue in mysql that i have a field id which is auto increment and some other fields. where the id field should not be autoincremented while enterting the null values and should be autoincremented while entering values insert values in the same row while giving not null values .
It sounds like you need to generate the value for the id field yourself, in your own code, rather than having the database generate it.
If you create an identity field in the database, the database will create the field automatically. Generally this occurs when the record is saved, whether there are null fields present or not. If you need more control than this, you have to generate the id values yourself.
If you need to know what the next id number is, you can get it with SELECT MAX(id_field);
Robert Harvey's answer tackles the problem but I would also suggest looking at what you are saving and see if there is another approach. Perhaps the null values should be saved in another table? Perhaps the column you have as auto-increment isn't the primary key.
This may not be the direction but in this case it's worth stepping back and re-examining.

Categories