Problem not running query in MySQL database remotely - php

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

Related

Have php7 working however when I try to make a mysqli object, "Class 'mysqli' is not found" [duplicate]

This question already has answers here:
PHP 7 cannot find MySQLi
(2 answers)
Closed 3 years ago.
I have php7 installed and working
I'm using the Built-in HTTP server
Uncommented "extension_dir="C:\php\ext" and extension="php_mysqli.dll" in the php.ini files development and production (did production too just to be safe)
When I used the phpinfo() method, I found that it says "Loaded Configuration file: none"
I've installed php7 therefore mysqli is supposed to already be installed. Looking online I found that you have to edit the php.ini file (mentioned how I did so in bullet points above) however when I run:
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
I get the error:
Fatal error: Uncaught Error: Class 'mysqli' not found in C:\Users\Calvin\try\learningMySQL.php:17 Stack trace: #0 {main} thrown in C:\Users\Calvin\try\learningMySQL.php on line 17
Thank you, any help is appreciated.
$servername = "localhost";
$username = "username";
$password = "password";
$database = "database";
$con = mysqli_connect("$servername","$username","$password","$database");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

Connecting to AWS RDS database using beanstalk and PHP

In PHP 7, As I try to connect to the Beanstalk I have set up, I receive the error:
Fatal error: Uncaught Error: Class 'mysql' not found in C:\Apache24\htdocs\php_file.php, Stack trace: #0 {main} thrown in C:\Apache24\htdocs\php_file.php on line 41.
This server uses AWS RDS running MYSQL, and I am using apache 2.4 to localhost (for testing).
The code I'm using is:
$servername = "MY BEANSTALK CONNECTION";
$username = "Username";
$password = "PSSWD";
$dbname = "DBNAME";
$conn = new mysql($servername, $username, $password, $dbname);
$sql = "SELECT * FROM column";
$result = $conn->query($sql);
$conn->close();
My updated code uses mysqli, but is still getting the same error.
From PHP documentation mysql class is deprecated since version 5.5 and removed in version 7. that's why you're receiving the class not found error.
Try using mysqli. The PHP documentation has good examples on this. Look at example 2. I've pasted part of the example in this post for you.
https://www.php.net/manual/en/function.mysql-connect.php
<?php
$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
}
$res = $mysqli->query("SELECT 'choices to please everybody.' AS _msg FROM DUAL");
$row = $res->fetch_assoc();
echo $row['_msg'];
?>
Use mysql_connect($servername, $username, $password) instead.

failed connection to database in php admin [duplicate]

This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Is it possible to use both MySQLi and PDO?
(2 answers)
Closed 5 years ago.
here is my code which I pass two variables from android studio to php. xampp has both apache and MySQL running on the default ports. my aim is to compare variables got from android studio with ones in the database table; but i get an error that says:
Uncaught Error: Call to a member function bindParam() on boolean in C:\xampp\htdocs\digikala\index.php:10 Stack trace: #0 {main} thrown in C:\xampp\htdocs\digikala\index.php on line 10.
I should also mention windows 10 pro is running on my system.
could any of you guys could help me get over this problem? thanks in advance.
here is my connect.php code:
<?php
function OpenCon()
{
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$db = "digikala";
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error);
return $conn;
}
function CloseCon($conn)
{
$conn -> close();
}
?>
and this is my main code which error belong to. if I remove bindParam and put $email and $pass directly in sql code the same error happens for line contains $result->execute():
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include "connect.php";
$email=$_POST["user"];
$pass =$_POST["pass"];
$conn = OpenCon();
$query = 'SELECT * FROM member WHERE email=:email AND pass=:pass';
$result= $conn->prepare($query);
$result->bindParam(':email',$email);
$result->bindParam(':pass',$pass);
$result->execute();
$row = $result->fetch(PDO::FETCH_ASSOC);
if ($row == false ){
echo("not existed!");
}else{
echo("welcome" . $email);
}
CloseCon($conn);
?>

MySQL PHP adding 3306 without my input

I have a back end cron script that checks MySQl regurlarly for an if-then condition and then runs a Curl action. Anyway, the script has been running fine for over a year now when suddenly a problem occured and it does not make sense to me
here is the truncated code for the script
<?php
//assign values to variables
$servername = "localhost:3306";
$username = "username";
$password = "password";
$database = "database";
$con = mysqli_connect($servername, $username, $password, $database);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$results = "SELECT NewUserID FROM queue ORDER BY ID";
$queryresults = $con->query($results);
if($con->error) {
printf("Errormessage: %s\n", $con->error);
die();
}
I would see this error:
Warning: mysqli_connect(): (HY000/2002): Failed to parse address "localhost:3306:3306" in /home/foo/bar/BackEnd.php on line 11
Failed to connect to MySQL: Failed to parse address "localhost:3306:3306"
Fatal error: Uncaught Error: Call to a member function query() on boolean in /home/foo/bar/BackEnd.php:19
Stack trace:
#0 {main}
thrown in /home/foo/bar/BackEnd.php on line 19
The solution was simple, I change "localhost:3306" to "localhost" and everything worked again, since it seemed that ":3306" was being added to what was already there. This solution seems like a problem waiting to happen but im not sure what went wrong in the first place. Any ideas?
You can't specify the port in the host parameter.
Here is the full function definition:
mysqli_connect($host, $username, $password, $dbname, $port, $socket);
You should just add the port at the end of your call. Like this:
$con = mysqli_connect($servername, $username, $password, $database, 3306);

MySQL won't load on localhost

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.

Categories