I developing a chat app, in that i need to connect between two users in my database and the point is i dont want to connect users that already connected earlier. So i store a column to store the already connected users which will be in the format: id1|id2|id3|.....|id50.
First i thought of selecting a user and get his id and then fetch the user's connected column and then by php check if id exists in his column, thus denying that chat. But it makes lot more complicated.
Can anyone recommend a strategy to make this simple by using only MySQL Queries, i am not advanced in writing nested or join queries and the don't know all pre-defined functions that exist in MySQL.
Can anyone make a suggestion?
SELECT * FROM table WHERE column IN (this,that,these,those,em)
if statement returns not empty value, sure, user exists.
I wrote example where you can check many users at once. Just implode your id-user-list and quire em in parenthesise..
Thus, u'll need to parse returned array to see who succeeded in selecting, who not. (compare two arrays)
Happy coding..
Related
I'm new in Firebird but I'd like to write a small script in PHP that reads a CSV file and fills an existing Firebird db with its data.
The problem is I don't really know how to use the autoincrement generator. I've googled a lot but it's still a mistery for me. There is a gen_main generator defined in the db and I can use it in the IBExpert's query builder but cannot in PHP...
I saw a function named ibase_gen_id, what is the "PDO-way" of it?
What is the process of inserting a row that has an autoincremented field with PDO?
Thanks in advance!
NOTE: I have never used PDO, so I can't comment on PDO specifics.
Depending on your exact needs you can use: NEXT VALUE FOR
NEXT VALUE FOR <sequence-name>
or GEN_ID
GEN_ID(<sequence-name>, 1)
To get the next value of the sequence/generator.
You can either use these directly in your INSERT statement, or first issue a SELECT query against RDB$DATABASE to retrieve the value yourself before inserting: in Firebird you need to use a SELECT to retrieve values, and you always need to select against a table. RDB$DATABASE is guaranteed to contain only one row (like Oracle's DUAL).
So you need SELECT NEXT VALUE FOR GEN_MAIN FROM RDB$DATABASE or SELECT GEN_ID(GEN_MAIN, 1) FROM RDB$DATABASE to get the next sequence value.
In general however I would advise you to add a trigger to do the auto-increment for you, see Firebird Generator Guide for details. You can then use INSERT ... RETURNING <column-list> to retrieve the generated id.
I have a website which generates each visitor a referral link (ex. http://mysite.com/?ref=12345678). The actual referral id (12345678) is a unique 8 character ID (using uniqid() ).
I then just add the ID to the end of http://example.com/?ref=....
I am trying to find a script which can connect to my MySQL database, check if the id exists, and if it doesn't, enter it into the table.
If it does exist it shouldn't do anything.
I am guessing that I need to implement a cookie to check if the id exists, so I don't really need help with that. I'm just confused to how to make the script I mentioned above.
I'm trying to make the table look like this:
Unique ID
---------
3af456yT
Sa32xs21
9af456yT
8a78Fs21
1wsd4Fav
7f3Xv5Bd
Here is a great place to start: http://php.net/manual/en/book.mysql.php. The documentation will provide the information you need to connect to a MySQL database via PHP and to insert data. Specifically, you will need to use the mysql_query() function.
You can use the "UPDATE INTO" syntax to ensure that you do not create duplicate rows. Please see http://dev.mysql.com/doc/refman/5.0/en/update.html for more information.
I'm creating a new web application and I only want to update certain tables when the db is changed, this will be done by firing off a PHP script in a JavaScript setInterval.
My issue is that I am having a hard time trying to find a way to easily determine if certain rows in the database table have changed. In other instances I have used "show table status" and compared that against the last update_time that I had already stored.
In this case though, I don't want to look at the entire table, I just need to know if the rows in my query have changed. I originally tried to md5 the array, as well as md5 on the serialization of the array. It seems that neither of them work and return the same md5 hash each time.
Just for example, here is my query:
$getUserTable = mysql_query("select * from uct_admin.resources where teamID='62' order by ResourceName");
Solution that I came up with after reading the answers:
$getUserTable = mysql_query("select * from uct_admin.resources where teamID='62' order by csiname");
while ($row = mysql_fetch_assoc($getUserTable)) {
$arrayTest[] = $row;
}
$hash = md5(http_build_query($arrayTest, 'flags_'));
This works perfectly due to the nested array returned from the query.
I would consider adding an additional TIMESTAMP field to the table that's updated on UPDATE/INSERT then you could just check SELECT MAX(lastModified) WHERE teamID=62
If you don't want to change the table structure you could add a trigger to this table and have it keep track of the last change to each teams's data in a separate table.
I'd create a table to log all changes, called dbLogTable. ID (foreign key) and timeStamp would be its columns. Each time your PHP script runs, use truncate table dbLogTable in order to clear the table. Your PHP script could detect any changes by running COUNT(*) FROM dbLogTable, which would be zero if there were no changes and nonzero if there were changes. All changes could then be accessed via the foreign key, ID.
Disclaimer: I'm a noob when it comes to DB work, so while this may be how I would do it, it might not be the best way to do it.
(I'd go with James' solution unless there's a specific reason you can't)
I did something similar and managed to get the hashing solution you alluded to working okay. It would be very strange to get the same hash from different data.
In my code I'm doing an implode on the array rather than a serialization. I then store the hash in the database and when I want to compare a new set of data against it I run the md5 on the new data and compare it to the hash.
If it still doesn't work can we maybe see your code for hashing and some sample data? I'd be curious to see what's up if this method doesn't work.
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'm working on a basic php/mysql CMS and have a few questions regarding performance.
When viewing a blog page (or other sortable data) from the front-end, I want to allow a simple 'sort' variable to be added to the querystring, allowing posts to be sorted by any column. Obviously I can't accept anything from the querystring, and need to make sure the column exists on the table.
At the moment I'm using
SHOW TABLES;
to get a list of all of the tables in the database, then looping the array of table names and performing
SHOW COLUMNS;
on each.
My worry is that my CMS might take a performance hit here. I thought about using a static array of the table names but need to keep this flexible as I'm implementing a plugin system.
Does anybody have any suggestions on how I can keep this more concise?
Thankyou
If you using mysql 5+ then you'll find database information_schema usefull for your task. In this database you can access information of tables, columns, references by simple SQL queries. For example you can find if there is specific column at the table:
SELECT count(*) from COLUMNS
WHERE
TABLE_SCHEMA='your_database_name' AND
TABLE_NAME='your_table' AND
COLUMN_NAME='your_column';
Here is list of tables with specific column exists:
SELECT TABLE_SCHEMA, TABLE_NAME from COLUMNS WHERE COLUMN_NAME='your_column';
Since you're currently hitting the db twice before you do your actual query, you might want to consider just wrapping the actual query in a try{} block. Then if the query works you've only done one operation instead of 3. And if the query fails, you've still only wasted one query instead of potentially two.
The important caveat (as usual!) is that any user input be cleaned before doing this.
You could query the table up front and store the columns in a cache layer (i.e. memcache or APC). You could then set the expire time on the file to infinite and only delete and re-create the cache file when a plugin has been newly added, updated, etc.
I guess the best bet is to put all that stuff ur getting from Show tables etc in a file already and just include it, instead of running that every time. Or implement some sort of caching if the project is still in development and u think the fields will change.