first php query error - php

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?

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

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

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

not able to show all records in my database

i am testing my database by executing some MySQLi statements
in this case : i want to display all the records of 2 specefic rows (name,score)
i checked how to do such thing in PHP , and i did it
problem is , the page is not showing anything at all , (blank empty page)
My code :
<?php
$servername = "sql3.freesqldatabase.com";
$username = "MY USERNAME";
$password = "MY PASSWORD";
$dbname = "MY DBNAME";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name,score FROM Scores");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Name: " . $row["name"]. " " . $row["score"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
and , i executed the same query in phpMyAdmin Control Panel , and it worked
What have i done wrong ?
This line
$sql = "SELECT name,score FROM Scores");
Should be
$sql = "SELECT name,score FROM Scores";
This syntax error will cause an error and your environment is likely suppressing errors/warnings.

Connecting PHP to mySQL and retrieving data

i am new to PHP, and i am attempting to create a simple connection from html to mySQL using php. I met with some problems when running my codes.
this is my code:
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT username FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["userid"]. ;
}
} else {
echo "0 results";
}
$conn->close();
?>
after running on a browser, this is displayed:
connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT username FROM users"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "
id: ". $row["userid"]. ; } } else { echo "0 results"; } $conn->close(); ?>
Do you have mysql running on your localhost machine? You must verify that it is working first before you can connect via php. Also, make sure you have TCP/IP sockets open in mysql and to make sure it isn't just listening via unix sockets.
echo "<br> id: ". $row["userid"]. ;
this is a syntax error, no need to end it with a full stop.also the correct syntax to connect to sql server is
mysqli_connect("localhost","my_user","my_password","my_db") or die("");
Debug the code before posting it here.
maybe try testing it with a try catch statement. Its what I've done. this way you can display your error messages a little more nicely. As for the cause, PressingOnAlways is probably right
If you are using wampserver or any other server you need to put your php files into c:\wamp\www folder and run them in browser by typing localhost/nameofyourfile.php (change c:\wapm\www for your server installation path or type)
you have the syntax error in blow line
echo "<br> id: ". $row["userid"]. ;
. (dot) should not be there.
second you fetch usernamestrong text by following query
$sql = "SELECT username FROM users";
but you try to get userid
echo "<br> id: ". $row["userid"]. ;
try below code.
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "databasename";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT usersname FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["usersname"] ;
}
} else {
echo "0 results";
}
$conn->close();
?>

Categories