I have a problem with PHP 5, I do a while for the last results of my table like this :
while($news = mysql_fetch_assoc("SELECT * FROM acts ORDER BY id DESC LIMIT 0,5"))
But I receive the following message
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in
That's because you are using it wrong, you shouldn't pass the query to the mysql_fetch_assoc function but:
$sql = "SELECT * FROM acts ORDER BY id DESC LIMIT 0,5";
$result = mysql_query($sql,$con);
while($news = mysql_fetch_assoc($result))
{
// do something
}
you should pass a resultset to mysql_fetch_assoc(), not a query.
while($news = mysql_fetch_assoc(mysql_query("SELECT * FROM acts ORDER BY id DESC LIMIT 0,5",$con)));
Related
Trying to get the last row in the table but throwing error ...
'Warning: mysqli_fetch_assoc() expects parameter 1 to be
mysqli_result, string given in '
$recents = "SELECT * FROM list ORDER BY id DESC LIMIT 1";
if ($result = mysqli_fetch_assoc($recents)) {
$mName = $result['name'];
$mDesc = $result['description'];
$mCost = $result['cost'];
}
You need to pass a result from a query, not the query string.
$sql = "SELECT * FROM list ORDER BY id DESC LIMIT 1";
$recent = mysqli_query($connetion, $sql);
You forgot to use the statement to run the query
$results=mysqli_query($conn,$recents)
I have the following code which selects information from one random row.
$query = mysql_query("SELECT * FROM lines_angles_shapes ORDER BY RAND() LIMIT 1 ");
while($rows = mysql_fetch_array($query))
{
$question = $rows['question'];
$hint = $rows['hint'];
$level = $rows['level'];
$keyword = $rows['keyword'];
$showme = $rows['showme'];
$picture_path = $rows['picture_path'];
}
This works well for me but I now I need to be able to select two more DIFFERENT pictures from the picture_path column and assign them to variables. Again, all three rows need to be different.
Any tips for a newbie on how to do this?
Just change your query as follows:
$query = mysql_query("SELECT * FROM lines_angles_shapes ORDER BY RAND() LIMIT 3");
You are ordering by ORD() so it will give you different records.
No, new modification, just change the limit to 3 (whatever your need).
$query = mysql_query("SELECT * FROM lines_angles_shapes ORDER BY RAND() LIMIT 3");
As your are already getting random values with order by clause it will always return different values so you just need to edit your limit value and you are done!
$query = mysql_query("SELECT * FROM lines_angles_shapes ORDER BY RAND() LIMIT 3");
I tried to count using following-
include 'db_login';
$sql = "(SELECT * , COUNT(*) FROM `table1`
WHERE `ADVERTISERCATEGORY` LIKE '%something%'
GROUP BY `MANUFACTURER` ORDER BY COUNT DESC )";
$row = mysql_fetch_array($sql);
$total = $row[0];
echo "Total rows: " . $total;
but i got following error message-
Warning: mysql_fetch_array() expects parameter 1 to be resource,
string given in /home/content/43/10130843/html/fashion_test.php on line 169
i am learning php/mysql therefore i need help.
thanks
You haven't called mysql_query(), so you can't fetch the array yet...
$sql = "SELECT * , COUNT(*) FROM `table1`
WHERE `ADVERTISERCATEGORY` LIKE '%something%'
GROUP BY `MANUFACTURER` ORDER BY COUNT(*) DESC ";
$query = mysql_query($sql) or die(mysql_error()); // exit on error
$row = mysql_fetch_array($query);
Note: mysql_query() and the rest of the mysql_* library have been deprecated for some time, you should make the move to paramterized queries with mysqli_* or PDO while you're in the development phase.
I wonder... is this possible to select a random row in my DB where only the record that contains field->Status = 0 ? because i only need a row that contains 0 in the field name=Status. if the status contains = 1 , then the row will not belong in the randomization.
my code in the query is
$result = mysql_query( " SELECT * FROM `$haha` ORDER BY RAND() limit 1");<br>
$result = mysql_query( " SELECT * FROM `$haha` ORDER BY RAND() limit 1 Where Status=0");
But it does not work.. i appreciate your replies.. thank you very much!
Try below code:
$result = mysql_query("SELECT * FROM `".$haha."` Where Status=0 ORDER BY RAND() limit 0,1");
Your where clause should be before then the order by clause.
And want to know what is the value of $haha.
You use the wrong syntax. The good one:
$result = mysql_query( " SELECT * FROM `$haha` Where Status=0 ORDER BY RAND() limit 1");
I'm not sure exactly how to explain what the query does, however the problem isn't entirely with how it's set up, because it does work, in another instance, when I use it as an array, however it's not working when I use it with mysql_fetch_assoc(), so here is what my original query is(not the one im having trouble with):
SELECT * FROM
(SELECT * FROM comments
WHERE postID='$id' AND state='0'
ORDER BY id DESC LIMIT 3
) t ORDER BY id ASC
what this does is selects the last 3 comments on a post, then orders them in another way (so they show up in the correct order, old to new) Now this is the query for echoing out the array of comments directly.
But now what I want to do, is to just get the first id out of the 3 comments.
here's what I have tried to do (and by the way, this query DOES work, when i replace my previous query to echo out the results in an array, but i need to get just the id for use, i don't want an array):
$previousIDq = mysql_fetch_assoc(mysql_query("
SELECT * FROM
(SELECT * FROM comments
WHERE postID='$id' AND state='0'
ORDER BY id DESC LIMIT 3
) t ORDER BY id ASC LIMIT 1"));
$previousID = $previousIDq['id']; //this doesn't return the id as I want it to.
Your problem may be that there are no matching rows.
Also, I think you could also improve your query to this:
SELECT * FROM comments WHERE postID='$id' AND state='0' ORDER BY id DESC LIMIT 2,1
But as others say, use PDO or MySQLi, and with prepared statements. And don't SELECT * ever.
try a var_dump($previousID) to see what you get
It is probably giving you back an object, and you need to get your id from that object
You script is too condensened for error handling, let alone debugging
$mysql = mysql_connect(...
$query = "
SELECT * FROM
(SELECT * FROM comments
WHERE postID='$id' AND state='0'
ORDER BY id DESC LIMIT 3
) t ORDER BY id ASC LIMIT 1
";
$result = mysql_query($query, $mysql);
if ( !$result ) { // query failed
die('<pre>'.htmlspecialchars(mysql_error($mysql)).'</pre><pre>'.htmlspecialchars($query).'</pre>');
}
$previousIDq = mysql_fetch_assoc($result);
if ( !$previousIDq ) {
die('empty result set');
}
else {
$previousID = $previousIDq['id'];
}
You need to separate your code to be able to debug
$query = "SELECT * FROM
(SELECT * FROM comments
WHERE postID='$id' AND state='0'
ORDER BY id DESC LIMIT 3
) t ORDER BY id ASC LIMIT 1";
$result = mysql_query($query);
echo mysql_error(); //what eror comes out here
while($previousIDq = mysql_fetch_assoc($result))
{
print ($previousIDq['id']);
}
NOTE: mysql_* is depreciated, upgrade to mysqli or PDO
Please stop using mysql_ functions to write new code, it is being deprecated. Use mysqli_ or PDO functions (mysqli below).
Bind your parameters to prevent SQL injection
Always use a column list (don't use SELECT *)
If you're returning > 1 row, use a while loop
mysqli_ solution
$link = mysqli_connect("localhost", "user_name", "password", "stock");
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
$stmt = mysqli_prepare($link, "SELECT t.id FROM
(SELECT id FROM comments
WHERE postID = ? AND state = 0
ORDER BY id DESC
LIMIT 3) t
ORDER BY id ASC");
mysqli_bind_param($stmt, 's', $id) or die(mysqli_error($dbh));
$result = mysqli_stmt_execute($stmt) or die(mysqli_error($link));
while($row = mysqli_fetch_assoc($result)) {
echo $row[id];
}
mysqli_close($link);
mysql_ solution
$stmt = "SELECT t.id FROM
(SELECT id FROM comments
WHERE postID = $id AND state = 0
ORDER BY id DESC
LIMIT 3) t
ORDER BY id ASC";
$result = mysql_query($stmt) or die(mysql_error());
$row = mysql_fetch_assoc($result);
while($row = mysql_fetch_assoc($result)) {
echo $row[id];
}