Simple MySQL echo - "Trying to get property of non-object" - php

I'm trying to figure out why I'm receiving this error when trying to echo data from a database table, but can't exactly see what I'm doing.
$query = "SELECT * from web_projects"; // Select all rows from web_projects table
$result = mysqli_query ($con,$query);
while ($row = mysqli_fetch_array ($result)) {
foreach($row as $web) {
echo "<p>Name: ".$web->name."</p>";
echo "<p>Technologies: ".$web->tech."</p>";
echo "<p>Description: ".$web->description."</p>";
}
}
Theres only one row in the table, but when compiled I'm getting this:
Notice: Trying to get property of non-object in
/Users/leecollings/Sites/leecollings.co/index.php on line 31 Name:
Notice: Trying to get property of non-object in
/Users/leecollings/Sites/leecollings.co/index.php on line 32
etc
Have I missed something glaringly obviously?

I would remove foreach line, and use array accessing:
$query = "SELECT * from web_projects"; // Select all rows from web_projects table
$result = mysqli_query ($con,$query);
while ($row = mysqli_fetch_array ($result)) {
echo "<p>Name: ".$row['name']."</p>";
echo "<p>Technologies: ".$row['tech']."</p>";
echo "<p>Description: ".$row['description']."</p>";
}

Error message is very clear: $web is not object. Read the manual more thoroughly: mysqli_fetch_array() returns array or NULL, not object.

You are fetching an array, but you are trying to echo an object. Use this instead:
echo "<p>Name: ".$web['name']."</p>";
Full edited code:
$query = "SELECT * from web_projects"; // Select all rows from web_projects table
$result = mysqli_query ($con,$query);
while ($row = mysqli_fetch_array ($result)) {
echo "<p>Name: ".$row['name']."</p>";
echo "<p>Technologies: ".$row['tech']."</p>";
echo "<p>Description: ".$row['description']."</p>";
}

Related

How to convert an object of class mysqli_result to a json object

