mysql connection throughtout the session - php

I have a problem. Successfully made a login.php page that uses function:
db_connect();
to connect to MySQL database. Now I'm creating another page that would edit previous entries, but I can't access it with simple
db_edit(){... $result = mysqli_query($conn, $insert_query); ...}
because it says either:
"Warning: mysqli_query(): Couldn't fetch mysqli" or " Warning: mysqli_query() expects parameter 1 to be mysqli, null given"
It looks like the connection breaks when jumping to another page, and it should be reestablished from beginning like:
db_connect(); //again?
db_edit();
What is the proper way to pass database connection parameters from one session page to another? Should I pass $conn or $user and $pass? How do I do it?

You must include page of database connection to every page. Here you used db_connect() at one page but in second you didn't use it so you can not access MYSQLI from second page
Let take simple sample
database.php
<?php
// Here place code for database connection for mysqli
$con = mysqli_connect("localhost","root","pass"); //Localhost
if (!$con) {
die('Could not connect: ' . mysqli_error());
}
mysqli_select_db("testdb", $con);
?>
Page1.php
<?php
include_once("database.php");
...
$result = mysqli_query($con, $insert_query);
...
?>
Page2.php
<?php
include_once("database.php");
...
$result = mysqli_query($con, $insert_query);
...
?>
If you have more pages then you use include_once("database.php"); on each page.
here link can help you Click Here

Related

How to fetch from MySql Database when using Include connection?

I'm using include connection file to connect to the database. My challenge is how do I fetch from the database this is where am stuck.
include 'connection.php';
$sql = 'SELECT * FROM country';
$results = mysqli_query($sql);
assume your connection.php contain
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
So the in your file, you're using include 'connection.php' to get the connection. By using include its act like single page now. Then you've to use it like below
require_once 'connection.php';
$sql= 'SELECT * FROM country';
$results = mysqli_query($con, $sql); # add connection string to query
Explanation
when you add this include 'connection.php'; then whatever the data on parent(connection.php) file (ex: variable, Functions, etc ..) will come to child.
Links to refer
In PHP, how does include() exactly work?
Are PHP include paths relative to the file or the calling code?
include, include_once, require or require_once?

PHP to access to a .db file

I am new to PHP and trying to create a search field that searches through my database file. I am putting my database file rc3.db inside the same folder that contains my PHP file and trying to connect it with mysqlite, however I
Tried $con = mysqli_connect("localhost:8888","user","password","rc3.db"); but errors,I don't have a user nor a password. Also tried $con = mysqli_connect(); it works but I'm not sure if which database it is connecting to. I also did the single arguement with rc3.db as below, but it is probably mistaking that for the hostname, which is the the first parameter that the method takes in.
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
<?php
//connection to the database
$con = mysqli_connect("rc3.db");
echo "Connected to MySQL<br>";
?>
</body>
</html>
Warning: mysqli_connect(): (HY000/2005): Unknown MySQL server host 'rc3.db' (22) in /Applications/MAMP/htdocs/searchform.php on line 9
Call Stack
# Time Memory Function Location
1 0.0005 229848 {main}( ) ../searchform.php:0
2 0.0005 230072 mysqli_connect ( ) ../searchform.php:9
As I understand your database is not a mysql db but a sqlLite database.
If that so, you can use the sample code bellow to access the data.
$db = new PDO('sqlite:rc3.db');
$query = $db->prepare("Your sql query here");
$query->execute();
while($row = $query->fetchObject())
{
//do your staff
}
Update 1
As #Sean pointed correctly in the comments as an alternative you can use sqlite_open
$con = sqlite_open('rc3.db', 0666, $error))
The connection to the database using the extension mysqli.
$link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($link));
use PDO like in code below. And read this https://renenyffenegger.ch/notes/development/web/php/snippets/sqlite/index
It really helped me
<?php
$db = new PDO("sqlite:tg.db"); // file tg.db in local folder
$db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $db -> query('SELECT * FROM tgdata'); // select all data from my table 'tgdata'
foreach ($query as $row) {
var_dump($row);
}

Outputting contents of database mysqli

Hi I know this is a little general but its something I cant seem to work out by reading online.
Im trying to connnect to a database using php / mysqli using a wamp server and a database which is local host on php admin.
No matter what I try i keep getting the error Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given when i try to output the contents of the database.
the code im using is:
if (isset($_POST["submit"]))
{
$con = mysqli_connect("localhost");
if ($con == true)
{
echo "Database connection established";
}
else
{
die("Unable to connect to database");
}
$result = mysqli_query($con,"SELECT *");
while($row = mysqli_fetch_array($result))
{
echo $row['login'];
}
}
I will be good if you have a look at the standard mysqli_connect here
I will dont seem to see where you have selected any data base before attempting to dump it contents.
<?php
//set up basic connection :
$con = mysqli_connect("host","user","passw","db") or die("Error " . mysqli_error($con));
?>
Following this basic standard will also help you know where prob is.
you have to select from table . or mysqli dont know what table are you selecting from.
change this
$result = mysqli_query($con,"SELECT *");
to
$result = mysqli_query($con,"SELECT * FROM table_name ");
table_name is the name of your table
and your connection is tottally wrong.
use this
$con = mysqli_connect("hostname","username","password","database_name");
you have to learn here how to connect and use mysqli

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

Cannot Display Data from MySQL table

I've got a pretty standard call to a MySQL database and for some reason I can't get the code to work. Here's what I have:
$mysqli = mysqli_connect("localhost","username","password");
if (!$mysqli)
{
die('Could not connect: ' . mysqli_error($mysqli));
}
session_start();
$sql = "SELECT * FROM jobs ORDER BY id DESC";
$result = $mysqli->query($sql);
$num_rows = mysqli_num_rows($result);
Now, first, I know that it is connecting properly because I'm not getting the die method plus I added an else conditional in there previously and it checked out. Then the page displays but I get the errors:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in blablabla/index.php on line 11
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in blablabla/index.php on line 12
I've double-checked my database and there is a table called jobs with a row of "id" (it's the primary row). The thing that confuses me is this is code that I literally copied and pasted from another site I built and for some reason the code doesn't work on this one (I obviously copy and pasted it and then just changed the table name and rows accordingly).
I saw the error and tried:
$num_rows = $mysqli_result->num_rows;
$row_array = $mysqli_result->fetch_array;
and that fixed the errors but resulted in no data being passed (because obviously $mysqli_result has no value). I don't know why the error is calling for that (is it a difference in version of MySQL or PHP from the other site)?
Can someone help me track down the problem? Thanks so much. Sorry if it's something super simple that I'm overlooking, I've been at it for a while.
You didn't selected the database
$mysqli = mysqli_connect("localhost","username","password","database");
The problem is you haven't selected the database.
use this code for select database.
$mysqli = mysqli_connect("localhost","username","password");
mysqli_select_db("db_name",$mysqli);
You have to select database in order to fire mysql queries otherwise it will give you error.
I believe that schtever is correct, I do not think you are selecting the database. It isn't in the code snip and if you search online you see other people with similar errors and it was because the database wasn't selected. Please let us know if you selected a database before anything else is checked. Thanks.
Try this:
session_start();
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if ($mysqli->connect_errno)
{
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
$mysqli->close();
}
$query ="SELECT * FROM jobs ORDER BY id DESC";
$values = $mysqli->query($query);
if($values->num_rows != 0)
{
while($row = $values->fetch_assoc())
{
//your results echo here
}
}
else
{
//if no results say so here
}
See this manual for mysqli_connect you can select the database right in this function.

Categories