All i want to do is to store the name of the guest that is on the A1 seat in a PHP variable named "resulted".
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$dbname = 'test';
$con=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
if ($con->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$resulted = mysqli_query("SELECT name FROM guests WHERE seat='A1');
echo $resulted;
?>
I know this is totally wrong but I don't know how i shoud do it....
There is some issue in your code. I have fetched associative array amd print gust name.
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$dbname = 'test';
$con=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
if ($con->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$resulted = mysqli_query($conn, "SELECT name FROM guest WHERE seat='A1'");
if(mysqli_num_rows($resulted) > 0) {
$row = mysqli_fetch_assoc($resulted);
print_r($row['name']);
}
// Free result set
mysqli_free_result($resulted);
mysqli_close($con);
?>
$resulted is a mysqli resource. You need to fetch a row from this resource like this:
$row = mysqli_fetch_assoc($resulted);
echo $row['name'];
For more information, visit http://www.w3schools.com/php/func_mysqli_fetch_assoc.asp.
Related
I have created html file with generated text, and i want to populate fields like name and date from mysql database. The thing is i am trying to show data from mysql in html input text tag. But it only shows letter S.
This is my php code:
<?php
$db_host = ''; // Server Name
$db_user = ''; // Username
$db_pass = ''; // Password
$db_name = ''; // Database Name
$con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = "SELECT ime_prezime,datumpocetka,datumzavrsetka,broj_dana,sektor,
mesto,tip,obrazlozenje,vreme_slanja
FROM godisnji
WHERE id=74";
mysqli_close($con);
?>
And this is a part of my html code:
<p style="text-align:justify">1. Zaposlenom <input name="lname" type="text" value="<?php echo $result['ime_prezime'];?>"/>
na poslovima $posao, se odobrava
korišćenje godišnjeg odmora za 2018. godinu, u trajanju od 20 radnih dana.</p>
Just because...
Now this is very basic and you should learn to use PDO or at least the mysqli class. But for now...
This is a little demonstration code... ( It may have bugs as I have not run it )
<?php
// The Database Connection Stuff should be in it's own file
// and included in each file that requires a database connection.
$db_host = ''; // Server Name
$db_user = ''; // Username
$db_pass = ''; // Password
$db_name = ''; // Database Name
$con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Create the SQL
$sql = "SELECT ime_prezime,datumpocetka,datumzavrsetka,broj_dana,sektor,
mesto,tip,obrazlozenje,vreme_slanja
FROM godisnji
WHERE id=74";
// Run the SQL statement
$query = mysqli_query($con,$sql);
// Read back the ROW as an associative array
$result = mysqli_fetch_assoc($query);
// DEBUG to learn what you get back and CHECK it's what you expect!
var_dump($result);
// If it exists...
echo ($result['ime_prezime']);
mysqli_close($con);
// Then Read up on how to use PDO or mysqli Class
And as a suggestion, due to the fact you won't always have a valid value you could use instead of...
<?php echo $result['ime_prezime'];?>
Try this
<?= isset($result['ime_prezime'])?$result['ime_prezime']:'';?>
The above tests whether $result['ime_prezime'] exists, and if it does it will print it, else it will print ''.
Look up "Ternary Operators in PHP"
<?php
$db_host = ''; // Server Name
$db_user = ''; // Username
$db_pass = ''; // Password
$db_name = ''; // Database Name
// Create connection
$conn = new mysqli($db_host, $db_user , $db_pass , $db_name );
if ($conn->connect_error)
{
echo "Failed to connect to MySQL: " . $conn->connect_error;
}
$result = "SELECT ime_prezime,datumpocetka,datumzavrsetka,broj_dana,sektor,
mesto,tip,obrazlozenje,vreme_slanja FROM godisnji WHERE id=74";
$sqlQuery = $conn->query($result);
$value = $sqlQuery->fetch_assoc()['ime_prezime'];
echo "<p style='text-align:justify'>1. Zaposlenom <input
name='lname'type='text' value='".$value."'/></p>";
$conn->close();
?>
I want to open a count in a php file in order to get the first register
<?php
include_once('dbConfig.php');
session_start();
$id_persona= $_SESSION["id"];
$query = "SELECT COUNT( * ) AS alias1 FROM `desayuno_ingreso` WHERE `desayuno_ingreso`.`id` =88757532";
echo $query;
$resultset = mysqli_query($conn,$query);
if(mysqli_num_rows($resultset) > 0){
$k=0;
while ($fila = mysqli_fetch_assoc($resultset)) {
$lat[$k]= $fila['alias1'];
$k=$k+1;
}
}
echo $lat[0];
?>
I tried in every single way I know (Every way worked for me before). However, it doesn't working right now.
I'm no sure what I'm doing wrong.
EIDTED
This is the dbConfig.php file:
<?php
$dbHost = 'localhost';
$dbUser = 'root';
$dbPass = 'mypassword';
$dbName = 'app';
$conn = mysqli_connect($dbHost,$dbUser,$dbPass,$dbName);
if(!$conn){
die("Database connection failed: " . mysqli_connect_error());
}
?>
How do I choose a tbl_uploads table in the database?
PHPMYADMİN IMG
<?php
$dbhost = "";
$dbuser = "";
$dbpass = "";
$dbname = "";
mysql_connect($dbhost,$dbuser,$dbpass) or die('cannot connect to the server');
mysql_select_db($dbname) or die('database selection problem');
?>
You don't need to choose any table just fire the Query on the database in which the table exists, and you're done. For example,
<?php
$dbhost = "";
$dbuser = "";
$dbpass = "";
$dbname = "";
mysql_connect($dbhost,$dbuser,$dbpass) or die('cannot connect to the server');
mysql_select_db($dbname) or die('database selection problem');
$query = "SELECT * FROM tbl_uploads;"
//executing the query and printing it's results
$results= mysql_query($query);
print_r($results);
?>
This will print the results of query in the variable $results.
Note :
This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
mysqli_query()
PDO::query()
First, connect to your database (keinotis_iletisim). For example:
$dbhost = "localhost"; // or any other address
$dbuser = ""; // the user you created for the database in phpmyadmin
$dbpass = ""; // the password you created for the database in phpmyadmin
$dbname = "keinotis_iletisim";
Then, in a query you can mention a table, for example:
SELECT * FROM tbl_uploads;
Also see this link on W3Schools.
You can select your table using sql statement like this
$sql = "SELECT * FROM MyGuests";
$result = mysql_query($conn, $sql);
Now you can fetch the all records by using
while($row=mysql_fetch_array($result))
{
echo $row['column_name'];
}
This one will work for you
<?php
//the connction
$hostname_localhost = "localhost";
$database_localhost = "keinotis_iletisim";
$username_localhost = "root";
$password_localhost = "";
$localhost = mysql_pconnect($hostname_localhost, $username_localhost, $password_localhost) or trigger_error(mysql_error(),E_USER_ERROR);
?>
<?php
mysql_select_db($database_localhost, $localhost);
$query_record = "SELECT * FROM tbl_uploads";
$record = mysql_query($query_record, $localhost) or die(mysql_error());
$row_record = mysql_fetch_assoc($record);
$totalRows_record = mysql_num_rows($record);
//echo $row_record['tbl_uploads_table_colum'];
?>
<!--And if you want to display the list-->
<?php do { ?>
<?php echo $row_record['tbl_uploads_table_colum']; ?>
<?php } while ($row_record = mysql_fetch_assoc($record)); ?>
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();
?>
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 7 years ago.
Here is my code:
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pass = 'root';
$db_database = 'drmahima_com';
$link = mysqli_connect($db_host,$db_user,$db_pass,$db_database) or die('Unable to establish a DB connection');
mysqli_query($link, "SET names UTF8");
function temp() {
$patient = mysqli_fetch_assoc(mysqli_query($link, "SELECT name,dob FROM patients WHERE id='69'"));
echo $patient['name'];
}
temp();
?>
When I run this, the query doesn't seem to execute and nothing shows up on the page. However, if I remove the function and just change the code to this:
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pass = 'root';
$db_database = 'drmahima_com';
$link = mysqli_connect($db_host,$db_user,$db_pass,$db_database) or die('Unable to establish a DB connection');
mysqli_query($link, "SET names UTF8");
// function temp() {
$patient = mysqli_fetch_assoc(mysqli_query($link, "SELECT name,dob FROM patients WHERE id='69'"));
echo $patient['name'];
// }
// temp();
?>
It works, and the patient's name is displayed.
What is going on here?
Pass the $link to function as a parameter. Try this:
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pass = 'root';
$db_database = 'drmahima_com';
$link = mysqli_connect($db_host,$db_user,$db_pass,$db_database) or die('Unable to establish a DB connection');
mysqli_query($link, "SET names UTF8");
function temp($link) {
$patient = mysqli_fetch_assoc(mysqli_query($link, "SELECT name,dob FROM patients WHERE id='69'"));
echo $patient['name'];
}
temp($link);
?>
Pass $link to the function. It is not working because of the scope of that variable. Try with -
function temp($link) {
$patient = mysqli_fetch_assoc(mysqli_query($link, "SELECT name,dob FROM patients WHERE id='69'"));
echo $patient['name'];
}
temp($link);