PHP to display data from SQL table on website - php

I have a database hosted on my computer using MySQL Workbench. I have a website hosted on my computer using IIS. I have created a database fennypvp and a table accounts with columns id, username and password. I have added multiple entries into the table from MySQL Workbench. Now I am simply trying to get these entries from the website with PHP. For now I'd simply like to be able to print the entries and from there I will work on validating credentials etc. I have look up quite a bit and nothing has worked for me. Here is what I've got so far:
<?php
$host="127.0.0.1";
$port=3306;
$socket="";
$user="root";
$password="";
$dbname="fennypvp";
$con = new mysqli($host, $user, $password, $dbname, $port, $socket)
or die ('Could not connect to the database server' . mysqli_connect_error());
$query = "select * from accounts";
if ($stmt = $con->prepare($query)) {
$stmt->execute();
$stmt->bind_result($field1, $field2);
while ($stmt->fetch()) {
printf("%s, %s\n", $field1, $field2);
}
$stmt->close();
}
$con->close();
?>
The above is code generated by MySQL Workbench although the page sql.php is blank. Any ideas?
Here is the database and table information:

Related

checking the database query ,always shows (database query failed)

i'm in a middle of making a query with my new database and i keep getting the message (Database query failed) through the code below:
<?php
//create a database connection
$dbhost= "localhost";
$dbname= "widget_corp";
$connection=mysqli_connect($dbhost , $dbname);
if(mysqli_connect_errno()){
die("Database connection failed :" . mysqli_connect_error ."(". mysqli_connect_errno .")");
}
?>
<?php
//perform a database query
$query = "SELECT * FROM subjects";
$result = mysqli_query($connection ,$query);
if (!$result){
die("Database query failed.");
}
?>
please advise
You aren't passing in a username or password. mysqli_connect() requires four parameters to be passed in : http://php.net/manual/en/function.mysqli-connect.php
You're only passing in a host and database name.

PHP mysql_query inside FOR only works first loop

I´m trying to update some different registers in a mysql database sending the commands from a FOR loop in php, but the query is only done the 1st loop. Here´s the code:
$conexion = mysql_connect($hostname, $user, $pass) or die ("Error establishing connection with the Database");
mysql_select_db($db,$conexion) or die("Error selecting the Database");
$j=0;
for ($i=0;$i<count($notifs);$i++){
$sql="UPDATE tef SET notif='$notifs[$i]' WHERE sn_rec='$unsersn_recs[$j]';";
echo $sql."<br>";
$res=mysql_query($sql, $conexion) or die (mysql_error());
$j++;
}
mysql_close($conexion);
The query text is correctly done (the echo shows the different lines created), but the changes in the database are done only in the 1st loop (1st query) and I don´t receive any error. What may I be missing?
Thanks in advance!
This is wonderful example where you should use prepared statements.
I give you an example which is also secure against SQL injections.
$mysqli = new mysqli($hostname, $user, $pass, $db);
if (mysqli_connect_errno()) {
die("Error establishing connection!");
}
$stmt = $mysqli->prepare("UPDATE tef SET notif=? WHERE sn_rec=?");
$j=0;
for ($i=0;$i<count($notifs);$i++) {
$stmt->bind_param('ii', $notifs[$i], $unsersn_recs[$j]);
$stmt->execute();
if(!empty($stmt->error)) echo $stmt->error;
$j++;
}
$stmt->close();
$mysqli->close();
Hint: If notif or sn_rec are varchar/text types, just replace the 'i' with a 's' in bind_param().

mysql_select_db("db_name") doesn`t work with freshly created DB

My PHP code
$handle = mysql_connect();
echo ($handle) ? "Connected to mysql" : "Could not connect";
$db = mysql_select_db("users");
if ($db ==false) {
die(' Could Not select database so Exiting;');
} else {
echo " Database selected;";
}
mysql_close($handle);
This code works with default mysql databases like "test" or "mysql".
My Data Base creation code sequence:
mariadb[none]> create database users;
mariadb[none]> use users;
mariadb[users]> create table registered_users ( user_name varchar(18), user_pass varchar(18) );
You should use mysqli_ functions instead:
$localhost=''; //SQL Host
$dbuser=''; //Database Username
$dbpass=''; //Database Password
$dbname=''; //Database Name
$connect=mysqli_connect($localhost,$dbuser,$dbpass,$dbname);
mysql_ functions are deprecated.
What's basically happening is you are connected to test by default please supply parameters mysql_connect("localhost", "username", "password") ;
Should work with no doubt.

connect oracle database with php and execute sql?

Hey guys im trying to connect to an oracle database with php. I tried it like i do it with mysql.
How to do it like this:
$host="localhost";
$user="username";
$pass="password";
$db="database";
$link = mysql_connect($host, $user, $pass) or die ("Keine Verbindung zu der Datenbank moeglich.");
mysql_select_db($db, $link);
$sql = "SQL query goes here";
$result = mysql_query($sql);
How can i do exact this with an oracle database. I have the following connection details
sid, ip, port, username, password.
Simple script:
$DB = '//1.2.3.4:1521/XE';
$DB_USER = 'user';
$DB_PASS = 'pass';
$DB_CHAR = 'AL32UTF8';
$conn = oci_connect($DB_USER, $DB_PASS, $DB, $DB_CHAR);
$statement = oci_parse($conn, 'select 1 from dual');
oci_execute($statement);
$row = oci_fetch_array($statement, OCI_ASSOC+OCI_RETURN_NULLS);
To connect with an Oracle database, you don't use the mysql extension (since that is for MySQL). You should use PDO, with the OCI/Oracle adapter.
You'll want to use PDO to connect to Oracle here is the PHP manual page on creating a connection using PDO, the example given is for MySQL but it will work fine with Oracle. You will need to ensure the PDO:Oracle extension is installed and running on your PHP configuration.

Connecting to remote mySql database manually

I want to know how to connect to a MySql database in a remote server from windows 7 .I want to give an external connection from Administrative panel.How to give connection to the database ? I have a php coding to insert and retrieve values from the remote database .But before that i need to connect to the remote database manually ? Pls can you help me out with this .#
<?PHP
$user_name = "fbapp";
$password = " 123";
$database = "fb_db";
$server = "mysql.dfw1.stabletransit.com";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$SQL = "INSERT INTO Sample(Firstname, Lastnamme)
VALUES (35,67)");
$result = mysql_query($SQL);
mysql_close($db_handle);
echo "Records added to the database";
}
else {
echo "Database NOT Found ";
mysql_close($db_handle);
}
?>
Simply create a mysql user and allow the same from your host to connect. So create user at
example1.com MYSQL and allow your new user to connect from your example2.com.
Then allow a firewall rule on mysql port from that very host
open command prompt
mysql -u user_name -ppassword -h ip-adress
use ip-adress of machine where mysql is running

Categories