In mysql I want to display matched information with values... But the code I use select information separately and I get a bad result. What I want is to wrap the "select" functions together so they both look for specific information in mysql database.
Here's the code I use:
$name = explode(',',$name); //splits search
$query = "SELECT * FROM lucky WHERE name LIKE '%" . implode("%' AND name LIKE '%", $name) . "%'";
$sname = explode(',',$sname); //splits search
$query = "SELECT * FROM lucky WHERE sname LIKE '%" . implode("%' AND sname LIKE '%", $sname) . "%'";
$result = $mysqli->query($query);
while($row = $result->fetch_assoc()) {
echo "<table id='box'>";
echo "<tr;>";
echo "<td id='text''>Name:</td><td id='haha'>" . $row['name']. "</td>";
echo "<td id='text'>Second Name:</td><td id='haha'>" . $row['sname'] . "</td>";
echo "</tr;>";
echo "</table>";
} else { echo "No mathed information" ; }
Thanks in advance :)
UPADATE
Thanks to Spencer for his help!!!
Here's the code that I use now. Please use this to select matched information from the database!!!
$query = SELECT *
FROM lucky
WHERE name LIKE '$name'
AND sname LIKE '$sname'
$result = $mysqli->query($query);
while($row = $result->fetch_assoc()) {
echo "<table id='box'>";
echo "<tr;>";
echo "<td id='text''>Name:</td><td id='haha'>" . $row['name']. "</td>";
echo "<td id='text'>Second Name:</td><td id='haha'>" . $row['sname'] . "</td>";
echo "</tr;>";
echo "</table>";
} else { echo "No mathed information" ; }
If your $sname string contains the value "jack,jill", then the query would only return rows that have a name column value that contains both of those strings, for example: 'jack and jill' and 'jillojellojacko' would match. But the query will not return rows where the name column contains 'jack' but doesn't contain 'jill'.
If your intent is to search for rows that have either of the values matching, for example
$name = 'fee,fi,fo'
$sname = 'fum'
That is, any rows where name column contains either 'fee', or 'fi', or 'fo', or sname column contains 'fum', you could use a query of the form:
SELECT t.*
FROM lucky t
WHERE t.name LIKE '%fee%'
OR t.name LIKE '%fi%'
OR t.name LIKE '%fo%'
OR t.sname LIKE '%fum%'
If you replace all those ORs with ANDs, then a row will need to satisfy all of those predicates to be returned. If you want a combination of AND and OR, then use parens to specify the order of precedence...
Related
I need to use the selection from and HTML select element as one of the variables in my sql query. Meaning if the user selects "Iphone" from the list, I want to query to be something like select * where name = Iphone.
I do not know how to go about this, thank you.
This code creates the select based on the query:
<option disabled selected value> -- Select a forum -- </option>
<?php
$con = mysqli_connect("host","user","pass","db");
if (!$con) {
die('Connection failed: ' . mysqli_connect_error() . '<br>');
}
$productName='SELECT p.name
FROM product as p
JOIN ownedproducts as o on o.productID = p.productID
WHERE usersID =2;';
$result=mysqli_query($con, $productName);
while ($row = mysqli_fetch_assoc($result)) {
unset($id);
$id = $row['name'];
echo '<option value="'.$id.'">'.$id.'</option>';
}
</select>
I would like to take what the user selects, including multiple selections, and build a table based on that something like this where the AND p.name = selected is replaced with what would actually work in this case. I think javascript is the way to go but I do not know enough.
<?php
$query1 = $db->query('SELECT p.productID, p.name, p.company, o.prodtype AS Type
FROM ownedproducts AS o
JOIN product as p ON p.productID = o.productID
WHERE o.usersID = 2
AND p.name = selected');
while ($row = $query1->fetch())
{
if . $row['name'] . = selected:
echo "<tr id=" . $row['productID'] . ">";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['company'] . "</td>";
echo "<td>" . $row['Type'] . "</td>";
echo "</tr>";
}
?>
Edited for more detail.
Thanks in advance
Though, I am not sure what are you asking for but select statement for exact match will be like below=>
select * from tablename where name='Iphone';
or if you want partial match then you can use wildcard(%) and like operator given below =>
select * from tablename where name like '%Iphone%';
Were can i insert my datediff query in this code
$search = $_POST['search'];
$result = mysql_query("SELECT * FROM documents where docnum "My Search Bar"='$_SESSION[test2]' and doctitle LIKE '%$search%' order by docprim desc");
while($row = mysql_fetch_array($result))
{
echo "<tr class ='hovered'>";
echo "<td width = '100px' class='text-left'>";
echo $row['docnum'];
echo "</td>";
echo "<td width = '100px' class='text-left'>";
echo $row['datein']." to ".$row['dateout'];
echo "</td>";
}
this is a sample of my code. i want to add duration from datein and dateout field from my table.
You need to modify your SQL Query String.
$startdate = date('Y-m-d');
$enddate = date('Y-m-d', strtotime("-3 days"));
$result = mysql_query("SELECT * FROM documents where docnum ='".$_SESSION['test2']."' and doctitle LIKE '%$search%' and DATEDIFF('".$startdate."', '".$enddate."') > 0 order by docprim desc");
Also your SQL Query string has issues. Let me know if you need help with that as well.
Hope this helps.
Insert TIMESTAMPDIFF before '*' in sql-query:
SELECT TIMESTAMPDIFF(SECOND, d.datein, d.dateout) AS timediff, m.* FROM documents m;
and use as:
echo $row['datein']." to ".$row['dateout'] . " (" . $row['timediff'] . ")" ;
$row['timediff'] will show seconds.
I have a while loop that iterates through the rows returned from a DB query.
Query
$sql = "SELECT t.barcode AS barcode, t.code, t.brand, t.name, t.cost, t.price, t.vat, SUM(n.stock) AS stock
FROM t
INNER JOIN c.am ON t.code=am.code
INNER JOIN n ON t.code=n.code WHERE (n.name='X' OR n.name='Y')
AND t.code IN ($in)
GROUP BY t.code, t.name, t.cost, t.salesprice, t.vat";
$result = $mysqli->query($sql);
echo "<table id='maintable'><tr><th>Barcode</th><th>Name</th><th>Brand</th></tr>";
while($row = $result->fetch_array()) {
if(empty($row['barcode'])){
$barcode = "none";
}
$barcode = $row['barcode'];
$name = $row['name'];
$brand = $row['brand'];
echo "<td>" . $barcode . "</td>";
echo "<td>" . $name . "</td>";
echo "<td>" . $brand . "</td>";
}
echo "</table>";
The problem is if barcode is empty (not in the DB table), no rows get displayed. However if there is a barcode, the row DOES display. In my example I have tried to check if $row['barcode'] is empty to assign a string so that the row will still display but its unsuccessful.
In the Table itself in the database, the Barcode has a Null field set to YES and Default Field set to NULL so I have also tried:
if(is_null($row['barcode'])) { ..... };
But unsuccessful.
Somewhere I read that empty can equate to: '', 0 and NULL so I'm thinking it's failing because checking if "empty" is the wrong approach?
Any help appreciated.
if(empty($row['barcode'])){
$barcode = "none";
} else {
$barcode = $row['barcode'];
}
or even better:
$barcode = empty($row['barcode'])?"none":$row['barcode'];
So your code would be:
while($row = $result->fetch_array()) {
$barcode = empty($row['barcode'])?"none":$row['barcode'];
$name = $row['name'];
$brand = $row['brand'];
echo "<td>" . $barcode . "</td>";
echo "<td>" . $name . "</td>";
echo "<td>" . $brand . "</td>";
}
UPDATE Not sure about your database structure, but if you need some empty t.barcode values your query could be like:
$sql = "SELECT
t.barcode AS barcode,
n.code,
t.brand, t.name, t.cost, t.price, t.vat,
SUM(n.stock) AS stock
FROM n
LEFT JOIN t
ON t.code=n.code
AND t.code IN ($in)
WHERE (n.name='X' OR n.name='Y')
GROUP BY n.code, t.name, t.cost, t.salesprice, t.vat";
And I don't understand what is INNER JOIN c.am ON t.code=am.code stands for, maybe you should delete it.
Your conditional logic is being followed by this line:
$barcode = $row['barcode'];
Whatever was assigned to $barcode previously is being overwritten.
Looks like you wanted that to be else, or you wanted that line before the conditional test.
As an alternative, you could just modify your query to return the string "none" in place of NULL with an IFNULL function, assuming that barcode is character type.
SELECT IFNULL(t.barcode,'none') AS barcode
, ...
FROM t
EDIT
The question has been edited (after I posted my answer), to add a SQL query to the question.
I thought the problem was that an empty (zero length) string was being assigned to a variable, and the intent was to replace the empty string with a literal.
I have a table with two collumns (shortened), NAME and CATEGORY.
I want to output the number of distinct categorys. For an examle: Sport : 5 , Houses : 10.
I use this one:
$test = mysqli_query($con,"SELECT category, COUNT(category) as count FROM tablename GROUP BY category ORDER BY count DESC");
This work then I run the code in SQL Shell, but I have no clue on how to output it in PHP. I have searced Google up and down without any successfull solution.
Any help?
I want to output it in a table format.
EDIT: Here is my full code: (tablename is changed, and $con is removed)
$test = mysqli_query($con,"SELECT DISTINCT lkategori, COUNT(lkategori) as count FROM tablename GROUP BY lkategori ORDER BY count DESC");
while($row = mysql_fetch_array($test)) {
echo $row['lkategori'] . ":" . $row['count'];
die("test");
}
$test = mysqli_query($con,"SELECT DISTINCT lkategori, COUNT(lkategori) as count FROM tablename GROUP BY lkategori ORDER BY count DESC");
echo "<table border='1'>";
while($row = mysqli_fetch_array($test)) {
echo "<tr>";
echo "<td>" . $row['lkategori'] . "</td>";
echo "<td>" . $row['count'] . "</td>";
echo "</tr>";
}
echo "</table>";
This will output all the categories and the count returned by the sql statement into a table. Also as a sidenote you should look into PDO.
EDIT: to make sure you do get the distinct values you should use the DISTINCT keyword in your sql statement:
$test = mysqli_query($con,"SELECT DISTINCT category, COUNT(category) as count FROM tablename GROUP BY category ORDER BY count DESC");
use this
while($row = mysqli_fetch_array($test)) {
echo $row['lkategori'] . ":" . $row['count'];
die("test");
}
Thanks
i cannot get a row to delete as the id is not going through the url. its a simple error somewhere and i cannot find the solution after having a look around for an hour.
this page contains the information on a table:
<?php
$result = mysql_query("SELECT review, ratings, date, user FROM reviews")
or die(mysql_error()); ;
if (mysql_num_rows($result) == 0) {
echo 'There Arent Any Reviews Yet';
} else {
echo "<table border='0'><table width=100% border='6'><tr><th>Comments/Thoughts</th><th>Ratings</th><th>Date</th><th>User</th><th>Delete</th></tr>";
while($info = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $info['review']. "</td>";
echo "<td>" . $info['ratings']. " Stars</td>";
echo "<td>" . $info['date']. "</td>";
echo "<td>" . $info['user']. "</td>";
echo "<td>" . " <a href='deletereview.php?review_id=" . $info['review_id'] . "'>Delete</a> </td>";
echo "</tr>";
}
}
echo "</table>";
?>
it goes to deletereview.php which carries out the delete function:
<?php
session_start();
require_once '../includes/db.php';
$id = $_GET['review_id'];
$info = "DELETE FROM reviews WHERE review_id = '$id'";
mysql_query($info) or die ("Error: ".mysql_error());
echo "<h2>Review Deleted</h2>";
?>
any ideas guys?
You're not selecting the review_id in the query, so $info["review_id"] is always null.
Aside from the other answers, I'll say this:
Your database will get jacked if you do not sanitize your variables.
For instance, what happens if I pass review_id=' OR '1'='1?
DELETE FROM reviews WHERE review_id = '' OR '1'='1'
This query will delete everything in reviews.
mysql_real_escape_string() your $_GET and $_POST variables before using them in your MySQL.
You forgot to select the review_id.
$result = mysql_query("SELECT review_id, review, ratings, date, user FROM reviews")
You're not selecting review_id from the database but you use $info['review_id'] to set the ID on the URL. Just change your first line to:
$result = mysql_query("SELECT review_id, review, ratings, date, user FROM reviews")
Also you must escape the input with mysql_real_escape_string:
$id = mysql_real_escape_string($_GET['review_id']);
You have to select the review_id in the query. But also you have to check for some SQL injection, because with the GET request it's easy to delete all the table records.