phpmyADMIN or mySQL? I am a bit confused with PHP - php

Hey guys the code below is what I am trying to run. I am trying to run a practice MySQL server because I am going to host it on my schools free database which is MySQL
I am using phpmyADMIN but I am a bit confused because yall are saying I am not connecting to MySQL
so what is this code to create a database? Do I need to download MySQL or something? I thought phpmyadmin is the same syntax to create a database/tables/ and values? since I have MySQL turned on?
//creation of database
$sql= 'CREATE DATABASE project';
if(mysql_query($sql,$con)){
echo 'DB created succesfully';
}
else{
echo 'error creating DB' . mysql_errno();
}

PHPMyAdmin is a web-based software program created with PHP to help you administrate MySQL databases. It has its own internally built code to connect to your MySQL databases and perform standard functions such as insert, select, delete, update, etc.
PHPMyAdmin does not really have anything to do with you creating your own PHP program. You will still need to connect to MySQL (as PHPMyAdmin does behind the scenes). You can still use the same code that PHPMyAdmin generates for you for use in your own PHP programs, but you must already be connected to MySQL as a user with valid permissions.
In your PHP code it is important to note that you are using the mysql_* functions which have been deprecated and you should be using the mysqli_* functions instead.
Your code should actually look something like this:
//connect to db
$link = mysqli_connect("localhost","my_user","my_password");
//creation of database
$sql= 'CREATE DATABASE project';
if(mysqli_query($link, $sql)){
echo 'DB created succesfully';
}
else{
echo 'error creating DB' . mysqli_errno();
}

MySQL is the database server you connect to in order to execute queries such as CREATE DATABASE project. phpmyadmin is a PHP-based front end to MySQL to allow easier browsing and manipulation of the database.
Think of text documents on your computer. notes.txt is a text file, but you could open it with any text editor such as Notepad or TextEdit.
Your specific snippet of code is missing the database connection string, so something more appropriate might look like:
$connection = new mysqli('db_host', 'db_user', 'db_pass');
$sql = "CREATE DATABASE project";
$results = mysqli_query($connection, $sql);
//check for errors
Without connecting to the database, your SQL is all just strings of text with no meaning.

I tried a lot hoping to get a good job here
If you are logged into phpmyadmin, then the only code you can execute is just SQL statements. Like: create database my_db1

Related

Import from sql file without command line

Using PHP, I have a sql file which I want to import into my database.
Unfortunately, I can't use any command (php exec).
How can I do that (without phpMyAdmin and co) ?
Thanks.
Or with PHP only.
Please note the SQL file should include USE database statement(s) because mysqli_connect doesn't use a default database.
SQL file code
USE stackoverflow;
CREATE TABLE test() (
id int
);
PHP code
<?php
$fileContentsString = file_get_contents([path_to_sql_file]);
$connection = mysqli_connect("127.0.0.1", "username", "password");
mysqli_multi_query($connection, $fileContentsString);
?>
Better use MySQL Workbench. MySQL Workbench is a visual database design tool that integrates SQL development, administration, database design, creation and maintenance into a single integrated development environment for the MySQL database system
if you have access to phpMyAdmin, then you should be able to create a database
create database
using the xampp or wampp GUI, select the newly created database and then click import import and then select the sql file select sql file you want to upload and click go click go to complete process at the bottom, it should then bring a message Import successful.... after the entire process, you are good to go

i dont wan to keep dbname before each table in mysql

I have installed my php website with database in IIS, windows 2012, The problem is it is accepting if i mention db name before table name for each query in my website. example below
select userid,profile_id,email,first_name,last_name,password,login_flag,usertype
from reqsbook.user
where email='$email'
and password='$password'
and usertype='$usertype'
actually i used to run this website in linux web server but as per my client requirement i have installed it in iis server.
please let me know is there any possibility for remove db name before table in all queries in my project or can i keep db name at once place for universal
Three ways to do this, assuming your database name is reqsbook.
Give the SQL command USE reqsbook; immediately after connecting from php to your dbms, before you give any SELECT or other queries.
if you're connecting with mysqli_, use something like this:
$mysqli = new mysqli('hostname', 'username', 'REDACTED_PASSWORD', 'reqsbook');
If you're connecting with PDO, try something like this
$db = new PDO('mysql:host=hosthame;dbname=reqsbook', 'username', 'REDACTED_PASSWORD');
If you're connecting with mysql_, well, stop doing that unless you want your customer's application pwned by cybercriminals.

Unable to read an MS Access Table (linked to an ODBC Data Source) using PHP

