This is my script atm, and it doesn't load more images. I really don't know what i'm doing wrong? I've tried first converting my mysql to pdo, it loads the first set of images, but when i visit my website, it already says, "No more content" at top, and can't scroll to infinite.
See the new code!
EDIT
Oke so I did it step by step and this doesn't work well. I tried it like this:
$result2 = $pdo->prepare("SELECT * FROM scroll_images ORDER BY id ASC LIMIT ?");
$result2->execute(array($set_limit));
Doesn't work, only when I use it like this:
$result2 = $pdo->query("SELECT * FROM scroll_images ORDER BY id ASC LIMIT $set_limit");
Why is that?
NEW EDIT!
So the data thing is working, now the index. The first statement works, because it load's the first images. But now the rest of the query for getting all the rows in one query and get it with FOUND_ROWS()
Code:
$result = $pdo->query("SELECT SQL_CALC_FOUND_ROWS * FROM scroll_images ORDER BY id ASC limit 12"); // This works, but don't know about the sql_calc_found rows part.
$resultALL = $pdo->query("SELECT FOUND_ROWS() AS rowcount");
$resultALL->fetch(PDO::FETCH_OBJ);
$actual_row_count = $resultALL->rowcount; // doesn't return anything.
Related
So I've been stuck on this for a while and I can't find anything on google for this specific thing.
I have this small snippet of code
$link = mysqli_connect("localhost", 'username','password',"database");
$sql = "SELECT * FROM `uploads` ORDER BY id DESC LIMIT 0, 1";
Which should select the latest table by order of id's right?
Well what I want to do is return this id. So if I have 5 items/rows I want to grab the latest (5 in this case) id of the table, and return it. With the eventual goal of using this returned id in javascript but that's a worry for later, right now I just want it in plaintext where the result should only be the id.
This is probably a duplicate question but I can't for the life of me find what I should google to get there
EDIT:
I guess I should clarify further. I know I'm able to do
$sql = "SELECT ID FROM `uploads` ORDER BY id DESC LIMIT 0, 1";
but whenever I try to actually retrieve it/print it its returned as a string instead of the ID.
EDIT 2: I, thanks to some comments, have managed to figure it out. Sorry for the badly worded everything, I'm new to this and as I said don't know how to word it.
SOLUTION:
After just throwing away the $sql thing I added:
$result = mysqli_query($link,"SELECT * FROM `uploads`");
Then I simply did
echo mysqli_num_rows($result);
To echo out the number of rows/what I called the "ID".
Sorry for all the confusion, thanks to those that tried to help. To the others there's no need to be rude.
If I understood your question correctly, you want to get the ID field only, so you have two options:
Option 1 (Recommended)
Given your code
$sql = "SELECT * FROM `uploads` ORDER BY id DESC LIMIT 0, 1";
Change it to:
$sql = "SELECT ID FROM `uploads` ORDER BY id DESC LIMIT 0, 1";
This way, your getting just that ID field you're after. Nothing else is returned from each row.
Option 2
Keep your sql query as it is, and get the ID field from each row in your results (it's an array, so you can retrieve only one field by using its index or name).
Of course, I assume there's an ID field in your table!
Just select the ID.
SELECT id
FROM uploads
ORDER BY id DESC
LIMIT 1;
Simply select what you want.
$sql = "SELECT id FROM `uploads` ORDER BY id DESC LIMIT 0, 1";
The * means you want to select every column there is. However, SQL gives you the possibility to select the specific columns you want. You could also do something like
$sql = "SELECT id, name, title, somethingelse FROM `uploads` ORDER BY id DESC LIMIT 0, 1";
and you'd receive these 4 fields as an array.
My problem is this: I found a easy and fast way to get random row in my table. First, i am using query, which counts my ids from my table. Second, i generate random number from 1 to result of count query. Third, i am selecting row from my table where id is equal to my random generated number. Everything works fine, but the problem is that sometimes query displays me blank page with no information given, with no error given.
here is my code:
$viso = $stmt = $db->query("select count(id) from intropage")->fetchColumn();
$min=1;
$max= $viso;
$lopas=rand($min,$max);
$stmt = $db->query('SELECT * FROM intropage WHERE id='.$lopas.'');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
... }
How can i fix this "blank page" issue?
Thanks to all of you for any answers!
It's not fast method, because you are using double request to db AND you are exposed to SQL injection. Try:
$query = $db->prepare('SELECT * FROM intropage ORDER BY RAND() LIMI 1');
$query->execute();
$results = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($results as $row) {
/* */
}
I think it will fix your blank page error too. If not, turn on error reporting and tell us what error you get.
Is error_reporting activated ?
Your query is wrong so an error is throw and you probably cannot see it
$db->query("SELECT * FROM intropage WHERE id='".$lopas."'");
Also, a better way to have random row, is to use RAND()
$db->query("SELECT * FROM intropage ORDER BY RAND() LIMIT 1");
I have made " news & updates " simple script
my query is:
$query = mysql_query("SELECT * FROM a_commants WHERE postid='$postid' ORDER BY id DESC LIMIT 0,10");
it shows last comment
i want to make it show all comments or at least 10 comments
if i change it to:
$query = mysql_query("SELECT * FROM a_commants WHERE
postid='$postid'");
it shows first comment only
idk whats wrong :(
I think that the problem is in your php code, not MySQL.
The query seems fine, as long as you have more than one comment, but it seems that you are not iterating through results, just printing the first row you get from db.
This should show last 10 comments:
$res = mysql_query("SELECT * FROM a_commants WHERE postid='$postid' ORDER BY id DESC LIMIT 0,10");
while($row = mysql_fetch_array($res)){ // iterate through results
print_r($row); // print the row
}
And you should definitely switch to mysqli or PDO, and sanitize your inputs.
The mysql_* functions are deprecated and going to be removed from PHP.
following sql query was working fine, i don't whats wrong i did its stopped fetching records.
$data = mysql_query("SELECT * FROM product_table where pid=$saa1 OR gpid=$saa1 OR
category_id=$saa1 ORDER BY autoid desc limit $no2,20")
or die(mysql_error());
when i remove or clause its works fine for example
$data = mysql_query("SELECT * FROM product_table ORDER BY autoid desc limit
$no2,20")
or die(mysql_error());
please have a look and let me know where i am doing mistake....
regards,
It seems,your query is OK, but when you use WHERE clause, you limit the results , so perhaps there is no recored for display, especially when you use LIMIT for starting offset and number of results.
Your query is ok but there is no record to satisfy your where part. go to your database and create some new rows with your criteria.
Try:
$data = mysql_query("SELECT * FROM product_table where (pid=$saa1 OR gpid=$saa1 OR
category_id=$saa1) ORDER BY autoid desc limit $no2,20")
or die(mysql_error());
I'm having some difficulties in making this PHP pagination work.
Here is the code, http://www.phpeasystep.com/phptu/29.html.
This works fine, but when I modify the SQL query and use the WHERE clause, it shows the first page of results, but when I change the page the data is lost. What is wrong?
What that comment means is you want to apply the same WHERE conditions (if you have any) to both the queries so your count and actual dataset will be in sync.
/*
First get total number of rows in data table.
If you have a WHERE clause in your query, make sure you mirror it here.
*/
$query = "SELECT COUNT(*) as num FROM $tbl_name";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
/* Get data. */
$sql = "SELECT column_name FROM $tbl_name LIMIT $start, $limit";
$result = mysql_query($sql);