With the php code below, I am trying to select a record from my database. When I run the code I get this error:
Catchable fatal error: Object of class mysqli_result could not be
converted to string
What I want to achieve is to convert the result into a jSON object, but instead I get this error.
<?php
session_start();
include_once 'db/dbconnect.php';
$var = $_GET['name'];
// echo $var;
$json = [];
$sql = "SELECT * from recipes WHERE recipes.recipeName = '.$var'";
$rslt = mysqli_query($con,$sql);
echo $rslt;
?>
you will need to iterate over the results, since mysqli returns one row at a time:
$sql = "SELECT * from recipes WHERE recipes.recipeName = '$var'";
$rslt = mysqli_query($con,$sql);
while($row = mysqli_fetch_assoc($rslt)){
print_r($row);
}
or, to JSON it:
$json = array();
while($row = mysqli_fetch_assoc($rslt)){
$json[] = $row;
}
echo json_encode($json);
mysqli_fetch_assoc returns the row as a keyd array - http://php.net/manual/en/mysqli-result.fetch-assoc.php
as to SQL injection defence, use mysqli_real_escape_string (http://php.net/manual/en/mysqli.real-escape-string.php) like:
$var = mysqli_real_escape_string($con,$_GET['name']);

Printing a MySQL query in PHP

function MattsScript()
{
$query = 'SELECT * FROM `ACCOUNTING` WHERE `ACCTSTATUSTYPE` = "start" AND `Process_status` IS NULL LIMIT 0,100';
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
while ($row = mysql_fetch_assoc($result))
{
echo $row['USERNAME'] . "<br />";
echo $row['ACCTSTATUSTYPE'];
}
}
I am trying to echo the results of a query. What I think is happening here is I am saving a query to a variable, the first 100 results (LIMIT 0,100) then using a loop to echo each row to the page.
However nothing is happening, no error and nothing written to the page.
Is there something I am missing?
if you are expecting only one result remove the while loop if not leave the while loop and remove the line $row = mysql_fetch_assoc($result); before the while loop. Also make sure you are querying your database correctly.
Example: $result = mysql_query($query) or die(mysql_error());

PHP - using labels instead of indexes in an array

I am fetching a row from a database using something like this
<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
echo $row[0]; // 42
echo $row[1]; // the email value
?>
This example shows accessing $row using an index, like $row[0], but this is a path to error. I would like to do it like $row['id'] or $row['email']... how is that possible?
Use mysql_fetch_assoc instead of mysql_fetch_row:
$row = mysql_fetch_assoc($result);
(That gives you the "associative array", so that you can fetch columns by their name.)
You can use mysql_fetch_assoc() instead of mysql_fetch_row()

Figuring out why I am getting a Resource ID #5 error

This is a part of my code, and the echo is to test the value and it gives me Resource ID #5
$id = mysql_query("SELECT id FROM users WHERE firstname='$submittedfirstname' AND lastname='$submittedlastname' AND email='$submittedemail'") or die(mysql_error());
$counter = mysql_num_rows($id);
echo $id;
I am just getting into programming, and lately seeing lot of Resource ID outputs/errors while working with Databases.
Can someone correct the error in my code? And explain me why it isnt giving me the required output?
This is not an error. This is similar to when you try to print an array without specifying an index, and only the string "Array" is printed. You can access the actual data contained within that resources (which you can think of as a collection of data) using functions like mysql_fetch_array().
In fact, if there were an error here, the value of $id would not be a resource. I usually use the is_resource() function to verify that everything is alright before using variables which are supposed to contain a resource.
I guess what you intend to do is this:
$result = mysql_query("SELECT id FROM users WHERE firstname='$submittedfirstname' AND lastname='$submittedlastname' AND email='$submittedemail'") or die(mysql_error());
if(is_resource($result) and mysql_num_rows($result)>0){
$row = mysql_fetch_array($result);
echo $row["id"];
}
Did you mean to echo $counter? $id is a resource because mysql_query() returns a resource.
If you are trying to get the value of the id column from the query, you want to use e.g., mysql_fetch_array().
Here is an excerpt from http://php.net/mysql.examples-basic:
$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
Adapted to the code you provided, it might look something like this:
$result =
mysql_query("SELECT id FROM users WHERE firstname='$submittedfirstname' AND lastname='$submittedlastname' AND email='$submittedemail' LIMIT 1")
or die(mysql_error());
if( $row = mysql_fetch_array($result, MYSQL_ASSOC) )
{
$id = $row['id'];
}
else
{
// No records matched query.
}
Note in my code that I also added LIMIT 1 to the query, as it seems like you are only interested in fetching a single row.
are you looking for
while ($row = mysql_fetch_array($id)) {
echo $row['id'];
}
?
$kode_gel = substr($_GET['gel'],0,3);
$no_gel = substr($_GET['gel'],3,5);
$cek = mysql_query("SELECT id_arisan
FROM arisan WHERE kode_gel = '".$kode_gel."'
AND no_gel = '".$no_gel."'");
$result = mysql_fetch_array($cek);
$id = $result['id_arisan'];
header("location: ../angsuran1_admin.php?id=".$id);

Getting "supplied argument is not a valid MySQL result resource" when calling mysql_fetch_array()

I keep getting the same mysql error code but I dont know how to correct it.
Error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /websites/123reg/LinuxPackage22/we/ez/y_/weezy.co.uk/public_html/search.php on line 29
I have marked out line 29 on the code below.
What does this mean?
Thanks
<?php
// Change the fields below as per the requirements
$db_host="";
$db_username="";
$db_password="";
$db_name="";
$db_tb_name="data";
$db_tb_atr_name="name";
$db_tb_atr_name="email";
$db_tb_atr_name="location";
//Now we are going to write a script that will do search task
// leave the below fields as it is except while loop, which will display results on screen
mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");
$query=mysql_real_escape_string($_GET['query']);
$query_for_result=mysql_query("
SELECT name, email, location
FROM data WHERE
$db_tb_atr_name like '%".$query."%'");
echo "<h2>Search Results</h2><ol>";
while ($row = mysql_fetch_array($result)) <<<<<<<<<<<<<<< LINE 29
{
echo "<li>";
echo substr($row["name"], $row["email"], $row["location"]);
echo "</li><hr/>";
}
echo "</ol>";
mysql_close();
?>
while ($row = mysql_fetch_array($result))
Where have you set $result to anything?
You probably meant
while ($row = mysql_fetch_array($query_for_result))
you store the result of your query in a variable named $query_for_result, later on you try to read $result, which is empty.
You're assigning the result of mysql_query to $query_for_result but then trying to loop over $result in mysql_fetch_array(). Try this:
$result = mysql_query("...");
while ($row = mysql_fetch_array($result)) {
...
OR
$query_for_result = mysql_query("...");
while ($row = mysql_fetch_array($query_for_result)) {
...
You should also check if there are any rows returned
$rs = mysql_query("
SELECT name, email, location
FROM data
WHERE ".$db_tb_atr_name." like '%".$query."%'
");
if(mysql_num_rows($rs)){
while ($row = mysql_fetch_array($rs)){
// code
}
} else {
// no results
}

Categories