Database selection (PHP) - php

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

Related

PHP error: Why isn’t this working, I can't see data result in my page?

I'am setting up a new wampserver and created a new database with a table named 'users'
apache 2.4.23 & PHP 7.1.10 & mySQL 5.7.14
<?php
$server = 'localhost';
$serverUsername = 'root';
$serverPassword = '';
$dbName = 'test';
$connection = mysqli_connect($server,$serverUsername,$serverPassword);
msqli_select_db($dbName);
if(!$connection){
echo 'connection failed to database '.mysqli_connect_error();
}
$sql = "SELECT * FROM users";
$query = mysqli_query($sql);
while($row = mysqli_fetch_array($query)){
print_r($row);
}
?>
my value is really in th data base but nothing appears in the page after running code
Have a look at the comments mentioning the fix
$server = 'localhost';
$serverUsername = 'root';
$serverPassword = '';
$dbName = 'test';
// FIX 1
// You need to mention the database name as the last argument
$connection = mysqli_connect($server,$serverUsername,$serverPassword, $dbName);
if(!$connection){
echo 'connection failed to database '.mysqli_connect_error();
}
$sql = "SELECT * FROM users";
// FIX 2
// The first argument should be your mysqli connection
$query = mysqli_query($connection, $sql);
// Check for errors
if (!$query) {
printf("Error: %s\n", mysqli_error($connection));
exit();
}
while($row = mysqli_fetch_array($query)){
print_r($row);
}
?>
Reference for mysqli_connect: https://secure.php.net/manual/en/function.mysqli-connect.php
Reference for mysqli_query: https://secure.php.net/manual/en/mysqli.query.php

Database doesn't load

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

Import data from mdb file to mysql database can not upload data

I'm using PHP code to upload or insert data from MDB file to MySQL database.I want my table value get inserted into MySQL database. But data does not inserted into MySQL database.This code shows me no error.Here is my code. please help I have tried every solution on net.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'payroll_system';
//mysql
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$sql = "SELECT * FROM attendance";
$result = mysql_query($sql);
//mdb
$conn2 = new COM("ADODB.Connection") or die("Cannot start ADO");
$conn2->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\\xampp\\htdocs\\payroll\\eTimeTrackLite1.mdb");
$rs = $conn2->Execute("SELECT * FROM AttendanceLogs");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$column1=$row["AttendanceLogId"];$column2=$row["AttendanceDate"];$column3=$row["EmployeeId"];$column4=$row["InTime"];
$column6=$row["OutTime"];$column8=$row["Duration"];
echo "hello";
echo $column1;echo $column2;echo $column3;echo $column4;
$rs->MoveFirst();
while (!$rs->EOF)
{
$attendance_id = $rs->Fields("AttendanceLogId");
$attendance_date = $rs->Fields("AttendanceDate");
$emp_id = $rs->Fields("EmployeeId");
$in_time = $rs->Fields("InTime");
$out_time = $rs->Fields("OutTime");
$duration = $rs->Fields("Duration");
mysql_query("UPDATE attendance SET AttendanceLogId = '$attendance_id', AttendanceDate='$attendance_date', EmployeeId='$emp_id',InTime='$in_time',OutTime='$out_time',Duration='$duration' '"); ?>
<?php
$rs->MoveNext();
}
}
?>
</table> <?php
mysql_free_result($result);
$rs->Close();
$conn2->Close();
$rs = null;
$conn2 = null;
?>
you need to INSERT each records data instead of UPDATE. Re-write the following line:
mysql_query("UPDATE attendance SET AttendanceLogId = '$attendance_id', AttendanceDate='$attendance_date', EmployeeId='$emp_id',InTime='$in_time',OutTime='$out_time',Duration='$duration' '");

Backing up a MySQL database using PHP

