Simple Mysqli Select query - php

So im trying to to post some data from database in wordpress ,i conect to the database normaly but it wont get any data from it. I have no idea what am i doing wrong.Here is the code
<?php
$servername = "localhost";
$username = "root";
$password = "root123";
$dbname = "MyDB";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully"."</br>";
$sql = "SELECT event_name FROM wp_em_events";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Name :".$row["event_name"];
}
} else {
echo "0 results";
}
$conn->close();
?>

You're missing the database name from your connection.
Change this:
$conn = new mysqli($servername, $username, $password);
to
$conn = new mysqli($servername, $username, $password, $dbname);

It looks like you've connected to the Database correctly, but you've not specified what table to use. The line:
$conn = new mysqli($servername, $username, $password);
Should be:
$conn = new mysqli($servername, $username, $password, $tablename);

Related

Selecting data from sql database doesn't work. how do I fix it?

I am trying to select data from a database. I do have a successful connection, but it seems like the query doesn't work even though I know for sure that the query is right. What am I doing wrong?
If I execute the code below, the result I get is: "Connected successfullyBad query". The 'Bad query' should mean that the query is wrong, but I checked it and it isn't wrong...
<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql ="SELECT * FROM `producten`";
$result = mysqli_query($conn, $sql) or die("Bad query");
$conn->close();
?>
I expect to only see "connected successfully"
You are missing your database name. You can do it two ways, or in the connect statement:
$conn = new mysqli($servername, $username, $password,$database);
Or you can do it in your select statement:
$sql ="SELECT * FROM `yourdatabase`.`producten`";
If you donĀ“t set your database your query is wrong
Your query is right just write your database name in mysqli constructor.
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
Visit: https://www.php.net/manual/en/mysqli.construct.php
Please give database name also, check below code.
<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = ""; //Enter database name
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql ="SELECT * FROM `producten`";
$result = mysqli_query($conn, $sql) or die("Bad query");
$conn->close();
?>

can anyone explain where have i done something wrong?

When I run this code it shows parse error. Can anyone help me with this?
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "form";
// Create connection
$conn = new mysqli($servername, $username,$password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " .$conn->connect_error);
}
SELECT * FROM my_db;
$conn->close();
You should use php tags and remove those ">" also you should use mysqli_query to query the data from mysql
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "form";
// Create connection
$conn = new mysqli($servername, $username,$password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " .$conn->connect_error);
}
$query = mysqli_query($conn,"SELECT * FROM my_db");
$conn->close();
?>

I want to fetch time column from mysql database and to compare with current time

I want to fetch time column from mysql database and to compare with current time.
It should execute only if it matches with the current time.
Please tell me a MySql query or php code to do this.
I am new to php.
Try This:
<?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);
}
$today = date("h:i a")
$sql = "SELECT * FROM Table_name WHERE SUBSTRING_INDEX(time, ' ', 2) = '".$today."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// execute you code
} else {
}
$conn->close();
?>
Try this: Updated 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);
}
$today = date("h:i a")
$sql = "SELECT * FROM Table_name WHERE time = $today";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// execute you code
} else {
}
$conn->close();
?>

How to connect to MySQL db (xampp) using php?

I'm trying to connect to mysql db in phpstorm, but I don't succeed.
I will appreciate your help.
<?php
$servername = "http://localhost:8012";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
die("Connected successfully");
}
The error is:502 Bad Gateway
You can connect database like that:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "";
$port = "8012";
// Create connection
$conn = new mysqli($servername, $username, $password, $database, $port);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
die("Connected successfully");
}
<?php
$servername = "localhost:8012";
$username = "root";
$password = "";
$database = "dbname";
// Create connection
$conn = mysqli_connect($servername, $username, $password,$database) or die(mysqli_error($conn);
echo "Connection Successful";

Returning results from database

I am trying to do a simple SELECT to return rows of data from my database. I have a valid connection from my database so I know the issue is not there. I have ensured the names of each column are correct but it just returns 0 results.
My table inside the db is called 'user' and here is the members.php file:
<?php include 'header.php'; ?> <- this is where the db conect file is pulled in.
<?php
$sql = "SELECT id, username, email_address FROM user";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo
$row["id"];
}
} else {
echo "0 Members";
}
$conn->close();
?>
Just for ref here is my DB connection (Not the most secure i am just testing):
<?php
$servername = "localhost";
$username = "***********";
$password = "**********";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully<br><br>";
?>
you didnt select your database
$conn=new mysqli($servername, $username, $password);
this require another parameter which is your d.b name
$conn=new mysqli($servername, $username, $password,$db_name);

Categories