I get this error when I want to connect my database - php

I am doing a course on the internet and everything was going well until I had to connect a database. It has not worked for me and I have looked for many solutions but I have 2 days and I do not get anything
Here the database code
<?php
function conectar_bd()
{
$servidor = "127.0.0.1";
$usuario = "jhon28";
$contraseƱa = "Elmenor28519";
$nombrebd = "empresa";
$conexion = mysqli_connect("127.0.0.1", "jhon28", "Elmenor28519");
mysqli_select_db($conexion, $nombrebd);
return $conexion;
}
?>
Here the connection code
<?php
include("basededatos.php");
$conexionbd=conectar_bd();
echo $conexionbd;
mysqli_close ($conexionbd);
?>
Here the error that come to me
Recoverable fatal error: Object of class mysqli could not be converted to string in C:\xampp\htdocs\prueba.php on line 4

Remove echo $conexionbd; or change it to print_r($conexionbd);

<?php
include("basededatos.php");
$conexionbd=conectar_bd();
print_r($conexionbd); //here you are getting object so you can't use echo use print_r instead
mysqli_close ($conexionbd);
?>

based on the docs you wont need select_db, you may insert it on the same function like below.
$link = mysqli_connect($servidor, $usuario, $contraseƱa, $nombrebd);
Therefore, you save one line. Just helping to optimize your code. Refer docs for more information.

Related

Mongo db connectivity Error?

<?php
$username='root';
$password='xyz';
$database='abc';
$host='localhost';
function MongoConnect($username, $password, $database, $host) {
$con = new Mongo("mongodb://{$username}:{$password}#{$host}"); // Connect to Mongo Server
$db = $con->selectDB($database); // Connect to Database
}
$collection=$db->movie;
$document = array( "name" =>"Calvin and Hobbes");
$collection->insert($document);
// find everything in the collection
$cursor = $collection->find();
// iterate through the results
foreach ($cursor as $document) {
echo $document["name"] . "\n";
}
?>
I had installed MONGO DB and tried to test my DB, but I am getting an ERROR
"Internal Server Error 500"
And also my Test.php file have my own content called Hello World, but if I had run the TEST.php file it displays Nothing.
My DB table is not accessing and I wasn't able to retrieve data from my Database.
So Kindly help me out here.
There can be several things wrong.
First - is Mongo driver installed?
Second - your MongoConnect function have no effect. You are defining it and not calling. Plus even if you would call it there would be no effect as $db is only in function scope and not outside.
Third - because function MongoConnect have no effect "$collection=$db->movie;" will result in problem as $db is not defined.
Consult http://php.net/manual/en/mongocollection.insert.php on how to insert data in collection.
Internal Server Error only occured when misspelled in code or some of function called wrongly. Please review ur code.

Warning: mysqli_query(): Couldn't fetch mysqli in C:\ ... on line 13

although this question has been asked (and answered) many times, I didn't find a solution to the problem.
Here is my code:
<?php
#session_start();
include("./include/config.php");
include("./include/db_connect.php");
include("functions.php");
if (!isset($_GET['artikelID'])){$_GET['artikelID'] = "";}
if (!isset($_SESSION['UserID'])){$_SESSION['UserID'] = "";}
$sql = "SELECT kundenID FROM kunden WHERE username = '".$_POST['myusername']."' AND password = '".md5($_POST['mypassword'])."' ";
$result = mysqli_query($connect, $sql) OR die("<pre>\n".$sql."</pre>\n".mysqli_connect_error()); // this is line 13
$row = mysqli_fetch_assoc($result);
if (mysqli_num_rows($result)==1){
doLogin($row['kundenID'], isset($_POST['Autologin']));
header("location:cart.php?action=add&artikelID=".$_GET['artikelID']."&id=". $_SESSION['UserID'] ." ");
}
else {
header("location:k_login.php?error=TRUE ");
}
include("./include/db_close.php");
?>
mysqli_connect_error() shows me the absolute correct sql-query; the sql-query is tested with a tool named mysql-front and brings exactly one (and the correct one) result, which is 'kundenID'.
I have tested many things (like $_SESSION['connect'] or $_GLOBALS['connect'] instead of $connect in db_connect.db), but with no result.
Can anyone please help me?
-- Update --
Why does nobody answer?
Is the description of the problem unclear?
The db-connection is established like this:
<?php
error_reporting(E_ALL);
$connect = mysqli_connect($dbserver,$dbuser,$dbpass,$dbname);
// Check connection
if (mysqli_connect_errno()){
echo "Zeile ".__LINE__.": Datenbankverbindung ist fehlgeschlagen ! " . mysqli_connect_error();
exit();
}
?>
All the db-variables are known in the checklogin-script (tested). All the $_POST-variables are also known in the checklogin-script (tested). I even tried a hard-coded sql-query (with the real data of the test-record in the db).
The result is still the same: mysqli_connect_error() reports the correct query - but then nothing more happens.
I have spent more than 10 hours in the meantime. I really would appreciate, if someone could help me.
Couldn't fetch mysqli means that PHP is unable to identify the contents of your $connect variable as a valid mysqli connection. Try adding some error handling into "./include/db_connect.php" to get an idea of what happened to the mysqli connection that is preventing you from using it.

