When I add a column, I can't retrieve it from mysql - php

After I add a column to my database, I want to retrieve it but not expected.
In PHP, I try reopening apache and mysql still not work.
Does anyone know how to resolve it? Thanks!

your question is not fully explanatory but with what I could try to understand you want to retrieve data or records from your database
you could try the code below and tweak it to work your way
<?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 databaseName";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data
while($row = $result->fetch_assoc()) {
print $row"<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>

Related

Access database remotely to fetch data

I need to access a database remotely using PHP and display the fetched data in html. My code is as below
$servername = "192.168.56.1:3306";
$username = "root";
$password = "";
$dbname = "grh";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
error_log("Failed to connect to database!", 0);
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM ip_patient_logs";
$result = $conn->query($sql);
$rows = array();
if ($result->num_rows > 0) {
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
print json_encode($rows);
} else {
echo "0 results";
}
$conn->close();
I expected to be able to connect to the database and fetch the data , but it displays blank page. I am a beginner in PHP.

MySQL table value be equal with php var (then display it)

I have the following MySQL table and I would like to do that RVR11 (500) be equal with a php var $RVR11
I have this code but it doesnt work.
$servername = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$price = mysql_query("SELECT RVR11 FROM tablename");
$result = mysql_fetch_array($RVR11);
echo $result['RVR11'];
Sorry about this beginner question.
You should choose a database before any queries, you can pass the fourth arguments to new mysqli constructor.

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

Query MySQL And Write Results To Screen

I have just set-up LAMP and am trying to query a mysql database and write the results on screen. The issue I have is that instead of writing the results of my query onscreen, my syntax is written on screen instead?
What is the proper way of querying a mysql database with php and writing the results on screen?
This is my syntax that does not work?
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "Select state from PHPTests.state";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo $row['state'];
}
}
else
{
echo "0 results";
}
$conn->close();
?>
<html>
<body>
<h4>Hello - This is test site with php</h4>
</body>
</html>

PDO SUM MYSQL table

I am learning as i go and been picking up snippets of code as i go and been working on this for the past few days and now i have thrown the towel in to seek help.
I am trying to calculate the sum of 2 columns in my database using PDO.
here is my code
<?php
$host = "localhost";
$db_name = "dbname";
$username = "root";
$password = "root";
try {
$con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
}
// show error
catch(PDOException $exception){
echo "Connection error: " . $exception->getMessage();
}
$query = "SELECT SUM (fill_up) AS TotalFill,
SUM (mileage_covered) AS Totalmiles
FROM fuel_cost";
$row = $query->fetch(PDO::FETCH_ASSOC);
$total_fill = $row['TotalFill'];
$total_miles = $row['Totalmiles'];
$myanswer = $total_fill/$total_miles;
//display the answer
echo $myanswer
?>
I have also checked my database table and both columns are varchar(32)
the error I am getting is Call to a member function fetch() on a non-object
I have searched into this but not sure what's the issue with the above code
Thank You in advance
Try this code :-
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT SUM (fill_up) AS TotalFill,
SUM (mileage_covered) AS Totalmiles
FROM fuel_cost";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
$total_fill = $row['TotalFill'];
$total_miles = $row['Totalmiles'];
$myanswer = $total_fill / $total_miles;
//display the answer
echo $myanswer;
die;
}
} else {
echo "0 results";
}
$conn->close();
?>

Categories