Why does mysqli() not recognize my database name? - php

I'm new to PHP and MySQL. While learning, I got this error that says my database is unkown.
I have already made this database.
and I have already made the table 'todos'
Here is my PHP
<?php
require 'functions.php';
$conn = new mysqli('localhost','root','', 'mytodo');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$statement = $conn->prepare('select * from todos');
$statement->execute();
var_dump($statement->fetchAll());
require 'index.view.php';
the PHP file is named 'Index.test.php'
but when I try to access localhost/Index.test.php on my browser
it returns this
Could you tell me why I am getting the error? Appreciate the help!

try to use this select:
<?php
require 'functions.php';
$conn = new mysqli('localhost','root','', 'mytodo');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//select query
$sql = "SELECT * FROM todos";
if ($result = mysqli_query($conn, $sql)) {
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)) {
echo $row['your column name'];
}
//free result set
mysqli_free_result($result);
} else {
echo "no records found";
}
}
else {
echo "error: could not connect" . mysqli_error($conn);
}
require 'index.view.php';
?>

Related

How to echo users from table those are logged in?

I am trying to make a list that echos only members who are currently signed in.
Been googling for an answer all day and no luck.
I have an ENUM row for each column that represents when a user is logged on or logged off as 0 or 1. But how would I go about populating a list of users which only have their ENUM value set to 1?
This is what I got:
<?php
// Create connection
include_once '/phpscripts/credentials.php';
$conn = new mysqli($db_hostname, $db_username, $db_password, $db_database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
$sql="SELECT * FROM users";
$INFO=mysqli_query($conn, $sql);
While($accounts=mysqli_fetch_assoc($INFO)){
$sql = "SELECT * FROM users WHERE loggedin = '1'";
echo $accounts['username'];
echo "<br>";
}//end while
}
?>
I have modified your code. Try the code below
<?php
// Create connection
include_once '/phpscripts/credentials.php';
$conn = new mysqli($db_hostname, $db_username, $db_password, $db_database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
$sql = "SELECT * FROM users WHERE loggedin = '1'";
$INFO=mysqli_query($conn, $sql);
if (mysqli_num_rows($INFO) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($INFO)) {
echo "Username: " . $row["username"]. "<br>";
}
} else {
echo "0 results";
}
}
?>

Php Header Function Not Working In My Website [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 7 years ago.
Im working on a reservation system deep dive and im stuck on the header function not redirecting my user to the 'index.php' page. Here's my PHP code.
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM customer WHERE transactionum = '$ticket' ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<strong>".$row["fname"]. " " . $row["lname"]."</strong><br>";
}
} else {
header('location:index.php');
}
?></li>
<li class="list-group-item">Paid: <?php if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM customer WHERE transactionum = '$ticket' ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<strong>".$row["paid"]. "</strong><br>";
}
} else {
header('location:index.php');
}
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM customer WHERE transactionum = '$ticket' ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$test1 = "<strong>".$row["fname"]. " " . $row["lname"]."</strong><br>";
}
} else {
header('location:index.php');
}
?></li>
<li class="list-group-item">Paid: <?php if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM customer WHERE transactionum = '$ticket' ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$var1 = "<strong>".$row["paid"]. "</strong><br>";
}
} else {
header('location:index.php');
}
echo $test1;
echo $var1;
Look this url http://php.net/manual/es/function.header.php
Use capital Letter
header('Location: http://www.example.com/');
Try to use this form and make sure to your code 'else' execution.

php read data from sql database

I'm trying to learn php and I can't read any data from my database!. I know the connection to the server is live and working but this line seem to be giving me problems.
$result = $conn->query($sql);
Where $sql = "SELECT firstName, middleName, lastName FROM Base";
I'm not sure what the problem is but any hints or answer are appreciated .
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>My first PHP page</h1>
<?php
// connect to database
$user_name = "superUser";
$password = "";
$database = "Base"$server = "127.0.0.5";
// open connection to the server
$conn = new mysqli($server, $user_name, $password);
// echo "Connection to the server is open";
// check connetion
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else {
print "Connection to the server is open";
}
// load the data from the table
$sql = "SELECT firstName, middleName, lastName FROM Base";
// echo $conn->server_info;
$result = $conn->query($sql);
if ($result) {
echo "Table was found";
}
else echo "no";
/*while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br />";
}
print $result["firstName"];// ": Number of Rows!" ;*/
// if ($result->num_rows > 0) {
// output data of each row
// close server connection
$conn->close();
?>
</body>
</html>
The following is a mysqli connect example.
<?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();
}
?>
You forgot to add your database name.
Change this,
$conn = new mysqli($server, $user_name, $password);
to this
$conn = new mysqli($server, $user_name, $password, $database);

Get value from specific MySql row

On my website, a user's background URL is stored with their data in a database. I need to get this value and store it in a variable.
However, I am having trouble doing so, the code I have right now gives me an error but doesnt tell me what the error is.
My code:
<?php
error_reporting(E_ALL);
include 'config.php';
$pin = $_COOKIE["UID"];
// Create connection
$conn = new mysqli($db_servername, $db_username, $db_password, $db_name);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT * from users where pin ='$pin'";
$result = $conn->query($query);
if ($conn->query($query) === TRUE) {
$bg_url = $row[bg_url];
} else {
echo "Error: " . $query . "<br>" . $conn->error;
}
echo $bg_url;
mysqli_close($conn);
?>
$query = "SELECT * from users where pin ='$pin'";
$result = $conn->query($query);
Actually yes, the John's answer is correct, you're not using the return values properly, but you are neither accesing the variables properly, for example, I don't see where $row comes from. Try this and change it for array mode if you need to. I'm not very used to mysqli_* API, because I use PDO mostly.
if ($result) {
while($row = $result->fetch_object()) {
$bg_url = $row->bg_url;
}
} else {
echo "Error: " . $query . "<br>" . $conn->error;
}
echo $bg_url;

PHP SELECT * FROM not working

I am trying to query data from a table using the following script:
//connect file
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//connect file included above - ECHO tested and it is connected as $conn
$sql = "SELECT * FROM userInfo";
$results = $conn->query($sql);
if (!$results) {
printf("Errormessage: %s\n", $conn->error);
exit;
} else {
echo $row['username'];
}
UPDATE --
It now no longer tries to throw an error and seems to go to the else section; however, no echo - and the spelling is correct this time and the column is filled.
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["username"];
}
} else {
echo "0 results";
}
This now returns results. Thank you #Fred -ii- especially for your help on this.
Also thanks #jjczopek for the error checking advice!

Categories