I am working on an application in which the data from an ODBC enabled Data Source is imported to an MS Access 2007 Database using a Linked Table. The tricky part is that every time a Link Refresh is made (or the linked table is opened) the ODBC Data Source prompts out a Window where 4 different parameters (username, password, servername, workgroupname) have to be input.
While this is possible manually, I find no way to read this linked Table programatically.
I am using a PHP Script to read the linked table. The Execution of PHP Script is halted indefinitely when I try to read this linked table. My DSN is properly configured and can successfully read the data in all other cases including those linked tables in which the data source doesnot require any input parameters/credentials. However, it fails in this case.
In a nutshell, my system is PHP<-->MS Access (Linked Table)<--ODBC Data Source. The PHP Script aims to read the updated data from the ODBC Data Source using the Linked Table. For some reasons, PHP cannot directly interface with the ODBC Data Source, so I am using MS Access as an intermediary.
The odbc_connect function of PHP allows only DSN Name, username, password, cursor_type as parameters. I am not sure how do I input the 4 parameters (username, password, servername, workgroupname) as required by the ODBC Data Source to which my MS Access table is linked.
I am using PHP 5.4 on Windows 7 with MS Access 2007.
I request the developer community to help me with necessary pointers on this. Any suggestions for Workaround is also welcome.
Thanks in Advance.
if you are using double ODBC: PHP > ODBC > MS ACCESS > ODBC > SOURCE.. you have following options.
Cut off the "Access" middle man and connect directly.
if you want to use MS Access, use "FILE DNS" to connect to the linked tables from MS ACCESS where file DNS has all 4 parameters initialized. (This will eliminate MS Access asking you to enter the server information when refreshing linked tables)
Haven't tried this from PHP side but you can loop through the msysobjects table or database.tabledefinitions and update the link with your custom connection string.
Any way, the solution for your trouble would be. Make Access not to prompt for parameters when refreshing linked tables by either using file dns or save passwords.
Connect directly to the data source where linked table derives. If that is MS Access, SQL Server, MySQL, etc. use the appropriate, installed ODBC driver for that particular database.
Here is a PHP/MS Access DSN-less solution using PDO.
Also, see different connection strings which includes workgroup, user, and password parameters.
<?php
$database="C:\Path\To\database.accdb";
# open the connection
try {
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};
DBq=$database;Uid=Admin;Pwd=;");
$sql = "SELECT * FROM table1";
$STH = $dbh->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
echo "\n";
while($row = $STH->fetch()) {
# output query results
echo $row;
}
# close the connection
$dbh = null;
?>

Update Access Database from PHP script?

I'm relatively new to PHP and databases. Currently I'm working with an existing Access database and am able to read and display the data just fine. However, I'm wanting to update a user record through an email verification link. I have the server send an email with a link of www.domain.com/verify.php?userID=#
I am able to read this GET variable just fine, but I'm completely lost as to how I update the record. Everything I search for is for updating MySQL databases whereas I'm using odbc.
Does anyone know how I could set this up?
something like this:
odbc_exec($conn, "UPDATE table SET field=value"); // $conn is your connection identifier
You need to use a ODBC database client library in PHP to connect to a ODBC datasource. For example you can use PDO with the driver ODBC and DB2 Functions (PDO_ODBC).

Can MySQL tables be created using only PHP?

I don't know much about programming, so I hired someone to create a simple MySQL database with an online form that I can enter information into.
I already setup the MySQL database at my hosting account and gave the programmer the login details as well as an FTP account.
He says he needs my hosting username/password in order to create the database because he needs to use PHPMyadmin to create the tables and test the database.
This seems kinda shady to me. Can't the programmer create the tables using PHP commands alone? I don't want to give out my main username/password to a stranger.
I'm hosted at 1and1.com if that matters any
If you give the MySQL user sufficient database permissions, they can use the CREATE TABLE statement to create the tables without having to use PHPMyadmin. If they don't know how to create tables without using the GUI, then I would be suspicious of their overall database programming abilities as being the lead on your project.
PHP can execute any SQL statement on a MySQL server which it has a valid connection too, including the CREATE TABLE command, however, these will be subject to the "privileges" of the user that PHP used to gain the MySQL connection.
The MySQL user that you provided the login credentials for may not have create table privileges. In this case, PHP could do nothing, as the SQL commands executed would fail.
That said, I don't know why your hosting username/password would be needed to use PHPMyAdmin. Usually a login to PHPMyAdmin is done with the MySQL user, not the hosting user. Though, that user would be subject to the same privilege restrictions, so we're back at part 1:
Does the MySQL user you created for the programmer have create table privileges?
If yes, they should be able to create tables from PHP alone, and if you have PHPMyAdmin installed, they should be able to log in using that same user already (and not your hosting user!)
If no (probably unlikely, though I don't know 1and1), then you will need to provide a MySQL user that does have create table privileges before they can do anything; then see above.
It's possible to submit the necessary CREATE TABLE queries via php to the database, but the developer is likely more familiar with the way PHPMyAdmin displays what is set up on the server.
Sounds viable to me. Technically he can do everything with PHP alone, but it is much much easier to use PHPMyAdmin.
I think you should hire someone you can trust and give them the tools to help them do a good job.
You don't have to give him your user/pass. You can create a new user in PHPMyAdmin, with less privileges. When he finishes installing your database, just delete that user.
Yes, he can create databases using only PHP, but it is slower than just use PHPMyAdmin.
Probably the person you hired is not very skilled in software development and the only way he know for modifying MySQL is using that web interface.
You can do it this way:
http://www.w3schools.com/php/php_mysql_create.asp
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Create database
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
// Create table
mysql_select_db("my_db", $con);
$sql = "CREATE TABLE Persons
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";
// Execute query
mysql_query($sql,$con);
mysql_close($con);
?>
I might also create a DBUser that has access to the database and tell him to download a mySQL client (such as HediSQL) to do his database work. Then after they are done, just remove the DBUser (or take away all permissions).

Categories