getting fatal error in php - php

I am trying to resolve this issue. But couldnt do it. Please help
I am trying to view all the records in my database, but getting " Fatal error: Call to a member function query() on a non-object ...." and its pointing to
if ($result = $mysqli->query("SELECT ProductName, Description, Price, Size FROM Products ORDER BY ProductID"))
Here is my Complete code
<?php
include('db_connect.php');
**$mysqli = new mysqli("localhost", "netelmbn", "password", "netelmbn");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}**
if ($result = $mysqli->query("SELECT ProductName, Description, Price, Size FROM Products ORDER BY ProductID"))
{
if ($result->num_rows > 0)
{
echo "<table border='1' cellpadding='10'>";
echo "<tr><th>ProductID</th><th>ProductName</th><th>Description</th><th>Price</th><th>Size</th></tr>";
while ($row = $result_fetch_object())
{
echo "<tr>";
echo "<td>" . $row->ProductID . "</td>";
echo "<td>" . $row->ProductName . "</td>";
echo "<td>" . $row->Description . "</td>";
echo "<td>" . $row->Price . "</td>";
echo "<td>" . $row->Size . "</td>";
echo "<td><a href='records.php?ProductID=" . $row->ProductID . "'>Edit</a></td>";
echo "<td><a href='delete.php?ProductID=" . $row->ProductID . "'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "No results to display!";
}
}
else
{
echo "Error: " . $mysqli->error;
}
$mysqli->close();
?>

You must have some database class, check at your code.
$mysqli = new Database();
$mysqli->connect();
before you execute your query:
$result = $mysqli->query("SELECT ProductName, Description, Price, Size FROM Products ORDER BY ProductID");
As you can see, query goes first, then the connection resource. >> So, Connect first.

You need to instantiate your $mysqli obect first. You're also not opening the database anywhere.
You need, at least, something like:
$mysqli = new ClassName( /*some parameters here*/ );
$mysqli->database_open( /* some parameters here */); // or something like this, look at the clas definition

Related

images appearing in warning but not in table?

I am working on a website whereby a load of advertisers are stored in the DB and then displayed to the user by there logo. I know storing directly in to the DB for images is not the done thing, however, I am starting out this way, to get the website running and then will refactor to move to a much more suitable approach.
Currently, I have the following PHP code:
<?php
session_start();
require_once "config.php";
// Create connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SELECT * FROM advertisers";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>advertiser_Name</th>";
echo "<th>advertiser_URL</th>";
echo "<th>advertiser_Category</th>";
echo "<th>advertiser_logo</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['advertiser_id'] . "</td>";
echo "<td>" . $row['advertiser_Name'] . "</td>";
echo "<td>" . $row['advertiser_URL'] . "</td>";
echo "<td>" . $row['advertiser_Category'] . "</td>";
echo "<td>" . $row['<img src="data:image/jpeg;base64,'.base64_encode($row['advertiser_logo']).'"/>'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
However, the images are displayed when called from the DB but they are displayed in the warning message rather than in the table?
<?php
session_start();
require_once "config.php";
// Create connection
if ($link === false)
{
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SELECT * FROM advertisers";
if ($result = mysqli_query($link, $sql))
{
if (mysqli_num_rows($result) > 0)
{
echo "<table>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>advertiser_Name</th>";
echo "<th>advertiser_URL</th>";
echo "<th>advertiser_Category</th>";
echo "<th>advertiser_logo</th>";
echo "</tr>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['advertiser_id'] . "</td>";
echo "<td>" . $row['advertiser_Name'] . "</td>";
echo "<td>" . $row['advertiser_URL'] . "</td>";
echo "<td>" . $row['advertiser_Category'] . "</td>";
echo "<td><img src='data:image/jpeg;base64," . base64_encode($row['advertiser_logo']) . "'/></td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
}
else
{
echo "No records matching your query were found.";
}
}
else
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
The fact that is showing the image in the warning is because you're using a tag with the source as an array key which is not correct.
The array keys, so what is inside the square bracket, is the reference to the array position. If you're familiar with C for example is the 0, 1, ecc.. and not the value itself.
Yes as #NigelRen mentioned this row $row['<img src="data:image/jpeg; looks very bad.
I think you should use:
echo "<td><img src='data:image/jpeg;base64," . base64_encode($row['advertiser_logo']) . "'/></td>";

Why does a PHP file not get included in the file when run by the server?

I have one file called showRelayTeams.php and another called databaseSELECTOperation.php
I am trying to put the one with the database function into the one called showRelayTeams.php
showRelayTeam.php
<?php
$House = $_GET['q'];
$Data = "No Data";
$Query = "SELECT Firstname, Lastname, AgeGroup, Event, Value FROM tblEventEntries WHERE House = '" . $House . "' ORDER BY Value ASC;";
require("http://127.0.0.1/phpscripts/databaseOperations.php");
$Data = databaseSELECTOperation($Query);
$Counter = 0;
if (mysqli_num_rows($Data) > 0) {
echo "<table>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>Age Group</th>";
echo "<th>Event</th>";
echo "<th>Time</th>";
echo "<th>Select?</th>";
echo "</tr>";
while ($Row = mysqli_fetch_assoc($Data)) {
$Counter++;
echo "<tr>";
echo "<td>" . $Row["Firstname"] . " " . $Row["Lastname"] . "</td>";
echo "<td>" . $Row["AgeGroup"] . "</td>";
echo "<td>" . $Row["Event"] . "</td>";
echo "<td>" . $Row["Value"] . "</td>";
echo "<td><input type='checkbox' id='" . $Counter . "'
onclick='boxChecked(this.id)'></td>";
echo "</tr>";
}
}
echo "</table>";
?>
databaseSELECTOperation.php
<?php
//use the SQL SELECT command and return the data
function databaseSELECTOperation($Query) {
//this file will include the host, username, password and the database name
include "http://127.0.0.1/includes/variables.php";
//start a connection to the database using the credentials
$Connection = mysqli_connect($DatabaseHost, $Username, $Password, $DatabaseName);
//if the connection to the database was not successfully made then
if (!$Connection) {
//end the script and then print an error
die("Could not connect to the database: " . mysqli_error());
} //end if
//run the query and put the data returned in to a variable
$DataReturned = mysqli_query($Connection, $Query);
//return the data to the script that called it
return $DataReturned;
}
?>
I get the following error:
Fatal error: Uncaught Error: Call to undefined function databaseSELECTOperation()
When I insert the following code just after the require statement, I get
function not found
<?php
if (function_exists('databaseSELECTOperation')) {
echo "function found.<br />\n";
} else {
echo "function not found<br />\n";
}
?>
Any ideas on how to solve this?
That is the error:
require("http://127.0.0.1/phpscripts/databaseOperations.php");
When you are referring to the file via URL, it is including the output which is already processed by a web server, while you need to include the source PHP code.
Instead, you need to specify the path to the file on the file system like in the following examples:
require('../phpscripts/databaseOperations.php');
require('/var/www/html/phpscripts/databaseOperations.php');
require('C:\inetpub\html\phpscripts\databaseOperations.php');

Selecting fields using variable PHP

how can I select the field using variable. I want to select the field (Seriennummer) in the base column (Seriennummer, MacAdresse).
I need to select meta_key columns, and meta_value where the variable $result is equal to meta_key.
Thanks to everyone for help.
I always get an error
Catchable fatal error: Object of class stdClass could not be converted to string in \wp-content\plugins\woocommerce\templates\checkout\thankyou.php
$result = wpuef_get_field('c2');
$value=$result->value;
echo $value,"<br>";
//Sql
$link = mysqli_connect("localhost", "root", "", "paymentdb");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT Seriennummer, MacAdresse FROM set_top_box WHERE Seriennummer ='".$result."'";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Seriennummer</th>";
echo "<th>MacAdresse</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['Seriennummer'] . "</td>";
echo "<td>" . $row['MacAdresse'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
database picture
this is the result
$result = wpuef_get_field('c2');
$value=$result->value;
echo $value,"<br>";
//Sql
$link = mysqli_connect("localhost", "root", "", "paymentdb");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT Seriennummer, MacAdresse FROM set_top_box WHERE Seriennummer ='".$value=$result->value."'";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Seriennummer</th>";
echo "<th>MacAdresse</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['Seriennummer'] . "</td>";
echo "<td>" . $row['MacAdresse'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);

View detailed MySQL Result when Hyperlink is Clicked

I'm trying to achieve the following. On the homepage a MySQL query returns results from table "parcid"(but limited results). The next step is, I would like to hyperlink a field (preferably "ID") to open a new page using the same ID but returning more detailed results. The code below works but obviously I'm missing something somewhere as it is returning all results rather than only results for the clicked on ID. Any help will be greatly appreciated.
Home url: "http://localhost/"
Pdetail url: "http://localhost/pdetail/"
Query on "Home" page
<?php
// set database server access variables:
$host = "localhost";
$user = "root";
$pass = "";
$db = "wordpress";
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create query
$query = "SELECT * FROM parcid";
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<table cellpadding=1 >";
echo "<tr>";
echo "<td>"."ID"."</td>";
echo "<td>"."City"."</td>";
echo "<td>"."Destination City"."</td>";
echo "<td>"."Weight"."</td>";
echo "<td>"."Length"."</td>";
echo "<td>"."Width"."</td>";
echo "<td>"."Height"."</td>";
echo "<td>"."Type"."</td>";
echo "<td>"."Courier Option"."</td>";
echo "</tr>";
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo '<td>'.$row[0].'</td>';
echo "<td>" . $row[6]."</td>";
echo "<td>" . $row[12]."</td>";
echo "<td>" . $row[14]."</td>";
echo "<td>" . $row[15]."</td>";
echo "<td>" . $row[16]."</td>";
echo "<td>" . $row[17]."</td>";
echo "<td>" . $row[18]."</td>";
echo "<td>" . $row[19]."</td>";
echo "<td>" . $row[20]."</td>";
echo "</tr>";
}
echo "</table>";
}
else {
// no
// print status message
echo "No rows found!";
}
// free result set memory
mysql_free_result($result);
// close connection
mysql_close($connection);
?>
Query on"pdetailed" page.
<?php
// set database server access variables:
$host = "localhost";
$user = "root";
$pass = "";
$db = "wordpress";
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
$ID = $_GET['ID'];
// create query
$query = "SELECT * FROM parcid WHERE ID LIKE '%$ID%'";
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<table cellpadding=1 >";
echo "<tr>";
echo "<td>"."ID"."</td>";
echo "<td>"."CID"."</td>";
echo "<td>"."From"."</td>";
echo "<td>"."Street"."</td>";
echo "<td>"."Suburb"."</td>";
echo "<td>"."Post Code"."</td>";
echo "<td>"."City"."</td>";
echo "<td>"."Province"."</td>";
echo "<td>"."Receiver"."</td>";
echo "<td>"."Destination Street"."</td>";
echo "<td>"."Destination Suburb"."</td>";
echo "<td>"."Destination Postcode"."</td>";
echo "<td>"."Destination City"."</td>";
echo "<td>"."Destination Province"."</td>";
echo "<td>"."Weight"."</td>";
echo "<td>"."Length"."</td>";
echo "<td>"."Width"."</td>";
echo "<td>"."Height"."</td>";
echo "<td>"."Type"."</td>";
echo "<td>"."Courier Option"."</td>";
echo "</tr>";
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>" . $row[0]."</td>";
echo "<td>" . $row[1]."</td>";
echo "<td>" . $row[2]."</td>";
echo "<td>" . $row[3]."</td>";
echo "<td>" . $row[4]."</td>";
echo "<td>" . $row[5]."</td>";
echo "<td>" . $row[6]."</td>";
echo "<td>" . $row[7]."</td>";
echo "<td>" . $row[8]."</td>";
echo "<td>" . $row[9]."</td>";
echo "<td>" . $row[10]."</td>";
echo "<td>" . $row[11]."</td>";
echo "<td>" . $row[12]."</td>";
echo "<td>" . $row[13]."</td>";
echo "<td>" . $row[14]."</td>";
echo "<td>" . $row[15]."</td>";
echo "<td>" . $row[16]."</td>";
echo "<td>" . $row[17]."</td>";
echo "<td>" . $row[18]."</td>";
echo "<td>" . $row[19]."</td>";
echo "<td>" . $row[20]."</td>";
echo "</tr>";
}
echo "</table>";
}
else {
// no
// print status message
echo "No rows found!";
}
// free result set memory
mysql_free_result($result);
// close connection
mysql_close($connection);
?>
If you want just the single row identified by $ID then the query is
$query = "SELECT * FROM parcid WHERE ID = '$ID'";
Also when you know you should only receive a single result row from a query, you do not need to run the mysql_fetch_row($result) in a while loop.
Please dont use the mysql_ database extension, it
is deprecated (gone for ever in PHP7)
Especially if you are just learning PHP, spend your energies learning the PDO or mysqli_ database extensions,
and here is some help to decide which to use
Unless you are using a framework this statement might be wrong
echo '<td>'.$row[0].'</td>';
Maybe it should be
echo '<td>'.$row[0].'</td>';

How to display new rows without refresh using AJAX

I'm actually a little bit confused with all these AJAX techniques. The aim is to display the new rows on my site when new rows are inserted into my database, but i dont know how. The following code has been inserted into my Joomla site threw an extension:
<!-- You can place html anywhere within the source tags -->
<script language="javascript" type="text/javascript">
// You can place JavaScript like this
</script>
<?php
$today = date("d/m/Y");
$sql = "SELECT * FROM queue WHERE";
$sql =$sql . " Date>='$today' ORDER BY Qnumber Desc LIMIT 3";
// echo $sql;
$con=mysqli_connect("localhost","root","db","pass");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Αριθμός Εξυπηρέτησης</th>
<th>Πελάτες σε Αναμονή</th>
<th>Ώρα</th>
<th>Ημερομηνία</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Qnumber'] . "</td>";
echo "<td>" . $row['WaitingCustomers'] . "</td>";
echo "<td>" . $row['Time'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>

Categories