PHP Fatal error: Call to undefined method mysqli::mysqli_fetch_all()

hoping someone can help me, I am having the following error, looked online and tried a load of things but can't seem to figure it out, error:
Fatal error: Call to undefined method mysqli::mysqli_fetch_all() in C:\xampp\htdocs\cyberglide\core-class.php on line 38
heres my code:
<?php
class Core {
function db_connect() {
global $db_username;
global $db_password;
global $db_host;
global $db_database;
static $conn;
$conn = new mysqli($db_host, $db_username, $db_password, $db_database);
if ($conn->connect_error) {
return '<h1>'."Opps there seems to be a problem!".'</h1>'.'<p>'."Your Config file is not setup correctly.".'</p>';
}
return $conn;
}
function db_content() {
//this requires a get, update and delete sections, before its complete
$conn = $this->db_connect();
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}
$query = "SELECT * FROM content";
// Escape Query
$query = $conn->real_escape_string($query);
// Execute Query
if($result = $conn->query($query)){
// Cycle through results
while($row = $conn->mysqli_fetch_all()){
//echo $row->column;
}
}
}
}
$core = new Core();
?>
I am trying to create a db_connect function, which I want to be able to call anywhere on the site that needs a database connection, I am trying to call that function on a function within the same class, I want it to grab and display the results from the database. I am running PHP 5.4.7, I am calling the database on a blank php file which includes a require to include the class file, then using this at the moment $core->db_content(); to test the function. I am building this application from scratch, running from MySQLi guides (not used MySQLi before, used to use normal MySQL query's) so if I am doing anything wrong please let me know, thanks everyone.
mysqli_fetch_all is a method of a mysqli_result, not mysqli.
So presumably it should be $result->fetch_all()
References:
http://php.net/manual/en/mysqli-result.fetch-all.php
Important: keep in mind mysqli_result::fetch_all returns the whole result set not a row as you assume in your code
There are three problems I see here.
while($row = $conn->mysqli_fetch_all()){
The method name is fetch_all() when used in the OOP way.
fetch_all() should be used with the $result object
fetch_all() is only available when the mysqlnd driver is installed - it frequently is not.
Reference
Only $result has that method. If you want to use it in a while loop use fetch_assoc(). fetch_all() returns an associative array with all the data already.
while($row = $result->fetch_assoc()){
}
thanks all, its working fine now, i had it as while($row = $conn->fetch_assoc()){
} before and changed to what i put above, but dident see it should of been $result instead of $conn, thanks for pointing that out.

Switching to mysqli from Mysql. Can't get simple query workign

Decided to move to procedural style coding. I've tried the below code and receive no errors or returned data. What am I doing wrong? Blanked out the mysqli_connect parameters because I am using a shared server - assume the parameters are correct. Thanks in advance.
function login(){
$connect = mysqli_connect("**********", "******", "****", "*****");
$result = mysqli_query($connect, "SELECT * FROM `new_base`");
$bang = mysqli_fetch_all($result);
echo $bang; }
NOTE: The call to the function does work echoing a string does get returned to the page.

Include no database selected

I have a project (exisiting) and I am ordered to continue it
but there's something strange
in my connection
<?php
include "adodb5/adodb.inc.php";
$config['mysqlhost'] = 'localhost';
$config['mysqluser'] = 'xxx';
$config['mysqlpass'] = 'yyy';
$config['mysqldb'] = 'zzz';
$DB = ADONewConnection('mysql');
$DB->Connect($config['mysqlhost'],$config['mysqluser'],$config['mysqlpass'],$config['mysqldb'],true);
?>
and if I try to call query (same queries as below) from this page, it works (and when I echo, it shows the value)
So I go to other page
<?
include ("inc/con.php");
?>
<?php
$sql = ("SELECT * FROM table");
$query = mysql_query($sql)or die($myQuery."<br/><br/>".mysql_error());
$result = mysql_fetch_array($query);
echo $result ['table id'];
?>
and the result is
Notice: Undefined variable: myQuery in C:\xampp\htdocs\d88\www\mypage.php on line 9
No database selected
is there anything wrong with it?
since i try on con page, it works and when i include it to other page, it not working
You are not defining any $myQuery either in inc/con.php nor in the same file itself. Also you are not selecting any database with mysql_select_db:
mysql_select_db($config['mysqldb']);
You are suggest, also, not to use mysql_* functions as they are going to be deleted and are yet deprecated (and you can use PDO or mysqli).
Notice: I think $sql = ("SELECT * FROM table") gets evaluated as $sql = true.
You can not connect with ADODB connection and establish a query with mysql_query.
the syntax is something like this mysql_query ($query ,$con). $con is optional but if you do not specify it, the last link opened by mysql_connect() is assumed; but you have not any mysql_connect() statement before
because of my version of php, i must use <?php ?> instead of <? ?>
thanks for helping

Categories