i want to pass limit value in my select query through user,can anybody help me to do this???? thanks in advance![][1]
$result = mysql_query("select distinct * from tweet_info ".
"where MATCH(tweet) ".
"AGAINST('".$search."')ORDER BY created ", $con);
lets just say that the user input is "LimitInput" then :
$limit = $_POST['LimitInput'];
$result = mysql_query("select distinct * from tweet_info ".
"where MATCH(tweet) ".
"AGAINST('".$search."')ORDER BY created limit 0,".$limit, $con);
$result = mysql_query("SELECT DISTINCT *
FROM tweet_info
WHERE MATCH(tweet)
AGAINST('" . $search . "')
ORDER BY created LIMIT 0,1 DESC", $con);
Just add a limit to the end, this will grab the first result found.
You shouldn't really be using the mysql_query function anyway, it's depreciated, maybe look into a different method, e.g. mysqli or PDO.
Related
I am trying to pull out a row of data from MySQL and put it in an array and then reverse it before displaying the results and then if confirmed by the user it will post the reversed results back into mysql
I am using this code :
for($i=0;$i<6;$i++) {
// Make a MySQL Connection
$query = "SELECT * FROM databasedemo WHERE id='$i'";
$result = mysql_query($query)or die(mysql_error());
$row = mysql_fetch_array($result);
array_reverse($row,true);
echo $i."--"."A".$row['A']. " - ". "B".$row['B']. " - ". "C".$row['C']. " - ". "D".$row['D']. " - ". "E".$row['E'];
echo "<br>";
}
I am getting this error
Warning: array_reverse() [function.array-reverse]: The argument should be an array in /home/nlp4mark/public_html/Databasedemo/main.php on line 37
Any help would be really appreciated. Thanks.
You should only be executing this query once and then iterating through the results and displaying them:
$query = "SELECT * FROM databasedemo ORDER BY id DESC";
$query = "SELECT * FROM databasedemo WHERE id='$i'";
That query will return a one record at the time. You are sending it 6 times with the for loop, that's why you get all the records, but you send 6 times a slighty different query.
try sending one query that return all the 6 records and then reverse it. It would be something like this
$sql = "SELECT * FROM databasedemo WHERE 1=1";
for($i=0;i<6;$i++){
$sql .= " AND id=$i";
}
do like this
select * from (SELECT * from messages INNER JOIN logindata ON messages.author = logindata.id ORDER BY messages.mid DESC LIMIT 0,10) as t ORDER BY t.mid asc
I want only one single data from that DB but I am not able to "take it out of" $res.
$sql = "SELECT * FROM `study_stuffs_extra`.`tid` ORDER BY `id` DESC LIMIT 1 ";
$res = query($sql);
$tid = $res['tid'];
I have also tried a while loop to do so, but "couldn't do it". Is there any other method to "do it"?
try
$sql = "SELECT * FROM `study_stuffs_extra`.`tid` ORDER BY `id` DESC LIMIT 1 ";
$res = mysql_query($sql);
$res=mysql_fetch_array($res);
$tid = $res['tid'];
SELECT tid FROM study_stuffs_extra ORDER BY `id` DESC LIMIT 1
Also, check what query returns. Is that a mysql result? the whole result set? a row? Do some print_r to see what you get. Check for db errors after executing queries.
You may need to subscript the first member of $res, assuming it is an array.
$firstRow = $res[0];
I am connecting to an SQL database in my PHP script and am having trouble with the LIMIT command:
$result = mysql_query("
SELECT *
FROM product
WHERE `category` like \"" . $_GET['category'] . "\"
LIMIT 0, 16
");
This all works, except that if I only have 10 rows then $result contains rows 0~10 and then 0~6 as well.
I am using a a while loop while($row = mysql_fetch_assoc($result)) to check if there is a result and then run an action. Is there any way of having it limit the select statement to only show rows 0~10?
$result = mysql_query("SELECT * FROM product
WHERE `category` like '" . mysql_real_escape_string($_GET['category']) . "' LIMIT 0, 10");
is it what are you looking for? It will give you ten rows maximally..
Additionally, please read this article about SQLi
I'm using the following code:
$con = mysql_connect("NOTGIVEN","NOTGIVEN","NOTGIVEN");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("user_live", $con);
$result = mysql_query("SELECT * FROM user_new_post ORDER BY user_date_post DESC");
while($row = mysql_fetch_array($result))
{
print($row['user_full_name']);
}
And instead of selecting the table/row user_new_post how can I be able to select individual values "users" and then print/echo them out?
Either you have another table "user" on which you can use
SELECT * FROM user
or you can use a WHERE clause
SELECT * FROM user_new_post WHERE user_full_name like 'a%' ORDER BY user_date_post DESC
to get all user full name starting with 'a'
You may perform a search for exactly a user name like this:
$result = mysql_query("SELECT * FROM user_new_post WHERE user_name= '".$user."'");
Or perform a regular expression in the query to search for user's name match your pattern:
$result = mysql_query("SELECT * FROM user_new_post WHERE user_name REGXP '".$pattern."'");
Or use like keywork provied by MySQL:
$result = mysql_query("SELECT * FROM user_new_post WHERE user_name like '".$user."'");
I'm trying to get a pick from my DB that would last for a day (daily pick). I use the following code:
$query = 'SELECT * FROM table ORDER BY rand() LIMIT 1
But as you can see it only gives me a random pick from the table, and every time I refresh the page it gets me a new random pick. How can I make the pick to last for a whole day?
Thanks in advance <3
I'm trying this:
$query = "SELECT * FROM table ORDER BY rand(" . date("Ymd") . ") LIMIT 1";
But I get the following error: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource. This is the part that gets broken:
$results = mysql_query($query);
while($line = mysql_fetch_assoc($results))
So... it should look like this, right? (I mean, choosing the daily random pick?)
$dailyPick = 'SELECT * FROM table ORDER BY rand() LIMIT 1';
$cacheKey = 'dailyPick'. date('dmY');
if($cache->has($cacheKey)) {
$dailyPick = $cache->get($cacheKey);
} else {
// hit database
$dailyPick = $cache->save($cacheKey);
}
I'm trying this now:
$dailyPick = 'SELECT * FROM table ORDER BY rand() LIMIT 1';
$cacheKey = 'dailyPick'. date('dmY');
if($cache->has($cacheKey)) {
$dailyPick = $cache->get($cacheKey);
} else {
// hit database
$dailyPick = $cache->save($cacheKey);
}
However, it gets me a mistake that I'm using the 'has' function on a non-object.
If you set the SEED for the rand to an integer value that changes daily, that would solve your problem
$query = "SELECT * FROM table ORDER BY rand(" . date("Ymd") . ") LIMIT 1";
Would do the trick.
A sane means of doing this would be to automatically generate the pick of the day content via a cron job that was setup to run once a day.
As such, the cron job would execute the SQL you provided and store the appropriate content in a flat file/database table, etc. (or perhaps even just store the choosen id in another table for future lookup purposes).
You can try something like this:
$total = 'SELECT COUNT(*) FROM table;';
$query = 'SELECT * FROM table ORDER BY id ASC LIMIT 1 OFFSET ' . (date('Ymd') % $total) . ';';
I think you'll need to update the random picked record with "today" field = 1..
Something like this:
// ------------
// Run this 3 commands once a day
// Reset all records
mysql_query("UPDATE `table` SET `today` = 0");
// Pick one
$sql = mysql_query("SELECT `id` FROM `table` ORDER BY RAND() LIMIT 1");
$id = mysql_result($sql, 0, 'id');
// Update the record
mysql_query("UPDATE `table` SET `today` = 1 WHERE `id` = {$id}");
// ------------
// Now you can find again your "random found record":
$query = mysql_query("SELECT * FROM `table` WHERE `today` = 1");