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!!!
Related
I am trying to get the data from mySql. In mySql data is stored from wordpress. and I also want to convert in json format but wordpress functions the_content() is not working.
I am working in php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM othpk_posts where post_type='product' AND post_status='publish'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$data =array();
while($row = mysqli_fetch_array($result)) {
array_push($data, array('id' => $row['ID'], 'productName' => $row['the_title()'], 'productContent'=>$row['the_content() ']));
}
$json = json_encode($data);
echo $json;
} else {
echo "0 results";
}
$conn->close();
?>
You are using WordPress functions to access the database columns instead of using the column name since you are working from a separate PHP file that is not attached to WordPress
To get your query correctly your code should be like so:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM othpk_posts where post_type='product' AND post_status='publish'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$data =array();
while($row = mysqli_fetch_array($result)) {
//We use the column name below not WordPress function
array_push($data, array('id' => $row['ID'], 'productName' => $row['post_title'], 'productContent'=>$row['post_content']));
}
$json = json_encode($data);
echo $json;
} else {
echo "0 results";
}
$conn->close();
?>
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.
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql="SELECT Nr FROM AlleTransaktionen1";
$my_money = 0;
if ($conn->multi_query($sql) === TRUE) {
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo"$row[Nr]";
}
echo "$my_money";
} else {
echo"Fehler: " . $sql . "<br>" . $conn->error;
}
$conn->close();
I get the error:
Call to a member function fetch_assoc() on a non-object in...
<?php
$conn = new mysqli($servername, $username, $password, $dbname);
/* check connection */
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $conn->connect_error);
exit();
}
$sql = "SELECT Nr FROM AlleTransaktionen1";
$my_money = 0;
if ($result = $conn->query($sql)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
printf ($row["Nr"]);
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
?>
<?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.
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.