Always getting blank page while using php and mysql connections [duplicate] - php

This question already exists:
PHP's white screen of death [duplicate]
Closed 5 years ago.
I am always getting blank page when using Php and mysql connections
this is my php file
this is my html file

Try this code instead:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>

Related

Unable to select MySQL database in PHP [duplicate]

This question already has answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Reference - What does this error mean in PHP?
(38 answers)
Closed 3 years ago.
I get the error message: Connection failed: Unknown database 'first'
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "first";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, age FROM table";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - name: " . $row["name"]. "age: " . $row["age"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
The database I'd like to select is there, but I get the error over and over

SQL database connection in PHP successful, but I can't query it [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "test";
$tablename = "mapcoords";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
echo "Failure";
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "SELECT (lat, lng) FROM mapcoords";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
{
echo "ok";
}
$conn->close();
?>
Here is the code. So like I said, it can connect successfully, but the code won't successfully query. What's weird is that if I copy and paste the same code, which seems to be EXACTLY the same, it works. It makes no sense. I can't find a single difference between their code and my code besides the way they space things and the way I space things. Here is their code:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT (lat, lng) FROM mapcoords";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["lat"]. " " . $row["lng"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
The problem is that this query:
SELECT (lat, lng) FROM mapcoords
returns the folloowing error:
[21000][1241] Operand should contain 1 column(s)
You have to change the query to
SELECT lat, lng FROM mapcoords

PHP MySQL Undefined index when displaying on web page

I have a fairly simple PHP page which displays some fields from a database.
For some reason I get:
Notice: Undefined index: entries.id in /var/www/html/originalprices.php on line 24
I can't see whats wrong, could anyone help? Thanks
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "zxdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT entries.id, entries.title, entrytypes.text,entries.original_price, entries.budget_price,labels.name FROM entries,publishers, labels, entrytypes
where entries.entrytype_id = entrytypes.id
and publishers.entry_id = entries.id
and publishers.label_id = labels.id
and labels.id = '1371'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["entries.id"]. " - Name: " . $row["entries.title"]. " " . $row["entrytypes.text"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
The Name of the returned field will be "id" not entries.id.
You can check that by using var_dump()

How do I import info from a database to my website? [closed]

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
Using the code below I am trying to import users table into my php page and only want to show the last ten entries in a table, what else do I need to add to my code to achieve this
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM dispenses";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Amount: " . $row["amount"];
}
} else {
echo "0 results";
}
$conn->close();
?>
$query = "SELECT * FROM 'users'";
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"];
}
} else {
echo "0 results";
}
$conn->close();
?>
This should help:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
http://www.w3schools.com/php/php_mysql_select.asp

How to write PHP to Mysql page

Currently I just done HTML and I trying writing PHP Script on connection Mysql, But all web site on google is confuse, because now I need specify write connection by used PHP Only,
Please could you help explain step by step on code from HTML to PHP and write PHP to MySql, Thank you very much.
There is many docs about this, but you can start from here:
http://www.w3schools.com/php/php_mysql_select.asp
And here is the example code, what i suggest you:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>

Categories