so i'm not sure what the issue is but i'm pretty basic with mysql and i'm still learning. However I have been searching on the internet for about two hours now and I cant figure out what i've done wrong.
<?php
$id = 0;
// Create connection
$conn = new mysqli($servername, $dbname, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed" . $conn->connect_error);
}
$user_qry = "SELECT * FROM users WHERE id = $id";
$result = $conn->query($user_qry);
echo("<pre>");
print_r($result);
$conn->close();
?>
I am not receiving any connection error, but i'm
You have to fetch the result after executing the query:
$result = $conn->query($user_qry);
while ($row = $result->fetch_assoc()) {
print_r($row);
}
Regarding comment, stolen from another Stack Overflow post:
$result = $conn->query($user_qry)
if (!$result) {
throw new Exception("Database Error [{$this->database->errno}] {$this->database->error}");
}
In your connection, make sure you have selected a database name
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->select_db('test');
And one more thing, to see what is the error you should enable display_errors = On in php.ini
Related
I tried connecting the database in order to create chat by using:
https://youtu.be/k8DhWcdKanM
I would like to know what do I need to change in this code in order to connect it to db:
<?php
include 'dbh.php';
$uname= $_POST['username'];
$email=$_POST['email'];
$pass=$_POST['password'];
$sql="insert into signup(username,email,password)
values ('$username','$email','$password')";
$result=$conn->query($sql);
header("Location:index.php");
?>
Thank you.
Here is Connection Code.
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
Did you check your variables names inside "values( )".
I believe it should be:
values ('$uname','$email','$pass')";
If you are trying to connect the DB from Another File you have to follow these steps
Step 1: db.php (Will provide two connectivity methods)
$host="localhost";
$user="phpmyadmin1";
$password="123456";// This can be empty in some cases
$db="XXX"; // XXX - DB Name
// This is First type of connection with returning
$con_1 = mysqli_connect($host,$user,$password,$db); // Here the $con will be acting as the connection variable
// This is Second type of connection with returing
$con_2 = new mysqli($host,$user, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
return $con_2; // This will return the connection variable to necessary pages that are needed.
Step 2: signup.php
In this we have to include the db.php file and then proceed with the connections.
<?php
include('db.php');
$uname= $_POST['username'];
$email=$_POST['email'];
$pass=mysqli_real_escape_string($con,$_POST['password']);
$sql="INSERT INTO `signup`(username,email,password) VALUES ('".$uname."','".$email."','".$pass."')";
$result=mysqli_query($con,$sql); // Here we have to add the connection variable
header("Location:index.php"); // Used for Header Relocation
?>
In the insert statement some of the variables like $uname,$pass are wrong. Hence the Query will fail and the insert operation will not be done. So kindly double check the variables and then proceed with the coding.
Hope this might be helpful for you. Thanks.
Try this code:
$conn = new mysqli($servername, $username, $password, $dbName);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
return $conn;
I've looked all over here. Please be patient as I am new to php and mysql.
I got WAMPP installed & seems to be working OK. I created a simple "test" database from phpMyAdmin and "firsttable" in that. I can do a simple connect using example from w3schools, but trying to select & display data I entered only throws back errors.
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Connect
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT reference, firstname, lastname, room FROM firsttable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["reference"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "room:" . $row["room"]. "<br>";
}
} else {
echo "0 results";
}
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
First off, I get a parse error on line 17. The one that reads:
if ($result->num_rows > 0) {
The error says: Trying to get property of non-object.
I tried wrapping the whole php code in tags and saving it as html, but then it appeared that no row data was ever found.
I am able to use very simple code that connects successfully. I can confirm the database is in there, so is the table, and the contents I added to it.
Please, what am I doing wrong?
You need to specify the database when you connect:
$database = 'test';
$conn = mysqli_connect($servername, $username, $password, $database);
where $database is the name of your database (test in this case). MySQL doesn't know which database your table resides in without you telling it.
In addition, you should always include error checking for your database connection (you have two of these, you don't need the last one) as well as any queries. Sans this, you can check your error logs for more information when something fails.
This question already has answers here:
Connect to Multiple Databases using MySQLi
(3 answers)
Closed 6 years ago.
Ok so this is my code for the connection:
$servername = "host";
$username = "user";
$password = "pass";
$db1 = "db1";
$db2 = "db2";
// Create connection
$conn = new mysqli($servername, $username, $password, $db1); // $db1 is here as the default, is this ok?
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Then I run a function with this connection and it works for the $db1:
$sql = "SELECT etc etc;
$result = $conn->query($sql);
etc etc
The problem is when I try to change the db to $db2, I use this:
$conn->select_db($db2);
And now it only returns the value of the 2nd Database ($db2), and the one from $db1 doesn't show up on the page anymore, it doesn't show any value.
Thanks for Reading.
You have use two different connections while connecting different databases:
Database One Connection:
$conn = new mysqli($servername, $username, $password, $db1);
$sql = "SELECT etc etc;
$result = $conn->query($sql);
Database Two Connection:
$conn1 = new mysqli($servername, $username, $password, $db2);
$sql = "SELECT etc etc;
$result = $conn1->query($sql);
The Problem is that you have already assigned the conn to DB1 and hence if you run by selecting the DB2 it will be displaying error or no results will be produced. Hence while selecting multi databases you can use different connection variables.
the one from $db1 doesn't show up on the page anymore, it doesn't show any value.
what about making it $conn->select_db($db1); back?
Create two config files
db1.php
$servername = "host1";
$username = "user1";
$password = "pass1";
$db = "db1";
db2.php
$servername = "host2";
$username = "user2";
$password = "pass2";
$db = "db2";
//connection to db1
include("db1.php");
$conn = new mysqli($servername, $username, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//db1 codes
--------------------
//connection to db2
include("db2.php");
$conn = new mysqli($servername, $username, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//db2 codes
I'm new to PHP and MYSQL, and I have what I think is going to be a relatively easy question.
My objective is to show one image (a green flashing light) when the database is connected, and display another image (a red flashing light) when there is no database connection.
I imagine it should be a simple variation on this:
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
But if I attempt to add an image to where it echos "Connected successfully" I receive an error.
I'm attempting to add the image like this:
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("<img src="Red_Light.gif" style="width:10px;height:10px;"> " . $conn->connect_error);
}
echo "<img src="Green_Light.gif" style="width:10px;height:10px;">";
?>
I probably have the completely wrong syntax but any help is appreciated.
Many thanks,
Leif
You can use Janno answer or you may use this (by changing " to ':-
if ($conn->connect_error) {
die("<img src='Red_Light.gif' style='width:10px;height:10px;'> " . $conn->connect_error);
}
echo "<img src='Green_Light.gif' style='width:10px;height:10px;'>";
if ($conn->connect_error) {
die("<img src=\"Red_Light.gif\" style=\"width:10px;height:10px;\"> " . $conn->connect_error);
}
echo "<img src=\"Green_Light.gif\" style=\"width:10px;height:10px;\">";
Whole problem seems to be related to not escaping the quote marks.
Hello I am new at php and mysql and I don't know what is wrong.
I cant show the results from query and the connection with mysql is successfully connected.
I don't use wampserver I just install php,mysql and Apache separately.
Thanks in advance.
Code
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$sql="select * from `books`;";
$result=mysqli_query($conn,$sql);
if (!$result){
echo "query cannot execute";
};
?>
its only show me "query cannot execute"
You need to pass fourth parameter database name in mysqli_connect()
It would be
$conn = mysqli_connect($servername, $username, $password,"YOUR_DATABASE");
Read http://php.net/manual/en/mysqli.error.php to check error in query.
Read http://php.net/manual/en/mysqli-result.fetch-array.php
To fetch data from query result