can anyone explain where have i done something wrong? - php

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

Related

How to display number of rows in a sql table using php?

I'm trying to get lines' number from the result of the sql query, so I started by connecting to the database, then checking if the connection was established and finally I tried to count the number of lines, here's the code :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$res = $conn->query('SELECT COUNT (id_offre) AS nb FROM offres');
$data = $res->fetch();
$nb = $data['nb'];
echo $nb;
?>
I get these errors :
Uncaught Error: Call to a member function fetch() on boolean
Call to a member function fetch() on boolean
I used echo to check the 'nb' value, what is the problem?
Your query fails which makes $conn->query() to return false.
So, why does the query fail?
SQL doesn't allow for any spaces between function names and the parentheses, like you have at COUNT ().
So if you change:
SELECT COUNT (id_offre) AS nb FROM offres
to
SELECT COUNT(id_offre) AS nb FROM offres
...it should work.
Note: I would highly recommend you to have a read about mysqli::error() and add some error handling to your code.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_test";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (! $conn) {
die("Connection failed: " . mysqli_connect_error());
}
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: ".$conn - > connect_error);
}
$res = $conn->query('SELECT * FROM offres');
$data_count = mysqli_num_rows($res);
echo($data_count);
?>
check this out!

PHP mysql insert not working without error check

My table has 2 fields a primary with auto increment and then entry_id
why does the below not insert in the table:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ecolog";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$entry_id = 5;
$sql = "INSERT INTO orders (entry_id) VALUE ('$entry_id')";
$conn->close();
?>
but when adding a error check it does?
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ecolog";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$entry_id = 5;
$sql = "INSERT INTO orders (entry_id) VALUE ('$entry_id')";
if ($conn->query($sql) === TRUE) {
echo "";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
cant get my head around why this would be happening.

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";

Why I can't run this query? [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 7 years ago.
I'm trying to run this code in php but does not print anything.
My database is not empty.
<?php
$dbname = "mytest";
$servername = "***";
$username = "mohsen";
$passwords = "****";
// Create connection
$conn = new mysqli($servername, $username, $passwords, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$User=$_POST['username'];
$sql="SELECT * FROM Money WHERE Debtor='$User' OR Creditor='$User' ";
$result = $conn->query($sql);
//
while($field=mysql_fetch_assoc($result))
{
print $field["Debtor"]."|";
print $field["Creditor"]."|";
print $field["Cost"]."|";
print $field["Status"]."|"."\n";
}
$conn->close();
?>
EX:
I have record with this user in my database
User=mohsen
link:
http://test.kholaseketab.ir/Update.php
You are mixing object oriented and procedural style query.
<?php
$dbname = "mytest";
$servername = "***";
$username = "mohsen";
$passwords = "****";
// Create connection
$conn = new mysqli($servername, $username, $passwords, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$User=$_POST['username'];
$sql="SELECT * FROM Money WHERE Debtor='$User' OR Creditor='$User' ";
$result = $conn->query($sql);
//
while($field = $result->fetch_object())
{
print $field->Debtor."|";
print $field->Creditor."|";
print $field->Cost."|";
print $field->Status."|"."\n";
}
$conn->close();
?>
<?php
$dbname = "mytest";
$servername = "***";
$username = "mohsen";
$passwords = "****";
// Create connection
$conn = new mysqli($servername, $username, $passwords, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$User=$_POST['username'];
$sql="SELECT * FROM Money WHERE Debtor='$User' OR Creditor='$User' ";
$result = $conn->query($sql);
while($field=$result->fetch_assoc())
{
print $field["Debtor"]."|";
print $field["Creditor"]."|";
print $field["Cost"]."|";
print $field["Status"]."|"."\n";
}
$conn->close();
?>

Categories