<?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.
Related
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.
I'm trying to create a table with php so I could display data from a database in a browser. I'm using apache, php, mysql and phpmyadmin. the php code is fine - all the examples I've seen on various threads show the same thing.
the problem is when I try to open a php file in a browser I get either ' Invalid argument supplied for foreach()' or 'no database selected'. this probably a basic mistake that I'm doing - but I'm a complete beginner. thanks in advance for any help.
php code below:
<?php
include_once('config.inc.php');
try{
$db = new PDO("mysql:dbname=$db_name;host=$db_server", $db_user, $db_pw);
} catch(PDOException $e) {
die('Connection failed: ' . $e->getMessage());
}
print "<div><h3>Title</h3>";
print "<table><tr><th>Column_1</a></th><th>Column_2</a></th></tr>";
foreach ($db->query("SELECT sth_one, sth_two FROM the_database") as $a) {
$row="<tr><td>$a[sth_one]</td><td>$a[sth_two]</td></tr>\n";
print $row;
}
print "\n</table></div>\n";
?>
Could you supply an example for what you're doing?
Generally this indicates, that you forgot to specify which database to use, while connecting to the mysql server
I'm not sure but I think I read something about the right sequence of parameters to insert in the constructor.
Try with:
$db = new PDO("mysql:host=$db_server;dbname=$db_name", $db_user, $db_pw);
or:
$db = new PDO("dbname=$db_name;mysql:host=$db_server", $db_user, $db_pw);
as indicated here: http://php.net/manual/en/pdo.connections.php
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.
I am trying to connect to mysql via php using this line
# $db = new mysqli_connect('localhost', 'bookorama', 'bookorama123', 'books');
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
I am not getting any response, no error message, nothing. I even added an echo'hi';
after the first line of code and it doesn't show up. When I added echo'hi'; before the first line hi prints out.
Any tips?
Get rid of the new instead do:
$db = mysqli_connect(...
or use new to create a mysqli object
$db = new mysqli(...
In the first case $db is assigned the return value of a function. The mysqli_connect function passes back the object created internal to the function. In the second case $db is being created through the new keyword as the result of the "new mysqli(..." expression.
First of all, remove the # before function calls. # is to suppress any errors from coming up.
Second, remove the new keyword. That's for instantiating a new class, not calling a function.
$db = #mysqli_connect('localhost', 'bookorama', 'bookorama123', 'books');
mysqli_connect is not an object it is function
EDIT:
remove #, make sure your error_reporting( E_ALL );
$db = mysqli_connect('localhost', 'bookorama', 'bookorama123', 'books');
"Call to undefined function mysqli_connect()" - do you have mysqli module on ? - Yes in php.ini file.
I have mananed to use the odbc_connect like the following for access 2007 and 2003 and I can get data. But when I try to get the column names the following function will not work for access 2007 but will for acccess 2003 - why?
if($type[1]=='mdb'){
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $username, $password);
}else{
$connection = odbc_connect("Driver={Microsoft Access Driver (*.accdb)};Dbq=$mdbFilename", $username, $password);
}
function get_columns($activity_file){
global $connection;
global $typee;
$coulmn_array = array();
$result = odbc_columns($connection, $typee, "", $activity_file, "%");
while (odbc_fetch_row($result)) {
$coulmn_array[] = odbc_result($result, "COLUMN_NAME");
}
echo '<br>Exporting table '.$activity_file;
return $coulmn_array;
}
I mean I can get the data and everything, it just seems this function just won't work!
Please help!
Update
I had a google around and found this thread.
I can confirm what this person is saying. Supplying a table_name means this won't work. But if you don't it will. This isn't acceptable as what columns are being returned and for what table? I need to know this!!
This is a long shot, but there was a problem with the ODBC function SQLDescribeCol and SQLColAttributes related to Access 2007. Those functions would likely be used by odbc_columns. It is described in this KB article.