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.
Related
How can I execute this using PDO? I use MySQL for my database. I am trying to call the last infog_id.
$q = $conn->query("SELECT * FROM Infographic ORDER BY infog_id DESC LIMIT 1");
$q->fetchAll();
Similar to what Sean said, if you only need one column, don't get all of them.
$q = $conn->query("SELECT infog_id FROM Infographic ORDER BY infog_id DESC LIMIT 1");
$infog_id = $q->fetchColumn();
fetchColumn() by default retrieves the first column from the next available row, for this query, this will be infog_id.
If you actually want the whole row, use fetch().
$q = $conn->query("SELECT * FROM Infographic ORDER BY infog_id DESC LIMIT 1");
$row = $q->fetch();
fetch() returns the next available row, in this case, there is only one (LIMIT 1).
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");
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.
i got a fairly simple layout going and for the life of me i cant figure out why this returns nothing:
<?php
// Gets A List Of Comic Arcs
$result = mysql_query("SELECT * FROM ".$db_tbl_comics." GROUP BY ".$db_fld_comics_arc." ORDER BY ".$db_fld_comics_date." DESC LIMIT 20");
while ($comic = mysql_fetch_array($result)) {
// Now Go Back And Count Issues For Each Comic Arc Above
$result22 = mysql_query("SELECT * FROM ".$db_tbl_comics." WHERE ".$db_fld_comics_arc."=".$comic[$db_fld_comics_arc]);
$total_issues = mysql_num_rows($result22);
echo $total_issues;
}
?>
No other query is refered to as $result22.
$comic[] has already been defined in the previous query.
echo mysql_error($result22); returns no errors.
Let me know if you need any other info.
I am assuming that the column $db_fld_comics_arc is a string.
Change:
$result22 = mysql_query("SELECT * FROM ".$db_tbl_comics." WHERE ".$db_fld_comics_arc."=".$comic[$db_fld_comics_arc]);
To:
$result22 = mysql_query("SELECT * FROM ".$db_tbl_comics." WHERE ".$db_fld_comics_arc."='".$comic[$db_fld_comics_arc]."'");
Am I wrong? If so, let me know the table structure, and what your error reporting is set to.
Also, could you let us know the purpose of your SQL? It may also be possible to put the data together in one query, instead of looping sql queries through, and using data from a first query.
Maybe it is because $db_fld_comics_arc is in $comic[$db_fld_comics_arc]
if both are the same then you should try replacing $db_fld_camics_arc with $comic[$db_fld_comics_arc].
MY SQL QUERY:
$q = mysql_query("SELECT * FROM `ads` WHERE keywords LIKE '%$key%' ORDER BY RAND()");
RESULTS: KEYWORD123
This query searches and results in one random row but i want to show 2 random rows.
How to do that?
any solution?
how??
im grabbing it using this
$row = mysql_fetch_array($q); if ($row
<= 0){ echo 'Not found'; }else{ echo
$row['tab']; }
That query (as-is) will return more than one row (assuming more than one row is LIKE %$key%). If you're only seeing one record, it's possible you're not cycling through the result set, but rather pulling the top response off the stack in your PHP code.
To limit the response to 2 records, you would append LIMIT 2 onto the end of the query. Otherwise, you'll get every row that matches the LIKE operator.
//Build Our Query
$sql = sprintf("SELECT tab
FROM ads
WHERE keyword LIKE '%s'
ORDER BY RAND()
LIMIT 2", ('%'.$key.'%'));
// Load results of query up into a variable
$results = mysql_query($sql);
// Cycle through each returned record
while ( $row = mysql_fetch_array($result) ) {
// do something with $row
echo $row['tab'];
}
The while-loop will run once per returned row. Each time it runs, the $row array inside will represent the current record being accessed. The above example will echo the values stored in your tab field within your db-table.
Remove your order by and add a LIMIT 2
That happens after the execution of the SQL.
Right now you must be doing something like
$res = mysql_query($q);
$r = mysql_fetch_array($res);
echo $r['keywords'];
what you need to do
$q = mysql_query("SELECT * FROM ads WHERE keywords LIKE '%$key%' ORDER BY RAND() LIMIT 2");
$res = mysql_query($q);
while($r = mysql_fetch_array($res)){
echo "<br>" . $r['keywords'];
}
Hope that helps
This query will return all rows containing $key; if it returns only one now this is simply by accident.
You want to add a LIMIT clause to your query, cf http://dev.mysql.com/doc/refman/5.0/en/select.html
Btw both LIKE '%... and ORDER BY RAND() are performance killers