Counting output repeat - php

<?php
// public trailer config
// trailer-config.php
$title = "What_I_am_looking_for";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// select statement
$sql = "SELECT * from SEARCHTABLE where WHAT-I-AM-LOOKING-FOR ='$title' ";
// result from select statement in groups of 4
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$count=0;
while($row = $result->fetch_assoc()){
$count++;
if ($count % 4 == 0) {
echo 'NOT FIRST RECORD';
} else {
echo 'FIRST RECORD';
}
}
} else {
echo '0 results',"\n";
}
?>
I need the output to be FIRST-RECORD the 3 NOT-FIRST-RECORDS then repeat. Any help would be appreciated.

Related

DB query failed in php

the php code does not return any results from the database
$conn = new mysqli($host, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM pelicula";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
array_push($result_array, $row);
}
}
echo json_encode($result_array);
$conn->close();
$conn = new mysqli($host, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM pelicula";
$result = $conn->query($sql);
$result_array=[];
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
array_push($result_array, $row);
}
}
echo json_encode($result_array);
$conn->close();`enter code here`
The $result_array had to be created and instantiate before using it with the array_push function!!!

How to return the results of my query in rows

This is my query, and it is working fine, but how do I have the results returned in an HTML TABLE. for example I want the first 5 results on row 1 the next 5 on row 2 and so on. Any help would be great. Thank you!
<?php
$servername = "XXX";
$username = "XXX";
$password = "XXX";
$dbname = "XXX";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM manufacturer LIMIT 10 OFFSET 0";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<td><a href="index.php?route=product/manufacturer/info&
manufacturer_id='. $row["manufacturer_id"] .'"><img src="image/' .
$row["image"] .'"></a></td>';
}
} else {
echo "0 results";
}
$conn->close();
<?php
$servername = "XXX";
$username = "XXX";
$password = "XXX";
$dbname = "XXX";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn- >connect_error);
}
$sql = "SELECT * FROM manufacturer LIMIT 10 OFFSET 0";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$i=0;
while($row = $result->fetch_assoc()) {
If($i==5){
echo "</tr>";
echo "<tr>";
$i=0;
}
echo '<td><a href="index.php? route=product/manufacturer/info&
manufacturer_id='. $row["manufacturer_id"] .'">. <img src="image/' .
$row["image"] .'"></a></td>';
$i++;
}
} else {
echo "0 results";
}
$conn->close();

PHP - MySQL - for Loop not behaving

Could anyone explain to me why the first DBcall function returns values, but the 2nd one does not...?
for ($i=0; $i <= 18 ; $i++) {
echo $sql = 'SELECT a FROM table_a WHERE id ="'.$a[$i].'"';
echo "<br>";
$g = dbCall($servername, $username, $password, $dbname,$sql)[0];
echo $g."<br>";
echo $sql = 'SELECT bFROM table_b WHERE id ="'.$a[$i].'"';
echo "<br>";
$h = dbCall($servername, $username, $password, $dbname,$sql)[0];
echo $h ."<br>";
}
`
Heres the function:
function dbCall($servername, $username, $password, $dbname, $sql) {
$item = [];
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_array($result)) {
$item[] = $row[0];
}
} else {
$item[] = "0 results";
}
$conn->close();
return $item;
}
`
And the results:
SELECT a FROM table_a WHERE id ="1234567"
72
SELECT b FROM table_b WHERE id ="1234567"
0 results
Shouldn't they return theri respective values?
I'm

how can I display all the data from database using an array

I'm trying to connect $countries to an array that will display all the data from my database, it only displays the last data I entered.Is there anyway I can display all of them?
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT product_id, product_name , quantity FROM inventory";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$tom = array(" ". $row["quantity"] . $row["product_name"]);
$countries = $tom;
}
} else {
echo "0 results";
}
$conn->close();
because you have assinged data to $countries with wrong manner:
$countries = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$tom = array(" ". $row["quantity"] . $row["product_name"]);
$countries[] = $tom; // use []
}
} else {
echo "0 results";
}
$countries[] = $tom;
With [] at the end of inicialization of variable you add a row to that array. If you do it without [], it's just inicialization every time in while loop.
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT product_id, product_name , quantity FROM inventory";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
$countries[] = " ". $row["quantity"] . $row["product_name"];
} else {
echo "0 results";
}
$conn->close();
// create connection
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli($servername, $username, $password, $dbname);
// get the data
$sql = "SELECT product_id, product_name, quantity FROM inventory";
$countries = $conn->query($sql)->fetch_all();
Although it's only available with mysqlnd installations, you need one, because without mysqlnd mysqli is unusable anyway.

Php Header Function Not Working In My Website [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 7 years ago.
Im working on a reservation system deep dive and im stuck on the header function not redirecting my user to the 'index.php' page. Here's my PHP code.
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM customer WHERE transactionum = '$ticket' ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<strong>".$row["fname"]. " " . $row["lname"]."</strong><br>";
}
} else {
header('location:index.php');
}
?></li>
<li class="list-group-item">Paid: <?php if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM customer WHERE transactionum = '$ticket' ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<strong>".$row["paid"]. "</strong><br>";
}
} else {
header('location:index.php');
}
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM customer WHERE transactionum = '$ticket' ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$test1 = "<strong>".$row["fname"]. " " . $row["lname"]."</strong><br>";
}
} else {
header('location:index.php');
}
?></li>
<li class="list-group-item">Paid: <?php if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM customer WHERE transactionum = '$ticket' ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$var1 = "<strong>".$row["paid"]. "</strong><br>";
}
} else {
header('location:index.php');
}
echo $test1;
echo $var1;
Look this url http://php.net/manual/es/function.header.php
Use capital Letter
header('Location: http://www.example.com/');
Try to use this form and make sure to your code 'else' execution.

Categories