Get n random rows from mysql and write them to file [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to take n random rows from mysql table and write them to file . Please help me how could I do it using php? Structure of mysql table is:
CREATE TABLE IF NOT EXISTS `keys` (
`keyword` varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Supposing that by first n rows you meant the first N alphabetically, your code shoud be something like:
$fd = fopen("/the/filename/you/want","w");
$db = new mysqli($host,$username,$passwd,$dbname);
$rs = $db->query("SELECT `keyword` FROM `keys` order by `keyword` LIMIT $n"); //Being $n the number of rows that you want
while ($row = $rs->fetch_assoc())
{
fwrite($fd,$row['keyword']);
}
$rs->free();
fclose($fd);

Related

mysql query for password should not be last three passwords? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have pwd_history table containing columns id(PK),old_password, created_date,user_id. When ever user modify the password an extra row will be added to that table.I want a single query through which it should verify the entered password is in last three passwords modified by the user. Is there a way in Mysql? thanks.
If the password was hashed with mysql's default password, then
try this, with valid input values:
Updated:
select * from (
select * from old_pwds_table
where user_id=input_id
order by created_date desc
limit 3
) a
where
old_password=password('new password')
Former answer:
select * from old_pwds_table
where user_id=input_id
and old_password=password('new password')
order by created_date desc
limit 3
Check this function hope It helps to you:
function check_old_pass($new_pwd){
$result=mysql_num_rows(mysql_query("select * from old_pwds_table
where user_id=input_id
and old_password=$new_pwd
order by created_date desc
limit 0,3"));
if ($result==1) {
return true;
} else {
return false;
}
}
SELECT
*
FROM
pwd_history
WHERE
user_id = 'USER_ID'
AND
oldpasswords = 'NEW_PASSWORD'
ORDER BY
created_date DESC
LIMIT 3

how to get number exist or not in a string value in sql [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have string like this '1~5~14~62~53~26' in a sql column, actually these are values of a user selected list. Now i want to check if the given string contains specific value like '5' exist or not how can i achieve this in sql query?
SQL Fiddle
MySQL 5.6.6 m9 Schema Setup:
CREATE TABLE mytable
(
id int auto_increment primary key,
cities varchar(20)
);
INSERT INTO mytable
(cities)
VALUES
('5~1~23~45~9~8~71'),
('2~22~39~8~23~5'),
('2~22~39~8~5~9'),
('2~22~39~8~23');
Query 1:
SELECT `cities` FROM `mytable` where `cities` REGEXP '^5~|~5$|~5~'
Results:
| CITIES |
|------------------|
| 5~1~23~45~9~8~71 |
| 2~22~39~8~23~5 |
| 2~22~39~8~5~9 |
More about REGEXP and RLIKE
You could use something like
SELECT * from MyTable where locate(concat('~','5','~'), concat('~',`MyColumn`,'~')) != 0;
It's horrible, and prone to error.
The real solution is to normalise your data, which in this case means put your user-selected list entries in a separate table, indexed by user id.
Is it in MySQL?
Then use REGEXP for example:
SELECT * FROM `table_name` WHERE `col_name` REGEXP '[5]';

MYSQL + PHP issue where Firstname [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Guys im having a issue with my mysql php problem ok i want this to pull table information out of my mysql database in it shows up the firstname by the number 10 but i want to add more too it like 10 100 1000 in other numbers how can i do this from this code
$result = mysqli_query($con,"SELECT * FROM tvshows
WHERE FirstName='10',100,1000");
SELECT * FROM tvshows WHERE FirstName IN (10,100,1000);
You can look here:
http://dev.mysql.com/doc/refman/5.0/en/any-in-some-subqueries.html
and here:
http://dev.mysql.com/tech-resources/articles/subqueries_part_1.html
If you want to select strings, you need to '', ex.:
SELECT * FROM tvshows WHERE FirstName IN ('10','100','1000');
If Numbers like this:
SELECT * FROM tvshows WHERE FirstName IN (10,100,1000);
The IN operator allows you to specify multiple values in a WHERE clause.
$result = mysqli_query($con,"SELECT * FROM tvshows
WHERE FirstName IN('10','100','1000')");
See Example
You have to change the = for IN
$result = mysqli_query($con,"SELECT * FROM tvshows WHERE FirstName IN(10,100,1000);
SQL IN clause documentation

SQL query display last row by a column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I trying to display all information submitted by a specific username(variable) in my case:
<?php
$Username = $_SESSION['VALID_USER_ID'];
$q = mysql_query("SELECT * FROM `article_table` ORDER BY FIELD (Username, '.$Username.') DESC LIMIT 1");
while($db = mysql_fetch_array($q)) { ?>
Your Articles: <?=$db['Subject'];?><br />
<? } ?>
But don't work, I'll display the last article subject(for example) from the table and not from the specific username, where I'm wrong?
That is not how ORDER BY works. You first need to filter the results by using the WHERE clause.
$q = mysql_query("SELECT * FROM `article_table` WHERE `Username` = '$Username' ORDER BY `other_column` DESC LIMIT 1");
Replace other_colum in this query by the name of your date column: ie. date_created
Here is your query:
SELECT *
FROM `article_table`
ORDER BY FIELD (Username, '.$Username.') DESC
LIMIT 1;
This should be returning the username you are looking for when it is available. The logic is that field returns 1 when the name matches and 0 otherwise. So the descending keyword puts the match first. If there is no match, then you will get another row.
Presumably the data does not exist. You can test that easily enough by running:
SELECT *
FROM `article_table`
WHERE Username = '.$Username.';

SQL query select all rows except one [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to select everything in the table jogos except the row where intCategoria is 11. How can I do that?
$query_GameData = sprintf("SELECT * FROM `jogos` ORDER BY RAND()");
SELECT *
FROM `jogos`
WHERE intCategoria <> 11
ORDER BY RAND()
Just use WHERE clause like that :
$query_GameData = sprintf("SELECT * FROM `jogos` where intCategoria != 11 ORDER BY RAND() ");

Categories