Read only mysql database - php

I'm trying to get work this small project. I have master-admin over classic admin.
my database connection is establish on cookie $key. If i have cookie google, it looks into my master-admin database and search there for DB_PASSWORD, DB_HOST etc. And then it build config.php.
This cookie is set to expire after few days.
This is my config.php :
<?php
$key = $_COOKIE["nazev_webu"];
$sql = mysql_query("SELECT * FROM weby WHERE nazev='$key'");
while($row = mysql_fetch_array( $sql )) {
// Print out the contents of each row into a table
$name = $row['DB_NAME'];
$pass = $row['DB_PASSWORD'];
$user = $row['DB_USER'];
$host = $row['DB_HOST'];
}
//připojení dataze
$connect = mysql_connect($host, $user, $pass)
or die("Nelze se připojit k databázi");
//výběr databáze
$select = mysql_select_db($name ,$connect)
or die("Nemohu vybrat databázi");
?>
it works perfectly but only in "read only mode". I can select from database but if I'm trying to insert or update something i get this error.
Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /data20/website/html/config.php on line 7
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /data20/website/html/config.php on line 7
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /data20/website/html/config.php on line 9
Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /data20/website/html/config.php on line 20
I have to say that insert and update action are build on ajax call, so i post via ajax id of article f.e.. to the update script.
I think problem can be that mysql are on another server, not on the same server as is my website.

The solution is too easy. I forgot to include static database connection into this file. So it can't look into database and get from here connection details for new config.php.

Related

Problems converting mySQL_pconnect to mySQLi_connect

I recently added an SSL certificate to my website and once it activated I got the following error when trying to view pages which are behind the login wall:
Warning: mysql_pconnect(): Access denied for user 'refini14_admin'#'70.39.144.73' (using password: YES) in /home/refini14/public_html/Connections/Database.php on line 9
Fatal error: Access denied for user 'refini14_admin'#'70.39.144.73' (using password: YES) in /home/refini14/public_html/Connections/Database.php on line 9
My hosting company's support has told me that this is because mysql_pconnect is not supported in current versions of php and that I should modify it to use a different sql connection method.
I have tried to modify my database connection file to connect using mysqli however am still getting errors. This is the code I am currently using to connect (I have replaced some details with *):
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_Database = "******";
$database_Database = "refini14_db";
$username_Database = "refini14_admin";
$password_Database = "*******";
$Database = mysqli_connect($hostname_Database, $username_Database, $password_Database) or trigger_error(mysqli_error(),E_USER_ERROR);
?>
I have only made modifications to line 9 as per the error message and have changed mysql_pconnect to mysql_connect. I am still getting the following error relating to mysqli_error():
Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/refini14/public_html/Connections/Database.php on line 9
Fatal error: in /home/refini14/public_html/Connections/Database.php on line 9
I am not experienced in mySQL and do not know how to resolve this error. Modifications I have made to mysqli_error() have not resolved the problem.
add your value and try:
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
mysqli_error() needs your connection variable i.e. $Database as a parameter so try it like this -
$Database = mysqli_connect($hostname_Database, $username_Database, $password_Database) or trigger_error(mysqli_error($Database),E_USER_ERROR);
instead of this -
$Database = mysqli_connect($hostname_Database, $username_Database, $password_Database) or trigger_error(mysqli_error(),E_USER_ERROR);

Why do I get an internal server error from my SQL query?

