failed connection to database in php admin [duplicate] - php

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);
?>

Related

MySQL database connect: There is an error while processing this request [duplicate]

This question already has an answer here:
PHP's white screen of death [duplicate]
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 3 years ago.
I am breaking my head why I cannot make a succesful database connection. When I am using the below code I get a blank page with this error: 500: There is an error while processing this request. The dbname, username, code is 100% correct. If i just set in:<?php echo "Hello World";?> the page is working fine and echo out Hello World. So I guess it has to be something to do with my database connection code?
Can anybody see what I am doing wrong here?
index.php:
<?php
include 'db_connection.php';
$conn = OpenCon();
echo "Connected Successfully";
CloseCon($conn);
?>
db_connection.php:
<?php
function OpenCon()
{
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "mycode";
$db = "store";
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error);
return $conn;
}
function CloseCon($conn)
{
$conn -> close();
}
?>
Image of the MySQL files:
I am running the database in a Synology DS918 server. As I recall the log is under /var/log/mysql/. But I cannot find that path here. Does anybody knows where the log should lay?

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();
}

Can not fetch data from database, when the connect is right [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I have this code:
include "config.php";
if (isset($connect)) {
echo "connected";
} else {
echo "not connected";
}
And this is the config.php file:
$localhost = "localhost";
$db_username = "*****";
$db_password = "*****";
$db_name = "*****";
If($connect = mysqli_connect("$localhost ","$db_username ","$db_password ","$db_name ")){}or die{}
I dont know why, but when I try to do some queryIi dont get anything
$sql="SELECT * FROM table_name ORDER BY column_name ASC";
$query=($connect,$sql);
While($row = mysqli_fetch_object($query)) {
Echo $row>column;
}
the error is:
Parse error: syntax error, unexpected '}' in /path/index.php on line 6
i see a more than one problem in your codes,
First from config.php
If($connect = mysqli_connect("$localhost ","$db_username ","$db_password ","$db_name ")){}or die{}
This is not right code, error in {}or die{} it must be {} else {die();}
But i recommended you to use this one instead,
$connect = mysqli_connect("$localhost", "$db_username", "$db_password", "$db_name") or die(mysqli_connect_error());
And about the third code have some problems, first
$query=($connect,$sql);
You forgot to use mysqli_query
$query=mysqli_query($connect, $sql);
And in printing the data you must use -> not >
Echo $row->column;
I hope this helps.

PHP and MYSQLI Error, Call to a member function query() on a non-object [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
I'm getting the Call to a member function query() on a non-object when I try to call my function.
My code looks like this:
function add_profile() {
$hostname = "localhost";
$dbusername = "username";
$dbname = "dbname";
$dbpassword = "password";
$link = mysqli_connect($hostname, $dbusername, $dbpassword, $dbname);
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
$sql = "INSERT INTO payment_profiles(id, client_id) VALUES ( '','$profile_id')";
$result = $mysqli->query($sql);
if (!result)
{
echo 'Error: ', $mysqli->error;
}
}
add_profile();
It says my error is on the line: $result = $mysqli->query($sql);
I'm assuming I'm not calling something properly. Thanks in advance for any help
In your code you're mixing both procedural and object-oriented code. Choose either one or the other. Here's how you would solve the problem the procedural way.
$result = mysqli_query($link, $sql, MYSQLI_USE_RESULT)
I'm getting the Call to a member function query() on a non-object when
I try to call my function.
That's because the $mysqli object is not declared anywhere (or is it)? Before you can use $mysqli you should first create an instance of mysqli and assign it to your object.
$mysqli = new mysqli("localhost", "my_user", "my_password", "database");
Only then you may call the methods of the mysqli class like $mysqli->query();
The error you made depends probably on two misconceptions:
1) you pasted half of your code from the procedural-style part of the mysqli manual and half from the oop part
2) you assume $mysqli is a global object instantiated with $mysqli_connect();. It is not. You should invoke the constructor with the new keyword if you'd like to use it as an object.
$link = mysqli_connect($hostname, $dbusername, $dbpassword, $dbname);
/////
$result = $mysqli->query($sql);
///////

PHP Notice: Undefined variable: conn in [duplicate]

This question already has answers here:
Giving my function access to outside variable
(6 answers)
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 4 years ago.
Hello I am pretty sure they are in the same scope and am unsure why I am getting this error now as it was working before.
These are the other files in the same folder as
the php file :
dbh.php
recipeLookUp.php
It is line 9 that I am getting the error
this is the recipeLookUp.php
<?php
function lookup($sql,$column_name){
$results_search=array();
include_once 'dbh.php';
$results=mysqli_query($conn,$sql);
$resultsCheck = mysqli_num_rows($results);
if ($resultsCheck > 0) {
while ($row = mysqli_fetch_assoc($results)){
$info= $row[$column_name];
$results_search[]=$info;
}
return $results_search;
}
}
and here is my other file
<?php
$dbServername = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'Recipe';
$conn = mysqli_connect($dbServername,$dbUsername,$dbPassword,$dbName);
Im sure it probaly something small but I can't find it please help!!!
The include_once 'dbh.php'; within your function seems fishy. What are you gonna do if you have other functions which need to perform SQL queries as well?
Instead, I would put the include_once 'dbh.php'; outside (above) your function, and perhaps use require_once instead of include once. And then inside your function, put global $conn; above the mysqli_query call, so your $conn variable is defined there when you pass it along to mysqli_query.
However, if you are worried about creating unnecessary SQL connections (e.g. in case your lookup function is never actually called) I think it would be even better to put this in your dbh.php :
<?php
$conn = null;
function Query($sql)
{
global $conn;
if (!$conn) // will only connect if connection does not exist yet
{
$dbServername = 'localhost';
$dbUsername = 'root';
$dbPassword = 'xxxxxxxxxxx';
$dbName = 'Recipe';
$conn = mysqli_connect($dbServername,$dbUsername,$dbPassword,$dbName);
}
return mysqli_query($conn,$sql);
}
?>
And now in your main PHP file, or any PHP file where you include require_once 'dbh.php'; you can directly use Query($sql) to perform queries, which will take care of the connection itself (and only do so if necessary).

Categories