Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
class Database
{
public $connect = "";
public function connect()
{
$connect = new mysqli("localhost", "root", "1521", "phone");
ini_set('default_charset', "UTF-8");
mysqli_set_charset($connect, "utf8");
header('Content-type: text/html; charset=UTF-8');
if ($connect->connect_error) {
die('Not connect' . mysqli_connect_error());
}
}
public function insert()
{
$sql = "SELECT id FROM personal ORDER BY id DESC LIMIT 1";
//global $connect;
$result = $connect->query($sql);//// this here !!!!!!!!
$row = $result->fetch_assoc();
$tempint = (int)$row['id'];
$tempint += 1;
}
}
why $connect in insert function not working ?
I added global befor $connect but still not working.
this very simple class for MySQL.
You syntax is incorrect.
Change this,
$connect = new mysqli("localhost", "root", "1521", "phone");
To,
$this->connect = new mysqli("localhost", "root", "1521", "phone");
Also,
$result = $connect->query($sql);
To,
$result = $this->connect->query($sql);
With the above changes it should fix the issue.
$connect has been created in the scope of the class so when you refer to it you need to use $this.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
<?php
$host ="localhost";
$user = "root";
$pass = "";
$db = "StuDet";
$conn = mysql_connect($host,$user,$pass);
mysql_select_db($db,$conn);
#mysql_select_db($database) or die ("Unable to select database");
?>
I have a doubt in line "$conn = mysql_connect($host,$user,$pass);"
<?php
$host ="localhost";
$user = "root";
$pass = "";
$db = "StuDet";
$con = mysqli_connect($host,$user,$pass,$db);
// Check connection
if (!$con){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Try the above code. Use mysqli.
The code is correct. If no connection is made, check your credentials and db name.
Also, since mysql is deprecated, change to mysqli.
link http://php.net/manual/en/book.mysqli.php
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
It's possible to create a mysql connection from localhost to another server using php, if it's possible, how? Thanks!
$host_name = "www.yourdomain.com";
$database = "pdo"; // Change your database name
$username = "root"; // Your database user id
$password = "test"; // Your password
try {
$dbo = new PDO('mysql:host='.$host_name.';dbname='.$database, $username, $password);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
Yes it is possible.You must have the username and password of that domain in which you want to connect.
mysqli_connect("www.domain.com","username","password","database_name")or die("Error " . mysqli_error());
Yes, Simply pass following details about your server:
<?php
$servername = "your-server-name-or-ip";
$username = "your-server-username";
$password = "your-server-password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
$url = 'mysql:host=xxx.xxx.xxx.xxx;dbname=xxxxxx'
$username = xxx;
$password = xxx;
$db = new PDO($url, $username, $password);
$query = $db->query('select * from some_table');
$query->execute();
$res = $query->fetchAll(PDO::FETCH_ASSOC);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I've double-checked and everything looks closed to me, so I can't find the error. I just want to create a table to display mySQL data.
EDIT: I don't know why the closing tag was above the rest of the code, but I still get the error when it's in the correct place.
<?php
$servername = "localhost";
$username = “x”;
$password = “x”;
$dbname = “x”;
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM Classroom”;
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo “<tr><th>Building</th><th>Floor</th><th>Room</th><th>Instructional</th><th>Type<th>Size</th>
<th>Seating</th><th>Decking</th><th>Access</th><th>Whiteboard</th><th>Chalkboard</th></tr>”;
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo “<tr><td>”.$row[“building”].”</td></tr>”;
}
} else {
echo (“0 results”);
}
mysqli_close($conn);
}
?>
Edit: As per your original post https://stackoverflow.com/revisions/27974352/1
This should go at the very bottom:
?>
In fact, it isn't even required unless you're going to put some pure HTML after it. So leaving it out completely might save you headaches in the future.
However, several of your double-quotes look funky pasted here. You might check that they are just double-quotes, and not special characters.
These curly/smart quotes “ ” should be replaced by regular double quotes " throughout your code.
Those alone will break its functionality and cause a parse/syntax error.
Edit: As per your edit: You need to remove the last } in your file, the one just after mysqli_close($conn);. The number of braces do not match.
This works!
<?php
mb_internal_encoding('UTF-8');
$servername = "localhost";
$username = "x";
$password = "x";
$dbname = "x";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM Classroom";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<tr><th>Building</th><th>Floor</th><th>Room</th><th>Instructional</th><th>Type<th>Size</th>
<th>Seating</th><th>Decking</th><th>Access</th><th>Whiteboard</th><th>Chalkboard</th></tr>";
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>".$row["building"]."</td></tr>";
}
}else{
echo("0 results");
}
mysqli_close($conn);
?>
Remove ?> from all your documents, as it's unneeded, as PHP self closes at the end of a file.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want my earth.php file to find the users id when a user enter mysite.com/earth.php. I want this because I want the id to be placed in a sql code that will be sent to the database when the user enters mysite.com/earth.php. And I think I have to start a session when a user logs in to help the earth.php to find the user id? Can someone help me with that because I don't know how to do it.
This is my earth.php file:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
session_start();
$id = $_SESSION['id'];
session_write_close();
$verbindung = mysql_connect("localhost","root","******");
mysql_select_db("lan");
if(isset($_SESSION['id'])) {
mysql_query("UPDATE users SET ally='3' WHERE id='$id'");
}
?>
And this is the value I want to be changed for the user to 3 when they enters mysite.com/earth.php
This is my login.php file
Please help, I've been trying to solve this problem for a long time.
You could do something like this
<?php
define('DB_TYPE', 'mysql');
define('DB_HOST', '127.0.0.1');
define('DB_NAME', 'dbname');
define('DB_USER', 'root');
define('DB_PASS', 'password');
session_start();
try {
// create a new instance of a PDO connection
$db = new PDO(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
// if the connection fails, display an error message
echo 'ERROR: ' . $e->getMessage();
}
if(isset($_POST['username']) && !empty($_POST['username'])) {
$username = $_POST['username'];
$sql = 'SELECT userid, username FROM users WHERE username = :username';
$stmt = $db->prepare($sql);
$stmt->bindValue('username', $username);
$stmt->execute();
$result = $stmt->fetchAll();
$_SESSION['id'] = $result[0]['userid'];
var_dump($_SESSION['id']); //this will show the the session
}
?>
As I see from your code, the $_SESSION['id'] is empty. Set a value for it. I see you edited your post. Then you just have to include your file after starting the session like
session_start();
include_once 'path/to/login/php/login.php;
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
I'm getting the Call to a member function query() on a non-object when I try to call my function.
My code looks like this:
function add_profile() {
$hostname = "localhost";
$dbusername = "username";
$dbname = "dbname";
$dbpassword = "password";
$link = mysqli_connect($hostname, $dbusername, $dbpassword, $dbname);
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
$sql = "INSERT INTO payment_profiles(id, client_id) VALUES ( '','$profile_id')";
$result = $mysqli->query($sql);
if (!result)
{
echo 'Error: ', $mysqli->error;
}
}
add_profile();
It says my error is on the line: $result = $mysqli->query($sql);
I'm assuming I'm not calling something properly. Thanks in advance for any help
In your code you're mixing both procedural and object-oriented code. Choose either one or the other. Here's how you would solve the problem the procedural way.
$result = mysqli_query($link, $sql, MYSQLI_USE_RESULT)
I'm getting the Call to a member function query() on a non-object when
I try to call my function.
That's because the $mysqli object is not declared anywhere (or is it)? Before you can use $mysqli you should first create an instance of mysqli and assign it to your object.
$mysqli = new mysqli("localhost", "my_user", "my_password", "database");
Only then you may call the methods of the mysqli class like $mysqli->query();
The error you made depends probably on two misconceptions:
1) you pasted half of your code from the procedural-style part of the mysqli manual and half from the oop part
2) you assume $mysqli is a global object instantiated with $mysqli_connect();. It is not. You should invoke the constructor with the new keyword if you'd like to use it as an object.
$link = mysqli_connect($hostname, $dbusername, $dbpassword, $dbname);
/////
$result = $mysqli->query($sql);
///////