Unable to select MySQL database in PHP [duplicate] - php

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

Related

I'm not able to display data from a mysql database, instead it tell me access denied [duplicate]

This question already has answers here:
MySQL Error: : 'Access denied for user 'root'#'localhost'
(28 answers)
Closed 3 years ago.
I want to display data from a database from instead of showing the data in html it tells me "Connection failed: Access denied for user ''#'localhost' (using password: NO)"
I've tried searching the problem online and when other people had the same issue it was the case where they either had 'root'#'localhost' or 'user'#'localhost'. But in my case it doesn't even show any kind of username, it's blank.
<?php
$servername = "localhost";
$username = "john";
$password = "johndoe";
$dbname = "login";
// Create connection
$conn = new mysqli($localhost, $john, $johndoe, $login);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT firstname, lastname FROM cards";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["id"]. " - Name: ". $row["firstname"]. " " . $row["lastname"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Where did you declare these variables?
$conn = new mysqli($localhost, $john, $johndoe, $login);
In your code, the real variable names are: $servername, $username, $password, $dbname and that's what you should use to retrieve their values, which are "localhost, "john", "johndoe" and "login".
Try:
$conn = new mysqli($servername, $username, $password, $dbname);

Returns Internal Server Error (500) after connecting to MySQL database [duplicate]

This question already has an answer here:
PHP: 500 Error to error page
(1 answer)
Closed 5 years ago.
I want to select data from a MySQL database with PHP. Problem is, that when I try to echo out the $result, I get a 500 Error. When I leave out the echo $result;, I get a 200 OK return.
You guys gut any ideas?
Here's the PHP:
$q = $_GET['q'];
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "test";
//establish connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check Conncetion
if(!$conn) {
die("Connection failes: " . mysqli_connect_error());
}
$sql = "SELECT * FROM phptesting";
$result = mysqli_query($conn, $sql);
echo $result;
/*
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. "- Name: " . $row["first_name"]. " " . $row["last_name"]";
}
*/
mysqli_close($conn);
For your info, $q is just an integer with an id for testing it out.
echo is used to output primitive data type such as String, Integers.
$result holds the metadata of your SQL query result.
In ur code ur trying to echo the metadata. And This caused PHP fatal error.

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

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();
?>

"SELECT email FROM table WHERE name=$name"; in PHP [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 6 years ago.
I am facing trouble printing the details of a username from MYSQL. The quotes in WHERE name = "xxx" is the cause.
This is the code:
$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 name, email FROM MyTable WHERE name=$name";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["name"]. " - email: " . $row["email"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
How do I replace the WHERE name = $name?
you are missing an ''
should be:
WHERE name = '$name'
notice the quotes
I think you shoudl use this notation (previously be sure of a proper sanitize of $name for preventing potential SQL injection)
"SELECT name, email FROM MyTable WHERE name='$name'";

Fatal error: Call to a member function fetch_assoc() on a non-object in ... on line 17 [duplicate]

This question already has an answer here:
Mysqli update throwing Call to a member function bind_param() error [duplicate]
(1 answer)
Closed 7 years ago.
After migrating from localhost to webserver I'm getting this error. Maybe it could be because of the PHP version on webserver (PHP 5.2). Any ideas?
<?php
$servername ="";
$username ="uran";
$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 answer WHERE topic_key='$id'";
$result = $conn->query($sql);
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<strong>Príspevok č.:</strong> " . $row["id"]. "<br>"." <strong>Napísal:</strong> "
. $row["name"]. "<br>". $row["topic"]."<br>" . $row["reg_date"]."<br>"." <br><br>";
}
$conn->close();
?>
I think that your query fails to that $conn->query($sql) will return FALSE (see mysqli::query).
Please add the following code to your code to get the error message:
if (!$result) {
printf("Error: %s\n", $conn->error);
}

Categories