MySQL won't load on localhost - php

I can't get mysql table to load on localhost. below i used PDO and the table worked fine. no errors...the table works fine in phpmyAdmin...please help driving me nuts. i imported table from my web server to local host from a earlier version..
<?php
$user = "root";
$pass = "root";
try {
$dbh = new PDO('mysql:host=localhost;dbname=iosLeads',$user,$pass);
foreach($dbh->query('SELECT * from Customer') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error! :" . $e->getMessage()."<br/>";
die();
}
?>
but when i used this code it show a blank screen, no errors..i tried same code with other tables and it works fine.
<?php
// set up the connection variables
$db_name = 'iosLeads';
$hostname = 'localhost';
$username = 'root';
$password = 'root';
// connect to the database
$dbh = new PDO("mysql:host=$hostname;dbname=$db_name", $username, $password);
// a query get all the records from the users table
$sql = 'SELECT * FROM Customer';
// use prepared statements, even if not strictly required is good practice
$stmt = $dbh->prepare( $sql );
// execute the query
$stmt->execute();
// fetch the results into an array
$result = $stmt->fetchAll( PDO::FETCH_ASSOC );
// convert to json
$json = json_encode( $result );
// echo the json string
echo $json;
?>
I'm getting this in my php error.log
[17-Dec-2014 03:39:55 Europe/Berlin] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost:8888' (20)' in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10
Stack trace:
0 /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php(10): PDO->__construct('mysql:host=loca...', 'root', 'root')
1 {main}
thrown in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php on line 10
[17-Dec-2014 03:40:00 Europe/Berlin] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost:8888' (20)' in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10
Stack trace:
0 /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php(10): PDO->__construct('mysql:host=loca...', 'root', 'root')
1 {main}
thrown in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php on line 10
[17-Dec-2014 03:40:23 Europe/Berlin] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost:8888' (20)' in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10
Stack trace:
0 /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php(10): PDO->__construct('mysql:host=loca...', 'root', 'root')
1 {main}
thrown in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php on line 10

If the array in $result has strings other than UTF-8 encoded, the function json_encode() may fail and will return false (see PHP manual json_encode). To check this, dump the value of $result before using json_encode():
var_dump($result);
If you have an array in $result but no other return value than false from json_encode(), you must set the correct character encoding for the database connection.

Related

Ubuntu22.04 can't connect SQLServer. "SSL Provider: [error:0A000086:SSL routines::certificate verify failed:unable to get local issuer certificate]"

I want to run this on Ubuntu22.04(AzureVM).
<?php
$dsn = 'sqlsrv:server=hogehoge';
$user = 'user';
$password = 'password';
$dbh = new PDO($dsn, $user, $password);
$sql = "select * from user";
foreach ($dbh->query($sql) as $row) {
echo $row['id'] . ' ' . $row['name'] . "<br>";
}
$dbh = null;
?>
But I got this error message.
PHP Fatal error: Uncaught PDOException: SQLSTATE[08001]: [Microsoft][ODBC Driver 18 for SQL Server]SSL Provider: [error:0A000086:SSL routines::certificate verify failed:unable to get local issuer certificate] in test.php:6
Stack trace:
#0 /var/www/test/test.php(6): PDO->__construct()
#1 {main}
thrown in test.php on line 6
What's happened? I searched for that error message but can't understand how to fix.
My local Windows does have not this problem. Maybe Ubuntu server is bad.
Thank you for your viewing.

getting an error to connecting mysql db in php

I am working on a new project and I am getting an error when I connect with my DB I don't know why it is happening my DB name and user name is all correct but why I am getting this error
Fatal error: Uncaught Error: Call to undefined function mysql_query() in C:\xampp\htdocs\treeview\treeview.php:9 Stack trace: #0 {main} thrown in C:\xampp\htdocs\treeview\treeview.php on line 9
Code:
<?php
mysql_connect('localhost', 'root');
mysql_select_db('test');
$qry = "SELECT * FROM treeview_items";
$result = mysql_query($qry);
$arrayCategories = array();
while ($row = mysql_fetch_assoc($result)) {
$arrayCategories[$row['id']] = array("parent_id" => $row['parent_id'], "name" => $row['name']);
}
I'll assume you are on PHP 7+, which means that mysql_query is deprecated. Please use PDO or mysqli.
More info on that function's man page: https://www.php.net/manual/en/function.mysql-query.php
You can obtain the connection to your database through:
$connection = new mysqli($hostname, $db_username, $password, $db_name);
if ($connection->connect_error){
$error = "Failed to connect to db: " . $connection->connect_error;
// Do whatever you want with $error
}
$connection->query("SET NAMES utf8mb4"); // mostly, people use utf8 instead

Problem not running query in MySQL database remotely

I have a virtual server and Xamp software is installed on it I have built in a MySQL service that can be easily controlled using database management software, but when I put the same user in PHP code and another server to remotely use the output And we will encounter the following errors
The above code is executed correctly at localhosts but does not run remotely
Problem not running query in MySQL database remotely
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$servername = "MyServer";
$username = "User";
$password = "MyPassword";
$dbname = "DBName";
$mysqli = new mysqli($servername, $username, $password,$dbname);
$result = $mysqli->query("SELECT * FROM users");
$row = $result->fetch_array();
echo $row['name'];
Eror :
Warning: mysqli::query(): Couldn't fetch mysqli in
/home2/irpixeli/domains/irpixel.ir/public_html/dbsync.php on line 15
Fatal error: Uncaught Error: Call to a member function fetch_array()
on bool in
/home2/irpixeli/domains/irpixel.ir/public_html/dbsync.php:16 Stack
trace: #0 {main} thrown in
/home2/irpixeli/domains/irpixel.ir/public_html/dbsync.php on line 16

Driver not found - PDO - AMPPS

I was using MAMP for a few months now, but today I installed AMPPS.
I am going to work with databases, but it doesn't work.
I get the following error:
Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in /Applications/AMPPS/www/functions.php:13 Stack trace: #0 /Applications/AMPPS/www/functions.php(13): PDO->__construct('mysql:host=loca...', 'root', 'usbw') #1 /Applications/AMPPS/www/functions.php(22): dbConn() #2 /Applications/AMPPS/www/test.php(18): imageDropdown('s') #3 {main} thrown in /Applications/AMPPS/www/functions.php on line 13
Code:
// database connection
function dbConn() {
$username = "root";
$password = "usbw";
$dbh = new PDO(
'mysql:host=localhost; port=3306; dbname=webshop',
$username,
$password
);
return $dbh;
}
// dropdown met afbeeldingen
function imageDropdown($zoek) {
$dbh = dbConn();
$sth = $dbh -> prepare("
SELECT afbeelding
FROM Afbeelding
WHERE afbeelding LIKE '%:zoek%'
ORDER BY afbeelding
");
$sth -> bindValue(":zoek", $zoek, PDO::PARAM_STR);
$sth -> execute();
$result = $sth -> fetchAll(PDO::FETCH_ASSOC);
print("<pre>");
print_r($result);
print("</pre>");
}
?>
(functions.php)
<?php
include "functions.php";
imageDropdown("s");
?>
(test.php)
There is a driver missing, but how do I install it?
I'm on Mac.
In user interface - PHP tab - php extension. Check if required extensions are enabled. Like pdo_mysql and pdo

PDOException not being caught?

I'm getting the following error in PHP:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'localhost' (10061)' in C:\xampp\htdocs\project\Service\Database.class.php:26 Stack trace: #0 C:\xampp\htdocs\project\Service\Database.class.php(26): PDO->__construct('mysql:host=loca...', 'root', '', Array) #1 C:\xampp\htdocs\project\Service\Database.class.php(54): Service\Database::initialize() #2 C:\xampp\htdocs\project\index.php(15): Service\Database::getHandler() #3 {main} thrown in C:\xampp\htdocs\project\Service\Database.class.php on line 26
The error itself is not the problem, I intentionally terminated the MySQL service in Windows to see what happened (I'm using XAMPP). The problem is that I'm unable to catch the exception that the PDO object throws and I don't know why.
try {
$host = "localhost";
$dbname = "project";
$userName = "root";
$password = "";
$charset = "utf8";
$dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";
$driverOptions = array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES $charset"
);
// This is the line that supposedly throws the exception (LINE 26):
$dbh = new PDO($dsn, $userName, $password, $driverOptions);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::setHandler($dbh);
} catch (PDOException $e) {
die("CATCHED"); // This line is never reached
} catch (Exception $e) {
die("CATCHED"); // nor this one.
}
What am I missing here?
The only thing I can think of is if you're inside a namespaced class, and should use \PDOException instead of PDOException.
Turn on error_reporting and check the errors.
ini_set('display_errors', true);
error_reporting(E_ALL);
May be there is a fatal error, before that line, or maybe PDO is not available.

Categories