Uploading my site from XAMPP to Apache public host - php

This is a general question.
I`ve compliled site in xampp and it is properly running, because lack of experience I used mysql functions rather than the improved mysqli.
Uploading my site on ecowebhost the site does not work, I have changed paths so that, apparently, no connection error happens. but still I cannot interact with my database.
From the site seems that php4 version are supported, am I required to recompile my web site because of that?
this is a little sample code of what I am trying to do...
<?php
function connectto($tablename){
$db_host = "xxx";
$db_username = "xxx";
$db_pass = "xxx";
$link = mysqli_connect("$db_host","$db_username","$db_pass","$tablename") or die ("Could not connect to MySQL");
// #mysql_connect("$db_host","$db_username","$db_pass") or die ("Could not connect to MySQL");
// #mysql_select_db("$tablename") or die ("No database");
}
?>
Thanks
if I run the following
<?php
// connection to database
// include_once "connectto.php";
// connectto('test');
$tablename = "test";
$db_host = "xxx";
$db_username = "xxx";
$db_pass = "xxx";
$link =mysqli_connect("$db_host","$db_username","$db_pass","$tablename")
or die ("Could not connect to MySQL");
if (mysqli_connect_errno()) { die("Failed to connect to MySQL: " . mysqli_connect_error());}
else { echo " <br /><br />connection established! <br />";}
?>
the only error message that comes out is
Could not connect to MySQL
update....
I got a bit of my code working but I am struggling with finding a way to insert some values in the database...
<?php
$db_host = "xxxx";
$db_username = "xxxx";
$db_pass = "xxxx";
// $link = mysqli_connect("$db_host","$db_username","$db_pass","cl45-members-7b5") or die ("Could not connect to MySQL");
#mysql_connect("$db_host","$db_username","$db_pass") or die ("Could not connect to MySQL");
#mysql_select_db("cl45-members-7b5") or die ("No database");
$sql ="INSERT INTO members (username, email, gps_lat, gps_long, password, skill, skill_rate,) VALUES ('a','2','3','4','5','5','6')";
print '<br /><br /> Great! now you are registered; now update your profile <br />';
mysql_query ("$sql");
print '<br /><br />tutto ok;<br /><br />';
?>
Is there anything that strikes being wrong? I got no error like database not found...

Your question is confusing me slightly but you can connect using MySQLi using the following:
$link = new mysqli($db_host,$db_username,$db_pass,$tablename);
In your code you have
or die ("Could not connect to MySQL");
Which says if you can not connect for whatever reason just print out this. You're not going to get any more information because you need to include $conn->connect_error e.g.
if ($link->connect_error) {die ("Failed: " . $link->connect_error);}
You should end up with something like
$tablename = "test";
$db_host = "xxx";
$db_username = "xxx";
$db_pass = "xxx";
$link = new mysqli($db_host,$db_username,$db_pass,$tablename);
if ($link->connect_error) {
die ("Failed: " . $link->connect_error);
} else {
echo " <br /><br />connection established! <br />";
}

After some wondering around and splitting the problem in little subtasks, i believe I managed to understand that that little stupid extra comma in the list was preventig me to run the program, I copy pasted a syntax shared on-line and now everything seems to work...
outcome:
- check the spelling
- Isolate problems one by one...

Related

database does not exist. but it does

So I wanted to install an acp on my homepage. I uploaded all needed files on my webserver (I use FileZilla Client) and opened phpMyAdmin with xampp.
It connects the database but then it says
"Database does not exist."
I'm fairly new to working with phpMyAdmin and mySQL as well.
this is what my db.php looks like:
<?php
$host = "localhost"; // database server
$user = "akichuchan"; // db username
$pw = "cherryjuice"; // db password
$db = "acp"; // db name
$link = mysql_connect($host, $user, $pw, $db) or die ("No connection possible.");
mysql_select_db($db, $link) or die ("Database does not exist.");
?>
What did I do wrong?
Thanks in advance. :)
Try the below working code:
<?php
$host = "localhost"; // database server
$user = "akichuchan"; // db username
$pw = "cherryjuice"; // db password
$db = "acp"; // db name
$link = mysql_connect($host, $user, $pw) or die ("No connection possible.");
mysql_select_db($db, $link) or die ("Database does not exist.");
?>
NOTE: Don't use " mysql_ ". Better to use " PDO or Mysqli_ " ( safe and secure ) .

Not able to connect to mysql php/xampp

my I am trying to make login registration page in php.
apache and mysql is up and running.
I am executing the following code
<?php
$db_host = "localhost:777";
$db_username = "root";
$db_pass = "";
$db_name = "login";
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");
?>
I am using localhost:777 because port 80 is being used by skype.
phpMyAdmin is up and running.
The output i am getting is nothing but the above code only.
can anyone help me on this issue?
Use MySQLi or PDO not MySQL.
You're passing the variables as a string using " " in the mysql_connect function
The syntax is as follows: mysqli_connect(host,username,password,dbname,port,socket);
<?php
$db_host = "localhost";
$db_port = "777";
$db_username = "root";
$db_pass = "";
$db_name = "login";
$conn = mysqli_connect($db_host,$db_username,$db_pass,$db_name,$db_port);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Try that and it should work. Please make sure you understand the code above and the error you made. Do not simply copy and paste please you will not learn from it.

trying to get PHP and database to work together

I'm trying to teach myself how to work with php and mysql. For practice, I made a simple test website that takes a username and a password, and a database using phpmyadmin and mysql. I was able to create a successful connection to my DB, but now I'm trying to take the data from the form on my website and insert it into a table called 'account'. Account has three fields: 'username', 'password', and 'userID'(primary key). user ID is supposed to auto increment, so that field doesn't require input data. I wrote code that I thought would collect the username and password and add it as a new record in the account table, but I get an error message:
Internal Server Error
The server encountered an internal error or misconfiguration and was
unable to complete your request.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
Below I've included my code. I've never worked with PHP before or made a DB before so it's possible I've made very obvious mistakes.
php_script
<?PHP
$db_host = "stevie.heliohost.org";
$db_username = "secret";
$db_pass = "secret";
$db_name = "secret";
$connection = #mysql_connect ("$db_host","$db_username", "$db_pass")
or die ("could not connect to mySQL");
#mysql_select_db("$db_name") or die ("No database");
$sql="INSERT INTO account(username, password)
VALUES('$_POST[user]','$_POST[password]')";
if (!mysql_query($sql,$connection ))
{die('Error: ' . mysql_error());}
echo "1 record added";
mysql_close($con);
?>
HTML FORM
<form name="LOGIN" action="php_script.php" method="post">
Username: <input id="username" type="text" name="user">
password: <input id="password" type="text" name="password">
<input id="submit" type="submit" value="Submit">
</form>
try this out
$db_host = 'stevie.heliohost.org';
$db_username = "secret";
$db_pass = "secret";
$db_name = "secret";
$connection = mysql_connect($db_host,$db_username, $db_pass)
or die ("could not connect to mySQL");
mysql_select_db($db_name) or die ("No database");
$sql="INSERT INTO account(username, password)
VALUES('".$_POST[user]."','".$_POST[password]."')";
if (!mysql_query($sql,$connection ))
{die('Error: ' . mysql_error());}
echo "1 record added";
mysql_close($con);
OBS: u should use mysqli or PDO , your code is easy for sql injections
If you are teaching yourself PHP, the First thing you should do is learn PDO. Because, the way you are using mysql, you are leaving your site open for SQL-Injection type of hacking, which anyone can do. Since you are directly submitting the value of $_POST['']; in your database. Start learning PDO today, it is much more secure. You can find the a good tutorial here: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
Additionally, Check this code as I have modified it a little bit. and host name, is almost always localhost
<?PHP
$db_host = "localhost";
$db_username = "secret";
$db_pass = "secret";
$db_name = "secret";
$connection = mysql_connect ("$db_host","$db_username", "$db_pass")
or die ("could not connect to mySQL");
mysql_select_db("$db_name") or die ("No database");
$sql="INSERT INTO account(username, password)
VALUES('$_POST[user]','$_POST[password]')";
if (!mysql_query($sql,$connection ))
{die('Error: ' . mysql_error());}
echo "1 record added";
mysql_close($con);
?>
First of all you shouldn't be using mysql_ functions because they are being deprecated. Try to use PDO or likes.
second try this
$db_host = 'stevie.heliohost.org';
$db_username = "secret";
$db_pass = "secret";
$db_name = "secret";
$connection = mysql_connect($db_host,$db_username, $db_pass)
or die ("could not connect to mySQL");
mysql_select_db($db_name) or die ("No database");
$sql="INSERT INTO account(username, password)
VALUES('".mysql_escape_string($_POST[user])."','".mysql_escape_string($_POST[password])."')";
if (!mysql_query($sql,$connection ))
{die('Error: ' . mysql_error());}
echo "1 record added";
mysql_close($con);
you should be escaping your values in your query too so the its not up for SQL injection
I am not up for for using mysql_escape_string either you should be using atleast mysqli
So go through a good tutorial to learn more about it

Connecting and inserting data into mysql table using php

I am a PHP newbie and have been trying for sometime now to connect to MySQL database using PHP so I can insert data into a table I have created but I am unable to do this.
I suspect the problem is coming from my PHP .ini file,but that's just me.
Would be grateful if anyone can help me configure my PHP .ini file so I can connect to MySQL and insert data into my table. Here is my PHP script in case you are wondering.
Any help will be gratefully appreciated.
<?php
$host ="localhost";
$username = "username";
$password = "password";
$database = "database1";
$table ="users";
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect:'.mysql_error());
}
mysql_select_db("database1",$con);
$mysql = "INSERT INTO $table(name,email,password)
VALUES('$_POST[name]','$_POST[email]','$_POST[password]";
if(mysql_query($mysql)) die(mysql_error());
echo"Data inserted";
mysql_close();
?>
I revised some of your code this should work. You had a bunch of little errors. I suggest you read a couple tutorials on just connecting and the syntax of php.
Here is some really basic examples of connecting to a database:
http://www.w3schools.com/php/php_mysql_connect.asp
Also once you get the hang of it here is a really good tutorial to teach you the OOP way of creating a class for a database:
http://net.tutsplus.com/tutorials/php/real-world-oop-with-php-and-mysql/
As far as I see this is not an ini issue. I hope this helps.
<?php
//Set your variables
$host = "127.0.0.1";
$username = "username";
$password = "password";
$database = "database1";
$table = "users";
//Make your connection to database
$con = mysql_connect($host,$username,$password);
//Check your connection
if (!$con) {
die("Could not connect: " . mysql_error());
}
//Select your database
$db_selected = mysql_select_db($database, $con);
//Check to make sure the database is there
if (!$db_selected) {
die ('Can\'t use the db : ' . mysql_error());
}
//Run query
$result = mysql_query("INSERT INTO $table(name,email,password) VALUES('$_POST[name]','$_POST[email]','$_POST[password]'");
//Check Query
if (!$result) {
die("lid query: " . mysql_error());
}
echo "Data inserted";
mysql_close($con);
?>
First, why do you have <br/> in your PHP statements? Remove all those.
Also, you have to use PDO or mysqli_ instead of the mysql_ library, mysql_ is deprecated.

mysql_connect (not working with php file)

Keep getting same error message
(Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/00/7882800/html/Connections/dueslogin.php on line 27
Unable to connect to database! Please try again later.)
<?php
//Variables for connecting to your database.
//These variable values come from your hosting account.
$hostname = "TarHeelsDues.db.7882800.hostedresource.com";
$username = "TarHeelsDues";
$dbname = "TarHeelsDues";
//These variable values need to be changed by you before deploying
$password = "************";
$usertable = "DuesLogin";
$yourfield = "your_field";
//Connecting to your database
mysql_connect($hostname, $dbname, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($TarHeelsDues);
//Fetching from your database table.
$query = "SELECT * FROM $usertable";
$result = mysql_query($query);
if ($result) {
while($row = mysql_fetch_array($result)) {
$name = $row["$yourfield"];
echo "Name: $name<br>";
}
}
?>
change the mysql_connect line to
mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname);
A little mistake on there, you have to use $username not $dbname in mysql_connect.
mysql_connect($hostname, $username , $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($TarHeelsDues);
The correct syntax should be like this:-
mysql_connect($hostname, $username , $password) OR die ("Unable to connect with the server");
mysql_select_db($TarHeelsDues) or die('Cannot connect with the database!');

Categories