I'm trying to get my site to only output a specific amount of data from my database, and not just the whole thing. Can anyone help me with this? Im trying to get it to display the first 25 lines of data.
Right now my php code, is as follows:
$sql = "SELECT * FROM cards WHERE name LIKE '%$query%';";
$sql = "SELECT * FROM cards WHERE name LIKE '%$query%' LIMIT 10;";
if you are using MySql u can use LIMIT to get a limited number of records.
If you are using MSSQL you can use TOP
hope this helps.
Try it:
$sql = "SELECT * FROM cards WHERE name LIKE '%$query%' LIMIT 0,25;"
Related
I have two tables set up in SQL... One for users, which works for users to log in and out completely fine...
And another to pull a timetable into a html table, which works fine when a user isn't specified... But I'm trying to include a WHERE statement to make only information with the correct user_id (in the timetable) to show, dependant on the users session id.
I'm only receiving errors with this line...
$sql = "SELECT * FROM time_table ORDER BY Number WHERE $_SESSION['user_id']";
$result = mysql_query($sql)or die(mysql_error());
Any advice on where I'm going wrong here?
You are writing a query with wrong syntax.
The query should be like
$sql = "SELECT * FROM time_table WHERE user_id ='".$_SESSION['user_id']."' ORDER BY Number";
Kindly let me know how can I transform the following query in a way that it'll work perfectly in ms access:
$sql = "SELECT * FROM Registration Limit 100,200";
I tried to use the following but it didnot work the way above query works in SQL.
$sql = "SELECT TOP 100,200 * FROM Registration";
You can't do it directly; Access doesn't support either of the LIMIT or TOP <countstart>, <countend> statements.
You can work around it, if you have an auto-increment (identity) column in your table (or something you can use instead to order rows):
SELECT
Top 100 reg.*
FROM
registration reg
WHERE
reg.RegistrationID >
(
SELECT
Top 100 r.RegistrationID
FROM
registration r
ORDER BY
r.RegistrationID
)
ORDER BY
reg.RegistrationID
I've spent the whole day googling and deleting and inserting trying to implement this code. I've been trying to implement a reddit-like site using php and mysql. I have been following another question: PHP MYSQL Query Algorithm Help and it works very well and ranks rows according to the algorithm coded in the previous question within myphpadmin when I query a stored function
SELECT
*,
reddit_rank(`time_added`, `up_votes`, `down_votes`) as rank
FROM
`table`
ORDER BY
rank;
, but when I paste the query into my php file:
<?php
include("config.php");
$q= "SELECT *,reddit_rank(`time` , `votes_up` , `votes_down`) FROM `wallposts` ORDER BY rank LIMIT 0 , 30";
$r = mysql_query($q);
if(mysql_num_rows($r) > 0) {
while($row = mysql_fetch_assoc($r)){
...?>
It doesn't work and I get a white HTML screen. So for example in my PHP when I have
$q = "SELECT * FROM wallposts ORDER BY votes_up DESC";
my reddit/facebook-like wall has prepended each of my rows from mysql and everything works just fine. but when i change it to
$q= "SELECT *,reddit_rank(`time` , `votes_up` , `votes_down`) FROM `wallposts` ORDER BY rank LIMIT 0 , 30";
the webpage returns nothing but a white screen even though I know it works in myphpadmin.
Is there something wrong with my syntax or is it not possible to query a select all with a stored function to order the results in php?
I think I found a solution by creating a view and then querying that view instead of the original table. After I queried the stored function in myphpadmin:
SELECT
*,
reddit_rank(`time_added`, `up_votes`, `down_votes`) as rank
FROM
`table`
ORDER BY
rank;
I then created a view after those results were returned. Then instead of querying the same way in my PHP file, I queried the new mysql view with:
SELECT * FROM [view] ORDER BY rank
Voila!
I have no idea why this isn't working. I've taken code from a previous project that works fine. I just want to select specific columns. Here's what im trying...
$result = mysql_query("SELECT when, where, name FROM tablename ORDER BY count DESC LIMIT 0, 20");
//I dont really know how to debug php well, so this is all I have to go by to know if its not working.
if (!$result)
{
echo "<p>Page load has failed please try again</p>";
}
if I select all like this it works fine:
$result = mysql_query("SELECT * FROM tablename ORDER BY count DESC LIMIT 0, 20");
If im doing an insert, that doesn't work either...
$query = "INSERT INTO tablename (when, where, name) VALUES ('$when' , '$where' , '$name');";
mysql_query($query);
I'm sure the spelling is correct i've looked at it several times, and even if I put
echo $row["name"]; and the others ect while using the select all * they appear...
It just seems like its happening when im selecting individual columns.
I have other tables, with other sites using the same exact code and its working fine.
How can i fix this? or at least how can i debug the php to get some better error messages?
edit:
I'm sure the values going in are good, its just simple helloworld string
I've tried including count in the select incase that needed to be there for the sort, but that didnt make it work.
The word where is a reserved word (the WHERE clause of SELECT, UPDATE and DELETE queries). So is when. count is a built-in function name but can be used as an identifier, though you shouldn't.
If you use it as an identifier, you must always enclose it in `backticks` in the query. I recommend not using parts of the query language as column names.
http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html
WHERE, WHEN and COUNT are reserved MySQL words (http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html). Use backticks like this.
$result = mysql_query("SELECT `where`, `when`, name FROM tablename ORDER BY `count` DESC LIMIT 0, 20");
where is a reserved sql word, use tablename.where to select the correct column.
I am using the query below
$result2 = mysql_query("select * from HandsetStock WHERE SubCategory NOT LIKE '%clearance%'", $dbh2);
I am getting a few duplicate results which I want to eliminate from the results however they are not completely duplicate rows. The column name is Make. I am guessing i need some kind of subquery but I am struggling to get it to work for me. Basically I need to select all records but where Make has the same value just the first record.
Thanks
$result2 = mysql_query("select * from HandsetStock
WHERE SubCategory NOT LIKE '%clearance%'
group by Make", $dbh2)