For some reason, the following code inside the query works in my MySQL command console, yet when I try to run it as a Query in PHP, something keeps going wrong and I'm not sure what. Here is the code I've done so far.
//2. Perform database query
$query = "SELECT skills.element_id, content_model_reference.element_id, element_name FROM skills, content_model_reference WHERE (skills.element_id = content_model_reference.element_id)";
$result = mysql_query($query);
//Tests if there was a query error
if(!$result){
die("Database query failed.");
}
Is there something preventing the code that worked in MySQL (The line with SELECT) from working, or is my syntax somehow wrong?
EDIT: So it's saying I didn't select a database. Yet I thought I had. Here is the code above it:
//1. Create a database connection
$dbhost = "host"; //Host: Can be either an IP address, or a domain (like google.com).
$dbuser = "user";//User: The user that is connecting to the database.
$dbpass = "pass";//Password: This is the password that the user is using.
$dbname = "db";//Name: This is the name of the database.
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);//The value, 'handle,' is the connection.
//Test if connection occurred. Die ends the program/php, and in this case, also prints a message
if(mysqli_connect_errno()){
die("Database connection failed: ".
mysqli_connect_error().
" (". mysqli_connect_errno() . ")"
);
}
Like I said, the error message I am getting is pertaining only to the query, the server is fine with my database connection.
You're using mysqli_* for the connection, but you're using mysql_* for the QUERY... don't think you can do that, has to be one or the other (MYSQLI_ preffered). Also the query should be:
$result = mysqli_query($connection,$query);
Related
I'm relatively new to any type of programming or coding. I'm not quite understanding why no matter what adjustments I make to my php file I can't seem to pull any data from a table.
Here is a link to the table: https://i.gyazo.com/4ad5e860895014c49dbe0539c38cdec2.png
Above is the test table I have been trying to use. From what I can understand I'm connecting to the database okay, but all of my problems come after the connection. Also, I'm using php 7.0 so a lot of the information I'm finding online has not been helpful.
If there is something glaringly wrong with my table or in my code, please let me know.
Here is my code:
'''
//Set Variables
$serverName = "localhost";
$userName = "root";
$password = "";
$databaseName = "test";
//Create Connection
$connection = mysqli_connect($serverName,$userName,$password,$databaseName);
//Check Connection
if(!$connection){
die("Connection failed: ".mysqli_connect_error());
}
echo "Connected successfully <br>";
//Fetch Data
$query = "SELECT * from table1";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_array($result, MYSQLI_NUM);
printf ($row[1], $row[2]);
mysqli_free_result($result);
mysqli_close($connection);
I figured out the issue a few hours ago. The code I had posted would have worked perfectly fine if I was connecting to the port for MySQL rather than MariaDB. Didn't realize that the port that MariaDB was connected to was the default.
MariaDB by default was port 3306, but MySQL was 3308. After specifying 'localhost:3308' I was able to start properly pulling rows from my tables.
i want to move my sql database and i have exported and imported mysql.sql file from localhost to live server and now i m not getting the files and content from that database. what i do ? i did make sure connection to database if fine and successful
here's my page http://shooop23.byethost7.com
<?php
$db = mysqli_connect('','','','');
if(mysqli_connect_errno()){
echo'Database Connection Failed with following errors: '. mysqli_connect_errno();
die();
}
?>
Once you have successfully established a connection to MySQL, you need to perform a query specifying what you want to retrieve and then subsequently retrieve it.
The following example uses mysqli_fetch_row
You should explore the documentation to learn the basics.
$db = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
if(mysqli_connect_errno()){
echo'Database connection failed with the following error: '.mysqli_connect_errno();
exit;
}
if ($result = mysqli_query($db, "SELECT MyCol1,MyCol2 FROM MyTable")) {
while ($row = mysqli_fetch_row($result)) {
echo"{$row[0]} - {$row[1]}<br>");
}
mysqli_free_result($result);
}
mysqli_close($db);
$db = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
$sql="SELECT * FROM login";
here i connected and stored my database and set a sql command (which is now a string) into these two veriables.
if(mysqli_connect_errno()){
echo'Database connection failed with the following error: '.mysqli_connect_errno();
exit;
}
this is to check if the database is correctly connected, if not then it will show some errors.
$result = mysqli_query($db,$sql);
here i put the database and the sql command to run my query.
while($row = mysqli_fetch_array($result)){
echo $row['username'];
}
here finally outputting the usernames(username is one of the column in my table in this case) which matched with that query
i will suggest This Sql site to get a better understanding on sql queries and try to improve the secuirty because this is the basic point where hackers try to inject their attact most offenly.
Note : If your table's in the database are empty then it will not able to fetch anything
Just built my first database to test out learning skills using the simple query below:
<?php
mysqli_connect("xxx","yyy","zzz" , "newtone" );
if (mysqli_connect_error()){
echo ("could not connect to database");
}
?>
This is what I got
:(42000/1044): Access denied for user 'yyy'#'xxx' to database 'newtone'......
I don't get it. So When i Use this:
mysqli_connect("xxx","yyy","zzz" );
if (mysqli_connect_error()){
echo ("could not connect to database");
}
?>
I don't get an error message, just a blank page (notice how the database name is not included on the login argument). What am I doing wrong?
You want to connect with database then need three things for create connection dbhost username,dbhost password and your database name.
<?php
$dbhost = "localhost";
$dbuser = "root"; //In case of localhost (default db username)
$dbpass = ""; //In case of localhost
$dbname = "testDb";//Your database name
$con = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname );
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Your 2nd example works because you are successfully connecting to the MySql server, and defaulting to a default database other than "newtone"
If you tried a command like mysqli::select_db after connecting, you would see the same error. http://php.net/manual/en/mysqli.select-db.php
The crux of your problem seems to be permissions for your user yyy. Make sure user yyy has various permissions like read/write on the DB "newtone"
I have written that seems to not be working, but MySQL does not return any error. It is supposed to get data from database1.table to update database2.table.column
<?php
$dbh1 = mysql_connect('localhost', 'tendesig_zink', 'password') or die("Unable to connect to MySQL");
$dbh2 = mysql_connect('localhost', 'tendesig_zink', 'password', true) or die("Unable to connect to MySQL");
mysql_select_db('tendesig_zink_dev', $dbh1);
mysql_select_db('tendesig_zink_production', $dbh2);
$query = " UPDATE
tendesig_zink_dev.euid0_hikashop_product,
tendeig_zink_production.euid0_hikashop_product
SET
tendesig_zink_dev.euid0_hikashop_product.product_quantity = tendesig_zink_production.euid0_hikashop_product.product_quantity
WHERE
tendesig_zink_dev.euid0_hikashop_product.product_id = tendesig_zink_production.euid0_hikashop_product.product_id";
if (mysql_query($query, $dbh1 ))
{
echo "Record inserted";
}
else
{
echo "Error inserting record: " . mysql_error();
}
?>
The manual page for mysql_error() mentions this about the optional parameter you're omitting:
link_identifier
The MySQL connection. If the link identifier is not
specified, the last link opened by mysql_connect() is assumed. If no
such link is found, it will try to create one as if mysql_connect()
was called with no arguments. If no connection is found or
established, an E_WARNING level error is generated.
So it's reading errors from $dbh2, which is the last connection you've opened. However, you never run any query on $dbh2:
mysql_query($query, $dbh1 )
Thus you get no errors because you are reading errors from the wrong connection.
The solution is to be explicit:
mysql_error($dbh1)
As about what you're trying to accomplish, while you can open as many connections as you want, those connections won't merge as you seem to expect: they're independent sessions to all effects.
All your tables are on the same server and you connect with the same users, there's absolutely no need to even use two connections anyway.
You can't just issue a cross-database update statement from PHP like that!
You will need to execute a query to read data from the source db (execute that on the source database connection: $dbh2 in your example) and then separately write and execute a query to insert/update the target database (execute the insert/update query on the target database connection: $dbh1 in your example).
Essentially, you'll end up with a loop that reads data from the source, and executes the update query on each iteration, for each value you're reading from the source.
I appreciate everyone's help/banter, here is what finally worked for me.
<?php
$dba = mysqli_connect('localhost', 'tendesig_zink', 'pswd', 'tendesig_zink_production') or die("Unable to connect to MySQL");
$query = " UPDATE
tendesig_zink_dev.euid0_hikashop_product, tendesig_zink_production.euid0_hikashop_product
SET
tendesig_zink_dev.euid0_hikashop_product.product_quantity = tendesig_zink_production.euid0_hikashop_product.product_quantity
WHERE
tendesig_zink_dev.euid0_hikashop_product.product_id = tendesig_zink_production.euid0_hikashop_product.product_id";
if (mysqli_query($dba, $query))
{
echo "Records inserted";
}
else
{
echo "Error inserting records: " . mysqli_error($dba);
}
?>
I am trying to connect to a mysql database within a php script. So far, my code looks like
//to my knowledge, this works. I was able to echo out the correct name
$name = $_POST["name"];
$server_name = "localhost";
$user_name = //my user name
$password = //my password
$db_name = //the db name
//it passes this error check, so I am connecting properly I am assuming
$dbconn = mysql_connect($server_name, $user_name, $password)
or die ('Could not connect to database: ' . mysql_error());
mysql_select_db($db_name, $dbconn);
$query = "SELECT *
FROM brothers
WHERE name = '$name'";
//it DOES NOT make it past this one
$result = mysql_query($query)
or die('Bad Query: ' . mysql_error());
//filter through the query as a row
$row = mysql_fetch_array($result, MYSQL_ASSOC);
//echo the result back to the user
echo $row["name"];
echo $row["major"];
//close the connection
mysql_close($dbconn);
I keep getting the error "No Database Selected", even though I am sure that I spelled the database name correctly (I copy pasted). Does anyone know why my code might be throwing this error?
Could be a permissions issue(the user which logon maybe don't have read rights on the database) :
mysql_select_db() fails unless the connected user can be authenticated
as having permission to use the database.
Here's my comment in answer form:
Do the same or die ('Could not use DB: ' . mysql_error()) after the mysql_select_db() call to find out what's going on there.
Your comment ("access denied") suggests that you have permissions to connect to the database server but not to use the database. Remember that one database server can have multiple databases on it, each with their own permission scheme. Check the permissions on the database you're trying to use and make sure that $user_name can use it with $password.