I'm trying to create a class for my database: class structure as below
<?php
require_once("config.php"); // include(Username, passowrd, server and databaseName)
class MySQLDatabase{
private $connection;
function __construct(){
$this->openConnection();
}
public function openConnection(){
$this->$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PWD, DB_NAME);
if(!$connection){
die("Database connection failed: ".mysql_error);
}
}
}
$database = new MySQLDatabase();
$db =&$database;
?>
then my first step was check this weather work or not. so I code index.php file as below
<?php
require("../includes/database.php"); if(isset($database)){
echo "true";
}else{
echo "false";
}
?>
after that I run the program and I got errors as below. I can not understand the reason. I google it and there are no clear solution for this. is there are anyone can help me to understand why this happen and how to solve this?
( ! ) Notice: Undefined variable: connection in C:\wamp\www\waytoits\includes\database.php
on line 9
Call Stack
# Time Memory Function Location
1 0.0005 240792 {main}( ) ..\index.php:0
2 0.0008 249872 require( 'C:\wamp\www\waytoits\includes\database.php' ) ..\index.php:3
3 0.0013 250528 MySQLDatabase->__construct( ) ..\database.php:55
4 0.0013 250592 MySQLDatabase->openConnection( ) ..\database.php:15
( ! ) Fatal error: Cannot access empty property in C:\wamp\www\waytoits\includes\database.php on line 9
Call Stack
# Time Memory Function Location
1 0.0005 240792 {main}( ) ..\index.php:0
2 0.0008 249872 require( 'C:\wamp\www\waytoits\includes\database.php' ) ..\index.php:3
3 0.0013 250528 MySQLDatabase->__construct( ) ..\database.php:55
4 0.0013 250592 MySQLDatabase->openConnection( ) ..\database.php:15
You are using $this->$connection instead of $this->connection . Remove the $ sign in connection and try again.
$this::$connection (with the $ preserved) would refer to a static variable. Because you have a class variable you should access it via $this->connection
Also you need to use the same instructions in your if-statement.
Related
I wrote a simple PHP code to connect to the MySQL server as below
<?php
$username = "root";
$password = "Kepwd";
$hostname = "localhost:81";
//connection to the database
$dbhandle = mysqli_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
but this generates the following errors. I found some topics regarding this issue in google and Stack Overflow. but those don't help me.
( ! ) Warning: mysqli_connect(): MySQL server has gone away in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
# Time Memory Function Location
1 0.0014 240936 {main}( ) ..\test.php:0
2 0.0014 241528 mysqli_connect ( ) ..\test.php:8
( ! ) Warning: mysqli_connect(): Error while reading greeting packet. PID=10612 in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
# Time Memory Function Location
1 0.0014 240936 {main}( ) ..\test.php:0
2 0.0014 241528 mysqli_connect ( ) ..\test.php:8
( ! ) Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
# Time Memory Function Location
1 0.0014 240936 {main}( ) ..\test.php:0
2 0.0014 241528 mysqli_connect ( ) ..\test.php:8
Unable to connect to MySQL
The error is here:
$hostname = "localhost:81";
You are not connecting to MySQL, but to Apache server. If you didn't change MySQL port just use
$hostname = "localhost";
you forget to specify the database name after entering database name try again. The syntax should be like this
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
I'm getting errors now when i submit my form
Why am I not seeing any results?
These are the error messages that I'm receiving. Not Sure why.
These are the error messages that I'm receiving. Not Sure why.
( ! ) Deprecated: mysql_query(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\1form.php on line 22
Call Stack
# Time Memory Function Location
1 0.0000 244864 {main}( ) ..\1form.php:0
2 0.0010 254352 mysql_query ( ) ..\1form.php:22
( ! ) Warning: mysql_result() expects at least 2 parameters, 1 given in C:\wamp\www\1form.php on line 25
Call Stack
# Time Memory Function Location
1 0.0000 244864 {main}( ) ..\1form.php:0
2 0.0120 262464 mysql_result ( ) ..\1form.php:25
:)
<html>
<head>
<form method="post" action="1form.php">
<label for="textfield">Nino ID:</label>
<input type="text" name="ninoid" id="ninoid">
<input type="submit" name="submit" id="submit" value="Submit">
</form>
<?php
if (isset($_POST['submit'])) {
//connect to the database
include ('connect.php');
$ninoid = ($_POST['ninoid']);
$query = "SELECT ninoid FROM nino WHERE ninoid = '$ninoid';";
$result = mysql_query("SELECT ninoid FROM nino WHERE ninoid = '$ninoid';");
echo mysql_result($result);
}
?>
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I have simple inventory DB. When trying to access the products table and display the data in a table, i continually get "Unidentified index" and nothing i have found thus far can assist me. I am just starting PHP
conn.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "slfs_storesb";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_errno) {
printf("Connection failed: %s\n" . $conn->connect_error);
}
?>
index.php
<?php
$sql = "select * from tbl_station";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result)) {
var_dump($row);
// print $row[4];
//echo '<table id="t01">';
echo $row["Station_id"]." ".$row["Station_name"]." ". $row["Station_email"];
// echo '</table>';
}
// } else {
// echo "0 results";
// }
?>
THis is the error
Notice: Undefined index: Station_id in C:\wamp\www\stores\index_1.php on line 21
Call Stack
# Time Memory Function Location
1 0.0010 246504 {main}( ) ..\index_1.php:0
Gros Islet Fire Station
( ! ) Notice: Undefined index: Station_id in C:\wamp\www\stores\index_1.php on line 21
Call Stack
# Time Memory Function Location
1 0.0010 246504 {main}( ) ..\index_1.php:0
GFL Fire Hall
( ! ) Notice: Undefined index: Station_id in C:\wamp\www\stores\index_1.php on line 21
Call Stack
# Time Memory Function Location
1 0.0010 246504 {main}( ) ..\index_1.php:0
HeadQuarters
( ! ) Notice: Undefined index: Station_id in C:\wamp\www\stores\index_1.php on line 21
Call Stack
# Time Memory Function Location
1 0.0010 246504 {main}( ) ..\index_1.php:0
Dennery class footer { This is footer }
Instead of using SELECT * just spell out the (three) fields you want to access.
This way the query will fail if one of the fields is misspelled (or simply doesn't exist in that table).
<?php
$sql = '
SELECT
Station_id, Station_name, Station_email
FROM
tbl_station
';
$result = $conn->query($sql)
or trigger_error('query failed: '.join(',', $conn->error_list));
while( $row=$result->fetch_assoc() ) {
echo $row['Station_id'], ' ', $row['Station_name'],' ', $row["Station_email"], "<br />\r\n";
}
I wrote a simple PHP code to connect to the MySQL server as below
<?php
$username = "root";
$password = "Kepwd";
$hostname = "localhost:81";
//connection to the database
$dbhandle = mysqli_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
but this generates the following errors. I found some topics regarding this issue in google and Stack Overflow. but those don't help me.
( ! ) Warning: mysqli_connect(): MySQL server has gone away in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
# Time Memory Function Location
1 0.0014 240936 {main}( ) ..\test.php:0
2 0.0014 241528 mysqli_connect ( ) ..\test.php:8
( ! ) Warning: mysqli_connect(): Error while reading greeting packet. PID=10612 in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
# Time Memory Function Location
1 0.0014 240936 {main}( ) ..\test.php:0
2 0.0014 241528 mysqli_connect ( ) ..\test.php:8
( ! ) Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
# Time Memory Function Location
1 0.0014 240936 {main}( ) ..\test.php:0
2 0.0014 241528 mysqli_connect ( ) ..\test.php:8
Unable to connect to MySQL
The error is here:
$hostname = "localhost:81";
You are not connecting to MySQL, but to Apache server. If you didn't change MySQL port just use
$hostname = "localhost";
you forget to specify the database name after entering database name try again. The syntax should be like this
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
after php code row 1 and 2, I got a notice and error message.
Notice: Undefined variable: dbc in ...
Warning: mysqli_get_host_info()
expects parameter 1 to be mysqli, null given in ...
connectdb();
echo mysqli_get_host_info($dbc);
Can you help me please to solve my notice & warning? How can I make $dbc defined also for outher functions? I am working with error level -1 by the time being. please don't tell not to display the notices as a solution. Unfortunately, I can't understand the custom function variable passage issues. thanks.
function connectdb()
{
$dbc = mysqli_connect (db_host, db_user, db_password, db_name);
if (!$dbc)
{
$txt = mysqli_connect_errno().mysqli_connect_error();
db_connect_error_mail($txt);
unset ($txt);
die('error message.');
}
else
{
return $dbc;
}
}
You aren't assigning the return value of connectdb() method anywhere. You want:
$connection = connectdb();
echo mysqli_get_host_info($connection);
For clarity I have used a different variable name as the one you use in your function, because they are different variables, as they are defined in different scopes.
Think, you are returning $dbc, from the function, but you are not assigning the return value of the connectdb() function in line 1. How will the compiler know that you saved the return value in $dbc?
$dbc = connectdb();
This will fix your error.