I am trying to query a mysql table using some "likes". But it doesn't work at all.
This is my code:
$color_base1 = $row[color_base1];
$color_base2 = $row[color_base2];
$result2 = mysql_query("SELECT * FROM item_descr WHERE (color_base1 LIKE
'%$color_base1%' OR color_base2 LIKE '%$color_base1%' OR color_base1
LIKE '%$color_base2%' OR color_base2 LIKE '%$color_base2%')
AND id_item != $itemId");
if (mysql_fetch_array($result2) == 0)
{
$result2 = mysql_query("SELECT * FROM item_descr WHERE (keywords LIKE
'%$keywords%') AND id_item != $itemId LIMIT 3");
}
else
{
$row2 = mysql_fetch_array($result2);
echo "<div class='similarTitle'>YOU MAY ALSO LIKE</div>";
while ($row2 = mysql_fetch_array($result2))
{
echo "<div class='similarItems'>";
echo "<img class='similarImage' src='images/{$row2[thumb1]}.jpg'>";
echo "<div class='similarItemsText'>".$row2[name]."</div></div>";
}
}
Thanks!
try changing your queries into these:
Query1:
SELECT *
FROM item_descr
WHERE (color_base1 LIKE CONCAT('%' , $color_base1, '%') OR
color_base2 LIKE CONCAT('%' , $color_base1, '%') OR
color_base1 LIKE CONCAT('%' , $color_base2, '%') OR
color_base2 LIKE CONCAT('%' , $color_base2, '%')) AND
id_item != $itemId
Query 2:
SELECT *
FROM item_descr
WHERE keywords LIKE CONCAT('%' , $keywords, '%') AND
id_item != $itemId
LIMIT 3
Try escaping your variables inside the query
mysql_query("select ... like '%" . $var . "%' ...");
$color_base1 = $row['color_base1']; // <- index NOT constant
$color_base2 = $row['color_base2'];
$result2 = mysql_query("SELECT * FROM item_descr WHERE (color_base1 LIKE '%$color_base1%' OR color_base2 LIKE '%$color_base1%' OR color_base1 LIKE '%$color_base2%' OR color_base2 LIKE '%$color_base2%') AND id_item != $itemId");
if(mysql_num_rows($result2) == 0) // <- check for NO RESULTS
{
$result2 = mysql_query("SELECT * FROM item_descr WHERE (keywords LIKE '%$keywords%') AND id_item != $itemId LIMIT 3");
}
$row2 = mysql_fetch_array($result2);
echo "<div class='similarTitle'>YOU MAY ALSO LIKE</div>";
while ($row2 = mysql_fetch_array($result2))
{
Your array referencing is done incorrectly. $row['color_base1'] is not the same as $row[color_base1]
Your code gets an array from the search results (first row) and checks it against 0. I believe you mean to check the number of results returned and if there were none then check for suggestions.
If your second query is executed, it does nothing with the results
When you process your results to produce output, you are looping through the results from row 2 onwards (see point 2)
DEBUG:
if you do $sql="..."; then you can debug by echo-ing $sql and then checking the results manually by putting them into phpmyadmin
do lots of echo "I am at line:".__ LINE __; to check which path your logic is going in.
compare your phpmyadmin results with a var_dump($row); inside your loop to see if your skipping rows.
myql_query($sql) or die(mysql_error()); // will stop if your query is bad
etc.....
Related
I got the following Mysql command:
// Call on a random style ID to display in rating window which user hasn't seen yet.
$resultSet = $conn->query("SELECT pictureID,userID FROM styles WHERE NOT
viewedByUser = (NOT LIKE '%$userID%') ORDER BY RAND() LIMIT 1");
while($rows = $resultSet->fetch_assoc() ){
$rateableUserID = $rows['userID'];
$rateablePictureID = $rows['pictureID'];
}
I want to use the WHERE NOT function to search through the field "viewedByUser" after a string that does not contain the same string as the variable $userID.
What options have I got here?
Try This :-
$resultSet = $conn->query("SELECT pictureID,userID FROM styles WHERE
viewedByUser NOT LIKE '%$userID%' ORDER BY RAND() LIMIT 1");
while($rows = $resultSet->fetch_assoc() ){
$rateableUserID = $rows['userID'];
$rateablePictureID = $rows['pictureID'];
}
You can directly use != instead of NOT LIKE and there is nothing called WHERE NOT
$resultSet = $conn->query("SELECT pictureID, userID FROM styles WHERE viewedByUser != $userID ORDER BY RAND() LIMIT 1");
Since they are email addresses, you can do:
$userId = 'abc#abc.com xyz#xyz.com';
$userId = explode(' ', $userId);
echo $userStr = implode("', '", $userId);
$resultSet = $conn->query("SELECT pictureID, userID FROM styles WHERE viewedByUser NOT IN ('" . $userStr . "') ORDER BY RAND() LIMIT 1");
If you need a (not) like on the viewedByUser you can simply use
"SELECT pictureID,userID
FROM styles W
WHERE viewedByUser = NOT LIKE '%$userID%'
ORDER BY RAND()
LIMIT 1";
I'm trying my best here to find a solution for my issue, but with no luck.
I have a SELECT in my PHP to retrieve some products information like their IDs.
mysql_query("SELECT id_item FROM mytable WHERE status = '0' AND cond1 = '1' AND cond2 = '1'");
Every time I run this SELECT, I get 5 rows as result. After that I need to run a DELETE to kill those 5 rows using their id_item in my WHERE condition. When I run, manually, something like:
DELETE FROM mytable WHERE id_item IN (1,2,3,4,5);
It works! But my issue is that I don't know how to make an array in PHP to return (1,2,3,4,5) as this kind of array from my SELECT up there, because those 2 other conditions may vary and I have more "status = 0" in my db that can't be killed together. How am I suppose to do so? Please, I appreciate any help.
Unless there is more going on than what is shown, you should never have to select just to determine what to delete. Just form the DELETE query WHERE condition as you would in the SELECT:
DELETE FROM mytable WHERE status = '0' AND cond1 = '1' AND cond2 = '1'
But to answer how to get the IDs:
$result = mysqli_query($link, "SELECT id_item FROM mytable WHERE status = '0' AND cond1 = '1' AND cond2 = '1'");
while($row = mysqli_fetch_assoc($result)) {
$ids[] = $row['id_item'];
}
$ids = implode(',', $ids);
Move to PDO or MySQLi now.
First of all you shouldn't be using mysql_query anymore as the function is deprecated - see php.net
If this is a legacy application and you MUST use mysql_query you'll need to loop through the resource that's returned by mysql_query, which should look something like this
$result = mysql_query("SELECT id_item FROM mytable WHERE status = '0' AND cond1 = '1' AND cond2 = '1'");
$idArray = array();
while ($row = mysql_fetch_assoc($result)) {
$idArray[] = $row['id_item'];
}
if(count($idArray) > 0) {
mysql_query("DELETE FROM mytable WHERE id_item IN (" . implode(',' $idArray) . ")");
}
As said before, probably you don't even need a select. But you can do a select, grouping all ids together, and then put it in the delete IN.
$result = mysql_query("SELECT GROUP_CONCAT(DISTINCT id_item) AS ids FROM mytable WHERE status = '0' AND cond1 = '1' AND cond2 = '1'");
$ids = mysql_result( $result , 0, 'ids') ; // 1,2,3,4,5
if ($ids != ""){
mysql_query("DELETE FROM mytable WHERE id_item IN (" . $ids . ")");
}
GROUP_CONCAT
i have a simple query who select me 3 news from table, but i wont to change this number from other file whith variable.
So this is query:
$query = 'SELECT * FROM news ORDER BY created_at DESC LIMIT 3';
I tried differently, but did not work...
Help please
My code(i can't answer on my question so i add it here)
$newsAmount = 3;
function get_news() {
$query = "SELECT * FROM news ORDER BY created_at DESC LIMIT $newsAmount";
$result = mysql_query($query);
$news = array();
while ($row = mysql_fetch_array($result)) {
$news[] = $row;
}
return $news;
if (!$result) {
trigger_error('Invalid query: ' . mysql_error() . " in " . $query);
}
}
This is this code.
Actually you can just do this:
$query = "SELECT * FROM news ORDER BY created_at DESC LIMIT $newsAmount";
But make sure to keep the string in double quotes so the variable can be evaluated, Single quotes will be printed out as it is.
Try to echo $query, you will notice that its being printed.
Try this:
$newsAmount = 3;
$query = 'SELECT * FROM news ORDER BY created_at DESC LIMIT ' + $newsAmount;
you cannot return an array like you did in your code.
I would suggest to do the query inside your main code instead of writing it in a function.
I am using LIKE to do my searching, i try it in phpMyAdmin and return the result but when i use it in php it return empty result.
$search = "ip";
$start = 0;
$query = "SELECT * FROM product WHERE product_name LIKE '%$search%' LIMIT $start,30";
$result = mysql_query($query);
if(empty($result))
$nrows = 0;
else
$nrows = mysql_num_rows($result);
It will return result when i using phpMyAdmin to run this query but when i use it in php, it return empty.
Update:
Sorry guys,
I just found out the problem is i didn't connect database as well. anyway, thanks for helping.
Try This
$query = "SELECT * FROM `product` WHERE `product_name` LIKE '%".$search."%' LIMIT 0, 30";
And if the sole purpose of your code is to get the number of products with the searched-for name, use SELECT COUNT(*) instead of doing a mysql_num_rows() on all your data. It will decrease your querytime and the amount of data that is (unnecessarily) fetched.
I am not sure why this is not working, as the query seems to be correct to me. I would like to suggest you writing query this way
$query = <<<SQL
SELECT * FROM product WHERE product_name LIKE "%$search%" LIMIT $start,30
SQL;
please note that there should not be any space or any character after SQL;
$query = "SELECT * FROM product WHERE product_name LIKE '%" . $search . "%' LIMIT " . (int) $start. ",30";
you can use directly mysql_num_rows()
but here is right code
$query = "SELECT * FROM product WHERE product_name LIKE '%".$search."%' LIMIT $start,30";
$search = "ip";
$start = '0';
$query = "SELECT * FROM product WHERE product_name LIKE '%".$search."%' LIMIT $start,30";
$result = mysql_query($query)or die(mysql_error());
if(mysql_num_rows($result) == 0){
$nrows = 0;
} else{
$nrows = mysql_num_rows($result);
}
//use mysql_num_rows($result) instead of empty($result) because in this situation $result is every time not empty so use inbuilt PHP function mysql_num_rows($result);
I have the following code:
$query = mysql_query("SELECT * FROM activity ORDER BY activity_time DESC LIMIT 50");
while($result = mysql_fetch_array($query)) {
extract($result);
if ($activity_type == "discussion") {
$query = mysql_query("SELECT * FROM discussions WHERE discussion_uuid = '$activity_ref'");
while($result = mysql_fetch_array($query)) {
extract($result);
echo $discussion_user . " said:<br>" . $discussion_text . "<br>";
}
} elseif ($activity_type == "file") {
}
}
But it just returns the last row. My goal is to have a chronological list of "activities" each displayed slightly differently depending on their type.
Your using $query and $result twice so the second loop is overwriting the result of the first and stopping...
$query = mysql_query("SELECT * FROM activity ORDER BY activity_time DESC LIMIT 50");
and
$query = mysql_query("SELECT * FROM discussions WHERE discussion_uuid = '$activity_ref'");
same with $results var...
I would suggest you change to $query and $query2 but best to use something like
$activies = mysql_query("SELECT * FROM activity ORDER BY activity_time DESC LIMIT 50");
while($activity = mysql_fetch_array($activies)) {
and
$discussions = mysql_query("SELECT * FROM discussions WHERE discussion_uuid = '$activity_ref'");
while($discussion = mysql_fetch_array($discussions)) {
I would also avoid using extract - as you might be overwriting vars your not expecting to...
You have to create another connection to the database so that you can run them at the same time.
OR
You can load the results of the first one into an array, and then just loop through the array.