I have an Android app that will POST the category variable to my PHP. In my PHP i wand to find the category (column) the buyer is interested in and then in that row concatenate the 3 other columns from that same row. Then save that into a variable which i will echo out.
i realize there are multiple products listed in the same category but that is a different question.
PHP code:
<?php
$conn = mysqli_connect("phpmyadmin.cvw71h2krjrb.us-east-1.rds.amazonaws.com", "phpmyadmin", "phpmyadmin", "Products");
$Category = $_POST["Category"];
//$sql_query = "select Product_Category from Products where Product_Category like '$Category'";
$sql_query = "select concat(Product_Owner_Email,'_',Product_Name,'_', Product_Key_Code) as productkeyword from table where <Product_Category like '$Category'>";
$result = mysqli_query($conn, $sql_query);
if(mysqli_num_rows($result) > 0 ){
$row = mysqli_fetch_assoc($result);
$Product_Owner_Email = $row['Product_Owner_Email'];
$Product_Name = $row['Product_Name'];
$Product_Key_Code = $row['Product_Key_Code'];
}
mysqli_close($conn);
echo $ProductKeyWord;
?>
How can i concatenate 3 columns with MySQL in PHP?
also
On my app the echo back is ""
...................................................................... ANSWER BELOW............................................................
<?php
ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(E_ALL);//This code is used so if i test the PHP in the browser itll tell me any errors or warnings!!
$conn = mysqli_connect("XXXX", "XXXX", "XXXX", "Products");
$Category = $_POST["Category"];
$sql = "SELECT Product_Owner_Email, Product_Name, Product_Key_Code FROM Product_Details WHERE Product_Category LIKE '$Category'
LIMIT 0 , 30";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["Product_Owner_Email"]. "_" . $row["Product_Name"]. "_" . $row["Product_Key_Code"] . " ";
}
} else {
echo "0 results";
}
$conn->close();
?>
the answer actually returns every "product" in the $Category
You can use the below query:
select concat(Product_Owner_Email,'_',Product_Name,'_', Product_Key_Code) as productkeyword from table where <where condition>
I have updated your code, now try this.
$conn = mysqli_connect("phpmyadmin.cvw71h2krjrb.us-east-1.rds.amazonaws.com", "phpmyadmin", "phpmyadmin", "Products");
$Category = $_POST["Category"];
//$sql_query = "select Product_Category from Products where Product_Category like '$Category'";
$sql_query = "select Product_Owner_Email,Product_Name,concat(Product_Owner_Email,'_',Product_Name,'_',Product_Key_Code) as productkeyword from Products where Product_Category like '".$Category."'";
$result = mysqli_query($conn, $sql_query);
if(mysqli_num_rows($result) > 0 ){
$row = mysqli_fetch_assoc($result);
$Product_Owner_Email = $row['Product_Owner_Email'];
$Product_Name = $row['Product_Name'];
$Product_Key_Code = $row['productkeyword'];
}
mysqli_close($conn);
echo $Product_Key_Code;
?>
Related
I have case manager table where i have inserted court table id as foreign key. i want to fetch record from both tables. when using nested while loop it shows only one row data.
$id = $_SESSION['id'];
$query1 = "SELECT * from `case_manager` where user_id = '$id' ";
$result1 = mysqli_query($conn, "$query1");
while($row = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
$Status = $row['status'];
$id = $row['id'];
$case_type = $row['case_type'];
$court_id = $row['court_id'];
$query2 = "SELECT * from `case_type` where case_id = '$case_type'";
if($result1 = mysqli_query($conn, "$query2")) {
while($row2 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
echo $row2['case_name'];
}
}
}
Because you are overwritting you $result1 change inner query result to $result2 then try
$id = $_SESSION['id'];
$query1 ="SELECT * from `case_manager` where user_id = '$id' ";
$result1 = mysqli_query($conn , "$query1");
while ($row = mysqli_fetch_array($result1 ,MYSQLI_ASSOC)) {
$Status=$row['status'];
$id = $row['id'];
$case_type = $row['case_type'];
$court_id = $row['court_id'];
$query2 ="SELECT * from `case_type` where case_id = '$case_type'";
if($result2 = mysqli_query($conn , "$query2")){;
while ($row2 = mysqli_fetch_array($result2 ,MYSQLI_ASSOC)) {
echo $row2['case_name'];
}
}
}
1st : Because you are overwriting the variable $result1 In second query execution.
if($result1 = mysqli_query($conn , "$query2")){;
^^^^^^^^ ^^
Note : And remove that unnecessary semicolon .
2nd : No need multiple query simple use join
SELECT cm.*,c.* from `case_manager` cm
join `case_type` c
on cm.cas_type=c.case_id
where cm.user_id=$id;
You can use below query to fetch your record:
$query = SELECT case_manager.* ,case_type.case_name FROM case_manager Left JOIN case_type ON case_manager.case_type=case_type.case_id where case_manger.user_id = $id;
While($row = mysql_fetch_array()){
echo $row['case_name'];
}
I would like to take data from DB via simple script
<a href='category.php?CAT=Shoes'>Shoes</a>
then simple show all rows with the specific data in "CAT" column like this:
$CAT = $_GET['CAT'];
$sql = "SELECT * FROM Shop WHERE CAT = $CAT" ;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo"
... results
"}}
The problem is that this script does work with INT (for example SELECT CAT = 5 like category.php?CAT=5) but not with VARCHAR (SELECT CAT = Shoes like category.php?CAT=Shoes). Now I'm not sure why is this happening.
With Error: Trying to get property of non-object
$sql = "SELECT * FROM Shop WHERE CAT = '$CAT'"
You need pass $cat as string
$cat = $_GET['CAT'];
$sql = "SELECT * FROM Shop WHERE CAT = '{$cat}'" ;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<pre>' . print_r($row, true) . '</pre><br/>';
}
}
I want to display hospital names from database by two ways:
By Selecting city.
By entering the name of hospital in search bar.
I wrote below php script. The 1st part works fine and shows all hospitals from a selected city, but 2nd part doesn't work for me. There is no error displayed. Need help with this. Did i not put the 'if' conditions in the right place? Or I missed something else?
if (isset($_POST['search'])) {
if (isset($_POST['search-by-city'])) {
$city_id = $_POST['search-by-city'];
$query = "SELECT * FROM `hospitals` WHERE `City_ID` LIKE '%$city_id%'";
$result = mysqli_query($con,$query);
if (mysqli_num_rows($result) == 0) {
echo '<h2>No recod Found</h2>' ;
}
}
if (isset($_POST['search-by-name'])) {
$hospital_name = $_POST['search-by-name'];
$query = "SELECT * FROM `hospitals` WHERE `Name` LIKE '%$hospital_name%'";
$result = filterTable($query); {
if (mysqli_num_rows($result) == 0) {
echo '<h2>No recod Found</h2>' ;
}
}
while($row = mysqli_fetch_array($result)){
$city_id = $row[3];
$query = "SELECT `Name` FROM `cities` WHERE `ID` LIKE '$city_id'";
$result2 = mysqli_query($con,$query);
$row2 = mysqli_fetch_row($result2);
$city_name = $row2[0];
echo '
<h3>'.$row[1].'</h3>
<h4>'.$city_name.'</h4>
';
}
}
I have a mySQL database table called stock which holds a stock record on items for sale. When querying the database using PHP I have no issue, but when I try to do it through Wordpress it doesn't work.
my PHP is as follows
// Query the stock level
function QueryStock($cat) {
$query = "SELECT * FROM stock";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);
for ($j = 0 ; $j < $rows ; ++$j)
{
$row = mysql_fetch_row($result);
if ($row[0] == $cat) {
$catno=$row[0];
$supplier=$row[1];
$itemname=$row[2];
$category=$row[3];
$price=$row[4];
$stock=$row[5];
}
}
mysql_close($db_server);
echo "There are " . $stock . " bears";
}
$cat in the function attribute is the catalogue number of the item I am querying and the output is "There are 2 bears".
I put this in Wordpress and the webpage hangs. Then through Google searching, I found out about the $wpdb object being used by Wordpress at designmodo.com. So with some code condensing too, I changed it to...
function QueryStock($cat) {
$query = "SELECT * FROM stock WHERE catno = " . $cat;
global $wpdb;
$row = $wpdb->get_row($query, ARRAY_N);
$catno == $row[0];
$itemname = $row[2];
$price = $row[4];
$stock = $row[5];
echo "There are " . $stock . " bears";
}
Now the output is "There are bears" (missing the $stock value). I have been doing loads of Google searching to get where I am and I am having problems understanding it. Where am I going wrong?
Try:
function QueryStock($cat) {
$query = "SELECT * FROM stock WHERE catno = " . $cat;
global $wpdb;
$row = $wpdb->get_row($query);
$catno = $row->catno; //Column name in table
$itemname = $row->itemname; //Column name in table
$price = $row->price; //Column name in table
$stock = $row->stock; //Column name in table
echo "There are " . $stock . " bears";
}
Also, I would advise against using "SELECT *". Try to query only for the columns used. This is better for performance.
Edit: saw a typo. You had "$catno == $row..." in your code. You should only use one "="
Eureka!!!,
It's often something missed, and it was in the MySQL query.
The query should be SELECT * FROM stock WHERE catno = '$cat' making $query = "SELECT * FROM stock WHERE catno = '$cat'";
I have taken on board the "SELECT *" issue #eckes and #rickonline pointed out, went for separate queries and used $wpdb->get_var().
Working coding is..
function QueryStock($cat) {
$query1 = "SELECT catno FROM stock WHERE catno = '$cat'";
$query2 = "SELECT itemname FROM stock WHERE catno = '$cat'";
$query3 = "SELECT price FROM stock WHERE catno = '$cat'";
$query4 = "SELECT stock FROM stock WHERE catno = '$cat'";
global $wpdb;
$catno = $wpdb->get_var($query1);
$itemname = $wpdb->get_var($query2);
$price = $wpdb->get_var($query3);
$stock = $wpdb->get_var($query4);
echo "There are " . $stock . " bears";
}
I'm working on coding a design for a project and have spent the past few days trying to get this to work but cannot wrap my head around how to do it.
I'm trying to get the first 4 results from a database, and print them to the header. For each result, the nav list number needs to increase by one. For example:
<li class="nav_list" id="nav_list1">RESULT 1
<li class="nav_list" id="nav_list2">RESULT 2
Heres what I have so far, but have no idea how I could get it to display correctly. Any help is appreciated!
$query = "SELECT id, title, description, groupid FROM category LIMIT 0, 4";
$result = mysqli_query($dbc, $query)
or die('Error querying database');
while ($row = $result->fetch_row()){
$catID = $row[0];
$catTITLE = $row[1];
}
?>
<ul id="nav_line1">
<?php
for ($i = 1; $i <= 4; $i++) {
print('<li class="nav_list" id="nav_list'.$i.'">catTITLEhere|</li>');
}
$i = 1;
while ($row = $result->fetch_row()) {
echo <<<EOL
<li id="nav_list{$i}"><a href="{$row[0]}"> etc...
EOL;
$i++;
}
Can you try this,
$query = "SELECT id, title, description, groupid FROM category ORDER BY id DESC LIMIT 0, 4";
$result = mysqli_query($dbc, $query)
or die('Error querying database');
echo '<ul id="nav_line1">';
while ($row = $result->fetch_row()){
$catID = $row[0];
$catTITLE = $row[1];
print('<li class="nav_list" id="nav_list'.$catID.'">'.$catTITLE.'|</li>');
}
echo "</ul>";
Try somethin like this:
query = "SELECT id, title, description, groupid FROM category LIMIT 0, 4";
$result = mysqli_query($dbc, $query)
or die('Error querying database');
$i = 1
while ($row = $result->fetch_row()){
$catID = $row[0];
$catTITLE = $row[1];
?><ul id="nav_line1"><?php
print('<li class="nav_list" id="nav_list'.$i.'">'.$catTITLE.'|</li>');
$i++
}