I'm trying to select a specific ID that is auto-generated by the oracle database for a specific session user(from log in and its working fine). I'm using MDB2 to achieve this. I am following the pear: MDB2 manual and did the codes the way it is but I'm getting an error. Please help.
I have used the fetchOne(MDB2_FETCHMODE_ASSOC) to achieve this from the reading manual and its giving an error
<?php
session_start();
$user = $_SESSION['user'];
echo "Welcome:\t".$user;
require_once 'MDB2.php';//pear MDB2
include "conn.php";
$do= "SELECT customer_id FROM customer WHERE username=".$user;
$query= $db->query($do);
if($one=$query->fetchOne(MDB2_FETCHMODE_ASSOC)){
$id= $one['customer_id'];
echo ($id);
}
?>
The number 2 should be printed out but instead there this error:
Fatal error: Call to undefined method MDB2_Error::fetchOne() in C:\wamp64\www\grahams\home.php on line 12
Try using below code.
<?php
session_start();
$user = $_SESSION['user'];
echo "Welcome:\t".$user;
require_once 'MDB2.php';//pear MDB2
include "conn.php";
$do= "SELECT customer_id FROM customer WHERE username=".$user;
$id = queryOne($do);
if(isset($id))
{
echo $id;
}
?>
Since queryOne() query the database and select the column from the given query.
Edit:
in conn.php check whether the connection is okay. following is the example code.
<?php
$dsn = array(
'phptype' => 'oci8',
'hostspec' => null,
'username' => 'oci_username',
'password' => 'oci_password',
);
// Establish database connection
$db = MDB2::connect($dsn);
// Check if the connection was established successfully
if (PEAR::isError($db)) {
die('Could not create database connection. "'.$db->getMessage().'"');
}
?>
Related
I am working on my php as I want to connect to the mysql database using PDO. I have stored the username, password and database in the config file, but I have got a problem with connecting to the mysql database because I keep getting an error.
When I try this:
<?php
//Connect to the database
include('config.php');
$smtps = $link->query('SELECT * FROM sent');
$smtps->execute();
$db = $smtps->get_result();
print($db);
?>
I am getting an error:
Fatal error: Uncaught Error: Call to undefined method
PDOStatement::get_result() in
/home/username/public_html/test_pdo.php:17 Stack trace: #0 {main}
thrown in /home/username/public_html/test_pdo.php on line 17
Here is the line 17:
$db = $smtps->get_result();
Here is the config:
<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_NAME', 'dbtablename');
//$errflag = false;
$link = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.'', DB_USER, DB_PASS);
?>
Can you please show me an example how do you connect to mysql database using PDO when you stored the username, password and database name in config.php?
Thank you.
This is happening because get_result() is not a PDO method.
In this situation you should just use fetch() (link) if you just want the first result or fetchAll() (link) if you want an array of the results
Try this:
$smtps = $link->query('SELECT * FROM sent');
$result = $smtps->fetchAll();
print($result);
You only need to use the excute() when using parameters in your select:
SELECT * FROM sent where id = ?
would be
$smtps = $link->prepare('SELECT * FROM sent where id = ?');
$smtps->execute([$id]);
$result = $smtps->fetch();
print($result);
If u use query method u can without execute method get result as as below cod
include('config.php');
$result = $link->query('SELECT * FROM sent')->fetchAll(PDO::FETCH_ASSOC);
print_r($result);
get_results is not a method of the PDO class. If you want to fetch data from the database in this case, use fetchAll() method of the query object.
<?php
//Connect to the database
include('config.php');
$smtps = $link->query('SELECT * FROM sent');
$smtps->execute();
$db = $smtps->fetchAll();
print($db);
?>
Hope that helps.
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.
<?php
include_once "Database.php";
$server="localhost";
$db="fantasy cricket";
$user="root";
$password="";
$db=new Database($server,$db,$user,$password);
$name=$_POST["name"];
$country=$_POST["country"];
$db->removePlayerFromTeam($name,$country);
This is the code that I am using to delete a row ,but the deletion is not working and database remains same .The removePlayerFromTeam($name,$country) is given below.When i use the query "delete from demo" ,that works and all rows get deleted,but when specifying column and value its not working.
By echoing i get the desired values passed as parameter to the function.Please help me out what is going wrong.
Thanks in advance.
public function removePlayerFromTeam($n,$cn)
{
echo "$n"."<br>"."$cn"."<br>";
$sql = sprintf("DELETE FROM demo WHERE Pname='%s' and country='%s'",$n,$cn);
$v=$this->connection->exec($sql);
if($v>0)echo "ok"."<br>";
else echo "wrong"."<br>";
}
Here's the code:
<?php
function widget_hello_world($vars) {
$username = "database_user";
$password = "database_password";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("database_name",$dbhandle)
or die("Could not select database!");
$test1 = mysql_query("select COUNT(*) FROM Table WHERE `Status`='new'");
mysql_close($dbhandle);
$content = '<table class="table">
<thead><tr><th style="text-align:left;">Title</th><th style="text-align:left;">Data</th></tr></thead>
<tbody>
<tr><td>Test Data 1</td><td>{$test1}</td></tr>
<tr><td>Test Data 2</td><td>{$test2}</td></tr>
<tr><td>Test Data 3</td><td>{$test3}</td></tr>
</tbody>
</table>';
return array( 'title' => 'Hellow World', 'content' => $content );
}
add_hook("AdminHomeWidgets",1,"widget_hello_world");
?>
This is the error message I'm getting when I go to the page:
Connected to MySQL
Unexpected input field parameter in database query.
Obviously it's connecting to the database, but it says there's an issue with the query.
At first I thought the issue was with this line:
$test1 = mysql_query("select COUNT(*) FROM Emails WHERE `Status`='new'");
However, even when I delete that line, the same error keeps on happening. What am I doing wrong?
It seems this error occurs on mysql_close($dbhandle);
However, I also notice that reserved word "Table" should be back-tick to prevent an invalid query. Perhaps something in your callback routine is global and affecting the connection.
Also, use this:
echo mysql_error();
to see any mysql errors during query.
Try to do a separate checking for the DB being selected, like:
<?php $con = mysql_connect('localhost', 'root', 'mypass');
$selected_db = mysql_select_db('DB Name here');
if (!$selected_db)
{
die ('Database not selected : ' . mysql_error());
}
?>
If it says 'Database is not selected' then check the spelling of your Database inside the mysql_select_db(). You could also comment the mysql_close(); just to see if error will still persist.
On the other note it is better to use mysqli_ because mysql_ is deprecated or better yet use PDO.
So I am trying to use PDO to query a database and return the results. I have been following along with the php manual and looking on this site but I'm not seeing a solution to my problem. Basically when I run the code below I get no results, nothing; just a blank page. I have php set to show me errors but none are returned. Here is my code:
<?php
ini_set('display_errors', 'on');
//this is where you put the login information:
$username = "my.user.name";
$password = "my.password";
$hostname = "localhost";
//this connects to the database:
$dbh = new PDO('mysql:host=localhost;dbname=first_base', $username, $password);
//this selects the data from the table(s)
function getResults($stuff) {
$stmt = "SELECT name, date, type, location FROM information ORDER BY name";
foreach($stuff->query($stmt) as $row) {
print $row['name'];
print $row['date'];
print $row['type'];
print $row['location'];
}
}
getResults($stuff)
?>
Try changing to your PDO connection $dbh instead of $stuff when calling the function -
getResults($dbh)