I am attempting to retrieve some data from my database, I appear to be able to connect to the database as I do not get an error message after the connection is tested. But when I attempt to run a select query, it causes an internal error.
If I take the FROM part away from the query, the page loads with no data. If I add the FROM part back in, with the table to be selected from, the internal error occurs.
The code is below:
$con = mysqli_connect("50.62.209.87:3306","myusername","mypassword",
"FiveExtras");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT `EventVenue`,`EventDate`,`EventTime`,
`EventPostCode` FROM `event`";
$result = mysqli_query($con, $query);
$row = $result;
echo $row;
Rewrite your connection:
$db_conn = mysqli_connect("$host", "$dbuser", "$dbpass") or die("Unable to connect to the database");
mysql_select_db("$dbase", $db_conn) or die("Unable to select the database");
$query = mysqli_query("SELECT `EventVenue`,`EventDate`,`EventTime`,
`EventPostCode` FROM `event`");
And why $result = $row ?
EDIT: For a better query , use "SELECT * ... "
did you change your htacess? Changes to .htacess often cause internal server error. or your host ip is bad(try no-ip.com, they give free subdomains for your ip's)
i checked it with removed port it worked(well...not connecting but no error about host), so maybe it's something to do with port
if error 500 remains try get support ticket or email webmaster or get another hosting provider
why would you say no error?
here we go:
Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given
in /home/u451361813/public_html/test.php on line 13
and
Warning: mysqli_connect(): (28000/1045): Access denied for user
'myusername'#'185.28.20.221' (using password: YES) in /home/u451361813
/public_html/test.php on line 3
Failed to connect to MySQL: Access denied for user
'myusername'#'185.28.20.221' (using password: YES)
1st one is for no information returned second for no password

php warning when logging in website folder by cpanel

I am writing some php script in the cpanel. When I access the website folder
(e.g. www.bbbbbb.com/folder/)
It give the warning like that.
Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2005): Unknown MySQL server host 'host07.portalwebhosting' (1) in /home/restaur/public_html/restaurant/variables/variables.php on line 7
Warning: mysqli::set_charset() [mysqli.set-charset]: Couldn't fetch mysqli in /home/restaur/public_html/restaurant/includes/connect_database.php on line 3
Warning: mysqli::stmt_init() [mysqli.stmt-init]: Couldn't fetch mysqli in /home/restaur/public_html/restaurant/includes/login_form.php on line 45
Fatal error: Call to a member function prepare() on a non-object in /home/restaur/public_html/restaurant/includes/login_form.php on line 46
And I use the default account admin to access, it gives me the same result.
And here is my php coding
<?php
// database configuration
$host ="host07.portalwebhosting.com";
$user ="user";
$pass ="password";
$database = "monkey";
$connect = new mysqli($host, $user, $pass,$database) or die("Error : ".mysql_error());
// access key to access API
$access_key = "12345";?>
check your connection string or may be you need to try with port
$connection = new mysqli("host", "user", "pwd", "db", "3306");
no need to change port 3306 is default
why you use die("Error : ".mysql_error()); use mysqli

Registered User counter

Ok so have this code that is supposed to show the users name and then show the number of registered users. So my problem is that is shows the users name but won't show the number of users. I keep getting this error.
Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/68/10509868/html/login-home.php on line 147
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/content/68/10509868/html/login-home.php on line 147
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
AND this is my code that works.
Welcome back <?= $fgmembersite->UserFullName(); ?>!
And this is my code that doesn't seem to work
We have
<?php
$sql = mysql_query("select * FROM users")or die(mysql_error());
$nrows = mysql_num_rows($sql);
echo $nrows;
?>
registered users now!
And this is how I connect to my database
<?php
$con=mysqli_connect("host IP","username","password");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
MySQLi and MySQL are different libraries. mysql_query is a command used with the MySQL library (and which is being deprecated) but you are connecting using the MySQLi libarary. Do do a query with MySQLi you should look at the PHP manual here: http://www.php.net/manual/en/class.mysqli.php

Lost connection to MySQL server during query - PHP, MySQL

I have this php code, the files are hosted on another server and db else where
$hostname_xxx = "xxxdb.db.234141.mysqldbhosted.com";
$database_xxx = "xx11xx";
$username_xxx = "xx11xx";
$password_xxx = "xx332211xx";
$shasown = mysql_pconnect($hostname_xxx, $username_xxx, $password_xxx) or trigger_error(mysql_error(),E_USER_ERROR);
$your_ip = $_SERVER['REMOTE_ADDR'];
echo $your_ip;
$insertSQL1 = "INSERT INTO table (users_ip) VALUES ('$your_ip)";
mysql_select_db($database_xxx, $xxx);
$Result21 = mysql_query($insertSQL1, $xxx) or die(mysql_error());
The error I am getting is
Warning: mysql_pconnect() [function.mysql-pconnect]: Lost connection to MySQL server during query in /domains/4444.com/html/55.php on line 8
Fatal error: Lost connection to MySQL server during query in /domains/4444.com/html/55.php on line 8
Thanks
Jean
mysql_pconnect() creates a persistent connection to the database, whereas mysql_connect() does not. IF you are creating a persistent connection, you need only connect once throughout your session, so if you're creating a persistent connection more than once this may be the cause.
On shared servers it may be worth trying mysql_connect() over mysql_pconnect() and see if this corrects the issue at hand. Also, in your code you have:
$Result21 = mysql_query($insertSQL1, $xxx) or die(mysql_error());
But should be:
$Result21 = mysql_query($insertSQL1, $shasown) or die(mysql_error());
because $xxx was never a connection variable but $shasown is.
Personally I like to use mysqli_connect() as I find it to be a little faster.

Categories