MySQL query results - php

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++
}

Related

How to fetch single row data from php?

for ($i=$start; $i<$start+$scale && $i < $total_record; $i++)
{
$sql = "select * from memo where num = ?";
$stmh = $pdo->prepare($sql);
//mysql_data_seek($result, $i);
$row = mysql_fetch_array($result);
$sql2 = "select * from phptest.memo order by num desc";
$stmh2 = $pdo->query($sql2);
$stmh2->execute();
//$row = $stmh2->fetch(PDO::FETCH_ASSOC);
$row = $stmh->fetchColumn($i-1);
$memo_id = $row['id'];
$memo_num = $row['num'];
$memo_date = $row['regist_day'];
$memo_nick = $row['nick'];
$memo_content = $row['content'];
Hi guys i want fetch single row data by using PDO method instead of $row = $stmh2->fetch(PDO::FETCH_ASSOC); like mysqli_data_seek($result,$i);. What should i do?
$sql2 = "select * from phptest.memo order by num desc";
$stmh2 = $pdo->query($sql2);
$stmh2->execute();
while($r = $stmh2->fetch()) {$row[] = $r;}
Now $row[$i] should be getting you the same results as mysqli_data_seek($result,$i) would have.
IE: $row[0] would return the first row.

concatenate 3 columns with MySQL in PHP

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;
?>

mysql query skips first row

$sqlList ="select title from podcast order by date_pd desc limit 5";
$rslt2 = mysqli_query($conn, $sqlList) or die ("Fail".mysqli_error($conn));
$record2 = mysqli_fetch_array($rslt2);
$nRows = mysqli_num_rows($rslt2);
for ($i=0; $i<$nRows; $i++) {
$record2 = mysqli_fetch_array($rslt2);
echo $record2["title"];
}
the echo result is skipping the first row of the query, what am I doing wrong?
You are loosing the first row of your result set because you read the first row and throw it ways/ignore it
$sqlList ="select title from podcast order by date_pd desc limit 5";
$rslt2 = mysqli_query($conn, $sqlList) or die ("Fail".mysqli_error($conn));
// this line gets the first row of your result set
// and you just ignore it therefore throw it away
$record2 = mysqli_fetch_array($rslt2);
$nRows = mysqli_num_rows($rslt2);
for ($i=0; $i<$nRows; $i++) {
$record2 = mysqli_fetch_array($rslt2);
echo $record2["title"];
}
As #Anant says, you are better advised to use a while as if the result set is empty, it just does nothing. Admittedly not always an advantage.
$sqlList ="select title from podcast order by date_pd desc limit 5";
$rslt2 = mysqli_query($conn, $sqlList) or die ("Fail".mysqli_error($conn));
while ( $record2 = mysqli_fetch_array($rslt2) ) {
echo $record2["title"];
}
remove the line $record2 = mysqli_fetch_array($rslt2);, which is placed before the for loop.
new code would be:
$sqlList ="select title from podcast order by date_pd desc limit 5";
$rslt2 = mysqli_query($conn, $sqlList) or die("Fail".mysqli_error($conn));
$nRows = mysqli_num_rows($rslt2);
for ($i=0; $i<$nRows; $i++) {
$record2 = mysqli_fetch_array($rslt2);
echo $record2["title"];
}

PHP MySQL How can I paginate within two while loop

I would like to know how can I do a LIMIT 0,10 in this case (in order to do a pagination later) because when I do this I obtain a total of 30 results (10 results for each result of the first loop) which is logical, any idea ?
$select = "SELECT X FROM Y"
$result = mysql_query($select,$link);
$total = mysql_num_rows($result);
while($row = mysql_fetch_array($result)) {
$Name = $row['X'];
$select2 = "SELECT id FROM `".$Name."` LIMIT 0,30 ";
$result2 = mysql_query($select2,$link) or die ('Erreur : '.mysql_error() );
$total2 = mysql_num_rows($result2);
while($row2 = mysql_fetch_array($result2)) {
echo $row2['id']
}
}
Thanks in advance for your help !

trying to show a total from mysql in php

I am trying to get the qunt with are int and add them all up and show a total
$qunt = 0;
$result = mysql_query("SELECT *
FROM properties_items
WHERE user_propid='$view'
ORDER BY id DESC") or die (mysql_error());
while ($row = mysql_fetch_array($result)) {
$itemid = $row['itemid'];
$qunt = $row['qunt'];
$qunt++;
}
echo $qunt;
$qunt = 0;
$result = mysql_query("SELECT *
FROM properties_items
WHERE user_propid='$view'
ORDER BY id DESC") or die (mysql_error());
while ($row = mysql_fetch_array($result)) {
$itemid = $row['itemid'];
$qunt += $row['qunt'];
}
echo $qunt;
Dare I say that you probably aren't using the itemid, in which case there's no point in looping through a result set in code. Instead, try something like this:
$qunt = mysql_query "SELECT SUM(qunt) FROM properties_items WHERE user_propid='$view'";
$qunt += $row['qunt'];
And get rid of the ++ line.

Categories