Get rows from database - php

I have a connection where i want to get some data from my database.
I have inserted some data but now i want to retreive it but i get NULL.
I have no idea why.
<?php
require "connect.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 tbltemperature ORDER BY time DESC LIMIT 1";
$result = $conn->query($sql);
var_dump($results);
$t = 0;
while($row = $result->fetch_assoc()) {
$weather[] = array(
$row["time"],
$row["inside_temperature"]
);
echo $row["time"];
echo $row["inside_temperature"];
}
$conn->close();
?>

check var_dump($results); , it looks like it should be var_dump($result);
otherwise you should check $result->num_rows() first to know if there is any row available.

Firstly you have to check your query in phpmyadmin query is working or not. If working you should try to var_dump($result); and after
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> time: ". $row["time"]. " - inside_temperature: ". $row["inside_temperature"]."<br>";
}
} else {
echo "0 results";
}

Related

Why is this php mysqli script giving me no results?

I have been working with this script for a while and I do not understand why it is giving me no results when the username and password exist. I have checked the capitalization and everything. I have attempted to edit my script to work with this link but alas no results. Here is a screenshot of the database user:. Thanks for your help!
$sql = "SELECT Status FROM Users WHERE Username = '".$_GET["username"]."' AND Password = '".$_GET["password"]."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row or remove while loop if you wish
while($row = $result->fetch_assoc()) {
echo $row['Status'] ;
}
} else {
echo "0 results";
}
$conn->close();
Edit: I also tried this code, which gave me the error "Getting property of non-object on line 16 (which here is line 9):
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Status FROM Users WHERE Username = '".$_GET["username"]." AND Password = '".$_GET["password"]."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row or remove while loop if you wish
while($row = $result->fetch_assoc()) {
echo $row['Status'] ;
}
} else {
echo "0 results";
}
$conn->close();
?>
Edit: I did fix my SQL injections problem, thanks for the advice!
I tried your code as followed, Its working on my machine. Can you make sure of the $conn object creation. Also please enable the errors if not to see whats going wrong.
$conn = new mysqli('localhost','user','password','db');
$sql = "SELECT Status FROM Users WHERE Username = '".$_GET["username"]."'
AND Password = '".$_GET["password"]."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row or remove while loop if you wish
while($row = $result->fetch_assoc()) {
echo $row['Status'] ;
}
} else {
echo "0 results";
}
$conn->close();

I can't get any data output from the database, i only get "0 results" as the else statement, what's the case?

I'm having some troubles with fetching some database information with my php code.
All I'm getting is this message: "Connected successfully0 results".
Here's my code guys, thanks for the help in advance.
<?php
$servername = "example";
$username = "example1";
$password = "example2";
$row = array();
$conn = new mysqli($servername,$username,$password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "Select Distinct subject from mobile_math_science_toc";
$result = mysqli_query($conn, $sql);
if ($result = $conn->query($sql)) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "Subject " ,$row["subject"];
}
} else {
echo "0 results ";
}
mysqli_close($conn);
?>
You should add dbname while creating your connection to database. You can use mysqli_num_rows function to count no. of rows.
<?php
$servername = "example";
$username = "example1";
$password = "example2";
$dbname = "your_db_name"; // Specify your db-name here.
$conn = new mysqli($servername,$username,$password,$dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "Select Distinct subject from mobile_math_science_toc";
$result = mysqli_query($conn, $sql);
// Checking if there are some records available.
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "Subject " ,$row["subject"];
}
} else {
echo "0 results ";
}
mysqli_close($conn);
?>
Change
$conn = new mysqli($servername,$username,$password);
To
$conn = new mysqli($servername,$username,$password, "<your database name>");
And
$result = mysqli_query($conn, $sql);
if ($result = $conn->query($sql)) {
}
To
$result = $conn->query($sql);
if ($result) {
}
Try
if (mysqli_num_rows($result) > 0) {
While checking you got the data or not

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.

SELECT COUNT if statement

Ok, so it seems like my SELECT count query doesn't work here:
<?php
$servername = " ";
$username = " ";
$password = " ";
$dbname = " ";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST['submitted1'])) {
$result= mysqli_query("SELECT COUNT (Motspillerid)
FROM SESSION
WHERE Motspillerid= 3 ");
echo $result ;
} else {
echo "Wrong" ;
}
?>
And when I press submitt nothing happens, I don't get any error message and I don't get the result. So it's something wrong with the SELECT query I guess.
I'm noob I know, I'm new to this.
:)
You're not calling mysqli_query() correctly, the first argument has to be the database connection object returned by mysqli_connect.
And after performing the query, you have to fetch the results using one of the mysqli_fetch_X() functions.
if (isset($_POST['submitted1'])) {
$result= mysqli_query($conn, "SELECT COUNT(*) AS count
FROM SESSION
WHERE Motspillerid= 3 ");
$row = mysqli_fetch_assoc($result);
echo $row['count'];
} else {
echo "Wrong" ;
}

PHP SELECT * FROM not working

I am trying to query data from a table using the following script:
//connect file
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//connect file included above - ECHO tested and it is connected as $conn
$sql = "SELECT * FROM userInfo";
$results = $conn->query($sql);
if (!$results) {
printf("Errormessage: %s\n", $conn->error);
exit;
} else {
echo $row['username'];
}
UPDATE --
It now no longer tries to throw an error and seems to go to the else section; however, no echo - and the spelling is correct this time and the column is filled.
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["username"];
}
} else {
echo "0 results";
}
This now returns results. Thank you #Fred -ii- especially for your help on this.
Also thanks #jjczopek for the error checking advice!

Categories