Why is this php mysqli script giving me no results? - php

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();

Related

Database Connected, but no data found

I have created this page, I have put dummy content into MySQL database, I have ran the same query in phpMyAdmin and it brings back data, yet on the web page it is bringing up "No Data".
Have I missed something? (obviously blanked out database details)
<?php
// Connection Info
$host ='localhost';
$user ='-----';
$pass ='-----';
$dbname ='-----';
$conn = new mysqli($host, $user, $pass, $dbname);
if($conn->connection_errno){
echo "Connection Failed" . $conn->connect_error;
exit();
}
?>
<?php
$query = "SELECT * FROM posts";
$result = $conn->query($query);
if($results->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row['title'];
}
echo"We Have Data";
} else {
echo"No Data";
}
?>
Change the following line
if($results->num_rows > 0){
to
if($result->num_rows > 0){
He typo mistake friend
Make following changes in to your code
From
if($results->num_rows > 0){
To
if($result->num_rows > 0){

Get rows from database

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";
}

This page contains the following error: error on line 1 at column 1: Document is empty

Im unable to find out the solution , please help. below is the code. Thanks in advance
<?php
require_once('connect.php');
$sql = "select * from projet";
$result = $conn->query($sql);
$xml = new SimpleXMLElement('<xml/>');
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$mydata = $xml->addChild('mydata');
$mydata->addChild('Id',$row['idProjet']);
}
} else {
echo "0 results";
}
$conn->close();
header ("Content-Type:text/xml");
echo($xml->asXML());
?>
and the file connect.php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mtocrowdrise";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "connected succesfully";
Meanwhile i keep getting this error:
This page contains the following errors:
error on line 1 at column 1: Document is empty
Below is a rendering of the page up to the first error.
You shouldn't be writing/outputting any HTML to the page when said page is being cast as an XML Document (header ("Content-Type:text/xml");).
Remove echo "connected succesfully"; from connect.php.
You'll also (eventually) get the same error if:
...
} else {
echo "0 results";
}
...
header ("Content-Type:text/xml");
satisfies. So you should only cast the document to XML if there are no errors and there is actually some XML to display.
Something like the following would only set the Document to XML if there are results to display (per your original code):
require_once('connect.php');
$sql = "select * from projet";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$xml = new SimpleXMLElement('<xml/>');
// output data of each row
while($row = $result->fetch_assoc()) {
$mydata = $xml->addChild('mydata');
$mydata->addChild('Id',$row['idProjet']);
}
header ("Content-Type:text/xml");
echo($xml->asXML());
} else {
echo "0 results";
}
$conn->close();

first php query error

I'm trying to write my first PHP query and I'm getting the following
error:
Notice: Trying to get property of non-object in
D:\Home\web\username\helloworld.php on line 36.
What am I doing wrong?
Code:
<?php
ini_set('display_errors', 1);
error_reporting(~0);
$servername = "myserver.com";
$username = "myusername";
$password = "mypassword";
$dbname = "mydbname";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM books";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Okay, so your code looks like this:
$sql = "SELECT * FROM books";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. "<br>";
}
} else {
echo "0 results";
}
The Problem lies in $result->num_rows > 0
Apparently you have an Error in your query so your $result is a boolean instead of an Object, so the property num_rows doesnt exist. A better solution would be:
if($result !==false){
//handle your result here
}else{
echo "An error appeared: ".$conn->error;
}
Your syntax and logic is correct but the query is not executing successfully. Try putting an if condition before the if ($result->num_rows > 0) condition to check that.
Also, I was able to reproduce the error by misspelling the name of the table. So, are you sure that the table books exists in your database? Correcting that will solve it.
Your query has a problem with it. Have you got a table called books? Did you spell it wrong?
Your code: if ($result->num_rows > 0) { is causing the problem because the query didn't return a success. So how can it get the num_rows?
You want to check for a MySql error before you run that if statement so try this final code and find out what the error is:
<?php
ini_set('display_errors', 1);
error_reporting(~0);
$servername = "myserver.com";
$username = "myusername";
$password = "mypassword";
$dbname = "mydbname";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM books";
$result = $conn->query($sql);
if($result !== false){
//query was a success!
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. "<br>";
}
} else {
echo "0 results";
}
}else{
//your query had an error so lets display it:
echo "Query Error: ".$conn->error;
}
$conn->close();
?>
What is the Error you now get printed out?

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