I was trying to make a backup of my MySQL db called "backup" using a PHP script below, but for some reason, it doesnt work. Any ideas what is wrong? I wanted to create a file called test.sql in the same folder that would contain the data (because the db is quite big I only selected values of Temp>35, but I could change that later). Right now when I run it, I get the echo, but no file created.
<?php
$dbhost = '...';
$dbuser = '...';
$dbpass = '...';
$dbname = 'backup';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$tableName = 'backup';
$backupFile = 'test.sql';
$query = "SELECT * WHERE Temp>35 INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query);
echo "Backed up";
?>
Just Try.
$query = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName WHERE Temp>35";
$result = mysql_query($query) or die(mysql_error());
Try this and let me know:
<?php
$dbhost = '...';
$dbuser = '...';
$dbpass = '...';
$dbname = 'backup';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$databasename = 'backup';
$backupFile = 'test.sql';
$query = "SELECT * INTO OUTFILE '$backupFile' FROM $databasename WHERE Temp>35";
$result = mysql_query($query);
echo "Backed up";
?>
<?php
$source_db='source_db';
$target_db='target_db';
$server='127.0.0.1';
$user='root';
$password='';
mysql_connect($server,$user,$password);
mysql_select_db($source_db);
// Get names of all tables in source database
$result=mysql_query("show tables");
while($row=mysql_fetch_array($result)){
$name=$row[0];
$this_result=mysql_query("show create table $name");
$this_row=mysql_fetch_array($this_result);
$tables[]=array('name'=>$name,'query'=>$this_row[1]);
}
// Connect target database to create and populate tables
mysql_select_db($target_db);
$total=count($tables);
for($i=0;$i < $total;$i++){
$name=$tables[$i]['name'];
$q=$tables[$i]['query'];
mysql_query($q);
mysql_query("insert into $name select * from $source_db.$name");
}
?>

Comparing database stringvalue with new stringvalue

Here is what I'm trying to do: When user adds a contact to his list, the number of this contact gets run by with the numbers in the database and it gives feedback if the user is already in the database or not. Right now I always get back "User is in database" even though he isn't. Then again I'm not that well acquainted with php. I changed the code a bit again, now it doesn't work at all, because it doesn't like the part
$number = ($_GET["number"] from $DB_Table);
Full code
<?php
$DB_HostName = "localhost";
$DB_Name = "db";
$DB_User = "user";
$DB_Pass = "pw";
$DB_Table = "contacts";
$number = ($_GET["number"] from $DB_Table);
$fnumber = ($_GET["fnumber"]);
if ($number == $fnumber) {
echo "This user is already in database";
} else {
echo "This user isn't in the database";
}
$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die (mysql_error());
mysql_select_db($DB_Name,$con) or die(mysql_error());
mysql_close($con);
?>
I don't actually see you executing the database query. You could do something like this:
<?php
$DB_HostName = "localhost";
$DB_Name = "db";
$DB_User = "user";
$DB_Pass = "pw";
$DB_Table = "contacts";
$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die (mysql_error());
mysql_select_db($DB_Name,$con) or die(mysql_error());
$fnumber = mysql_real_escape_string($_GET["fnumber"]);
$result = mysql_query("SELECT * FROM $DB_Table WHERE Something = '$fnumber'", $con);
if ($result) {
// Check the number of rows in the result set
if (mysql_num_rows($result) > 0) {
echo "This user is already in database";
}
else echo "This user isn't in the database";
}
mysql_close($con);
?>
This is not valid PHP code: $number = ($_GET["number"] from $DB_Table);
$_GET["number"] represents the value of the "number" parameter that you find in the url of your page.
Example: http://example.com/index.php?number=7 so $_GET["number"] is 7.
In your code, $DB_Table is a just a string ("contact") and "from" does not fit there using php syntax.
mysql_select_db($DB_Name,$con) or die(mysql_error());
is valid PHP but you are not doing anything with what you get from the database. I suggest you at least take a look at this tutorial php mysql select

Categories