Cannot connect to database using local server, getting a blank white screen - mysql

So I just started building a basic site in preparation for my internship in the coming weeks, and I installed MAMP and everything was working fine. Set up my database and began designing the site, then for some reason I just got a blank white screen.
I tried refreshing, restarting the browser and restarting MAMP and continue to get the same blank page. At one point I got a message saying it could not connect to the database but I can't reproduce it at the moment. When I did localhost:8889 to access the MySQL port I received a download page with the following text:
J
5.5.34TW8Qp]bVˇ˜Äe|]<.Utj7oNt}mysql_native_password!ˇÑ#08S01Got packets out of order
(added a period after the <. because it wouldn't show the rest)
I am new to this so I am not really sure what to do from here. Please explain as if you are talking to someone with no knowledge on the subject, I took a couple of courses last semester but never got into hands-on stuff like this.

Try with this code.
<?php
$con = mysql_connect('database host', 'user name', 'password'); //Database host may be:127.0.0.1:3306
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('database name', $con);
$sql="set names 'utf8'";// for unicode support, must use after mysql_select_db() function.
mysql_query($sql);
mysql_query('SET CHARACTER SET utf8')or die('charset problem:'.mysql_error());
mysql_query("SET SESSION collation_connection ='utf8_general_ci'") or die('collation problem:'.mysql_error());
?>
Thanks.

Related

Data INSERTS into a particular table on local test server but WILL NOT INSERT on the live site

As the title says, works perfect on my local WAMP test server. Same PHP version installed however mysql on local test server is 5.6.12 and the live mysql version is 5.6.39. All other inserts and tables on the live site work except one I had an issue with until I signed into the site under a different user and it worked under their profile. Not having such luck with this last one. Waited a day a it's not inserting into the table under any profile. Also I confirmed its pulling the data properly by using echo statements at the end of the script. So its got the data but refuses to insert into the database table on the live site. Any help or ideas is greatly appreciated.
Here's the code:
$db->Query("INSERT INTO `ysub` (user, url, title, y_av, max_clicks, daily_clicks, cpc, country, sex) VALUES('".$data['id']."', '".$yt_url."', '".$url."', '".$yt_image."', '".$max_clicks."', '".$daily_clicks."', '".$cpc."', '".$country."', '".$gender."') ");
Try adding the error function after the query line to see if an error is being caught.
$db->query('INSERT INTO...');
echo $db->errorInfo();

Couldn't access to db with Zencart

Trying to copy a zencart website in another server, but after openning the right port and copying the database, i have this error while openning the url in an internet browser.
0 DB_ERROR_NOT_CONNECTED
in:
[select * from project_version WHERE project_version_key = 'Zen-Cart Database' ]
If you were entering information, press the BACK button in your browser and re-check the information you had entered to be sure you left no blank fields.
Does'nt anyone already encounter this error ? Or have suggestion of how to solv this ?
Tx
Something in includes/configure.php is wrong. Check each of these values very carefully:
define('DB_PREFIX', '');
define('DB_SERVER', 'localhost');
define('DB_SERVER_USERNAME', '');
define('DB_SERVER_PASSWORD', '');
define('DB_DATABASE', '');
You likely have a typo in one of them, or perhaps you forgot to specify the prefix.
For anyone which encounter the same problem.
It comes from the fact that the password were saved in an old format on mysql.
You should migrate them to the new format to solv this issue:
update user set password=password('<my_password>') where user = '<my_user>';
flush privileges;

MYSQLI not playing nice with INSERT

So excuse the coding if it's not totally up to par, but I just got a hang of MYSQL and am switching to MYSQLI (procedural as I find it easier as I haven't learned object oriented yet, i'm new to this)
My problem is I have a simple 'Invoice' page on a small website that worked perfectly with MYSQL. When I switched to MYSQLI (I'm sure I am missing something obvious) I can echo all the variables, have checked for proper DB connection yet it will not INSERT anything into the DB.
This is the function in question, it is called after a "fill invoice" form is submitted, again, all the $_POST variables can be echoed in the function properly.
function createinvoice($billto,$cust,$addr,$desc,$desccost,$labcost,$comment){
$taxrate=".13";
$kaboom=str_replace(array("\r\n","\r","\n"),"<br />",$desccost); //keep original desccost for database
$kaboom=mysqli_real_escape_string(nl2br($kaboom));
$kaboom=nl2br($kaboom);
$lines=explode("<br />",$kaboom);
$matcost=0;
for($i=0;$i < count($lines);$i++)
{
$matcost=$matcost+$lines[$i];
}
$fullcost=$matcost+$labcost;
$taxdue = $fullcost*$taxrate;
$totaldue=$fullcost+$taxdue;
$desc=mysqli_real_escape_string($desc);
mysqli_query($con,"INSERT INTO invoice(customerID,customerName,customerAddr,description,desccost,material,labour,taxdue,totaldue,comment)
VALUES('$billto','$cust','$addr','$desc','$desccost','$matcost','$labcost','$taxdue','$totaldue','$comment')");
mysqli_free_result($result);
mysqli_close($con);
}
Edit: to show connection string
if(include("/../dbinvoice.php")){echo "DB file connected";}
$con = mysqli_connect($dbhostname, $dbuser, $dbpass, $dbname);
if (mysqli_connect_errno($con)){echo "No MYSQL connection: ".mysqli_connect_error();exit();}
Every time the page loads I get a message "DB file connected" showing it read the dbinvoice.php file and I don't get an error message with connect. I have verified with phpinfo() that mysqli is on my machine (webmatrix testing).

How to make Mysql database to support Arabic Language?

I am trying to save Arabic language in mysql database but it doesnot save in Arabic format.
It shows question marks instead of Arabic. How to make it store values in Arabic.
I tried many queries seeing from internet but it doesnot changes. How to change it for Arabic.
"ar_SA: Arabic - Saudi Arabia"
Please suggest a way?
i use WAMP Server. (windows,apache,mysql,php).
//so important
FIRST :
in phpmyadmin or MySQL :
make sure that Mysql Database is utf.
make sure that the your database and it's tables are utf-general-ci
after connecting to Mysql immidiately (before choosing your DB) make this order.
mysql_set_charset('utf8');
example :
<?php
//connect to MySQL
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_set_charset('utf8'); // that's the order.
echo "Connected to MySQL<br />";
//connect to your DB
mysql_select_db("mydb") or die(mysql_error());
echo "Connected to Database";
?>
SECOND :
in the meta data in the php file make the meta data as following :
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Third :
Make sure that the php file it self is utf-8 enabled . u can make sure in your IDE settings , or if u work on notepad++ or Komodo Edit then u can find it in the status bar at the bottom of the window , right side.
// i tried this but it didn't have effect.
in the header of the php file (before every thing).
<?php header("Content-type: text/html; charset=utf-8"); ?>
in the form submitted:
<form accept-charset="utf-8" ...>
Just use UTF-8 in the page's encoding, in the database connection, and the database itself.
Make sure your database encoding and collation is utf8_general_ci

MySQL connection reuse problem

I am trying to reuse mysql connection. Hence I have a global variable in databasemanager.php class that returns a connection.
The problem is somehow on one particular page mysql is executing prior query as well.
Looks like there is some leftover query in connection object that gets executed if same connection is being reused . Is it possible ? how to solve this ..
function getDBConnection(){
global $conn;
if (!empty($conn)){
// echo $conn ;
return $conn;
}
$conn = mysql_connect($GLOBALS['HOSTNAME'],$GLOBALS['DBUSER'],$GLOBALS['DBPASS']);
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($GLOBALS['DBNAME'],$conn);
return $conn;
}
This how code lookslike
Have you looked into mysql_pconnect?
According to the documentation:
mysql_pconnect() acts very much like
mysql_connect() with two major
differences.
First, when connecting, the function
would first try to find a (persistent)
link that's already open with the same
host, username and password. If one is
found, an identifier for it will be
returned instead of opening a new
connection.
Second, the connection to the SQL
server will not be closed when the
execution of the script ends. Instead,
the link will remain open for future
use (mysql_close() will not close
links established by
mysql_pconnect()).