I've looked all over the place and cant find a solution that helps my case. I hope someone can help?
I have this and receive
Warning: sqlite_query() expects parameter 1 to be resource, string given
It is relating to $dbresult line - so a problem with the query.
function Up(){
$dbquery = "DELETE FROM toolList WHERE toolId='".$data['toolId']."'";
$dbresult = sqlite_query($dbhandle, $dbquery);
}
foreach($result as $data){
print '< a href="'.Up().'">DELETE!< /a>';
}
Where is $dbhandle defined? I'm guessing it's a global variable, in which case you have to explicitly mention so within the Up function:
function Up(){
global $dbhandle, $data, $dbresult;
$dbquery = "DELETE FROM toolList WHERE toolId='".$data['toolId']."'";
$dbresult = sqlite_query($dbhandle, $dbquery);
}
You are either missing a call to sqlite_open(), or your $dbhandle variable is not available in your function.
Related
I'm getting this error on my site after I tried to convert to mysqli on line 26 ($rs = mysqli_query($link,MySQLi) or die(mysqli_connect_error());)
My code is like this
//Get configuration control record from database
$MySQLi = "SELECT configValLong
FROM storeadmin
WHERE configVar = 'controlRec'
AND adminType = 'C' ";
$rs = mysqli_query($link,MySQLi) or die(mysqli_connect_error());
$totalRows = mysqli_num_rows($rs);
if ($totalRows!=0){
$rows = mysqli_fetch_array($rs);
$configArr = trim($rows["configValLong"]);
if (strlen($configArr) > 0 ){
$configArr = explode("*|*",$configArr);
}
If you have defined, $link=mysqli_connect() outside the function, you need to pass it as a parameter to the function or declare it global inside the function.
1. Passing as parameter
yourFunction($link); //function call, passing the variable to the function
function yourFunction($link) //you can use any name for parameter
{ //now it can be accessed here
}
2. Declaring as Global inside the function
function yourFunction()
{
global $link;
//now it can be accessed here
}
By using the keyword global, you are specifying that you want to use the $link which you have defined in global scope.
You may have to do the same with $MySQLi and other variables if you
have defined them outside the function.
On this line: $rs = mysqli_query($link,MySQLi) or die(mysqli_connect_error());, your variable MySQLi needs the variable sign $.
So change that to this:
$rs = mysqli_query($link,$MySQLi) or die(mysqli_connect_error());
I have similar code that works with no errors but when I try and query my database I'm getting the several errors. Can anyone help ?
errors:
Notice: Undefined variable: link in C:\wamp64\www\twitter clone\functions.php on line 22
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp64\www\twitter clone\functions.php on line 22
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\wamp64\www\twitter clone\functions.php on line 24
Code;
<?php
session_start();
$link = mysqli_connect("localhost", "root", "pass", "twitter");
if (mysqli_connect_errno()) {
print_r(mysqli_connect_error());
exit();
}
if (isset($_GET['function']) == "logout"){
session_unset();
}
function displayTweets($type) {
if ($type == 'public'){
$whereClause = "";
}
$query = "SELECT * FROM tweets ".$whereClause." ORDER BY 'datetime' DESC LIMIT 10";
$result = mysqli_query($link, $query);
if (mysqli_num_rows($result) == 0) {
echo "No Tweets";
}
}
?>
The problem is that your $link variable is not in the function's local scope.
A way to fix this is by defining the variable in the function's local scope by adding the following line to the function:
global $link
Please have a look at this page to read more about the variable scope in PHP.
Edit:
An even better way would be to inject your connection by adding it as an argument to your function, which would have your code look something like this:
function displayTweets($type, $connection) {
if ($type == 'public'){
$whereClause = "";
}
$query = "SELECT * FROM tweets ".$whereClause." ORDER BY 'datetime' DESC LIMIT 10";
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) == 0) {
echo "No Tweets";
}
}
After this, you would call your function using displayTweets('public',$link), with 'public' being the type, and $link being your defined connection
Also, in your current function, $whereClause could be undefined. I'm guessing your aware of this, just wanted to state it in case you get errors on that.
You have to use $link variable as global variable ($GLOBALS['link']) in displayTweets function or you have to pass $link as parameter.
$result = mysqli_query($GLOBALS['link'], $query);
or
displayTweets('public',$link);
I'm using mysqli for a function, and I'm getting an error, Fatal error: Call to a member function query() on a non-object in /home/u250000297/public_html/forum/system/db.php on line 46 I've tried different things, but I just get different errors, what am I doing wrong and where is my error?
Code lines 45-51:
function fetch($query) {
$sql = $mysqli->query($query);
$result = $sql->fetch_array(MYSQLI_BOTH);
return $result;
$sql->free();
}
Here's my previous attempt and error : Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/u250000297/public_html/forum/system/db.php on line 46 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/u250000297/public_html/forum/system/db.php on line 47 Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/u250000297/public_html/forum/system/db.php on line 46 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/u250000297/public_html/forum/system/db.php on line 47
Code, lines 45-51:
function fetch($query) {
$sql = mysqli_query($mysqli, $query);
$row = mysqli_fetch_array($sql, MYSQLI_BOTH);
return $row;
mysqli_free_result($row);
}
Connection:
$mysqli = mysqli_connect($this->host,$this->username,$this->password);
mysqli_select_db( $mysqli,$this->database );
if ($mysqli->connect_error) {
trigger_error('Database connection failed: ' . $mysqli->connect_error, E_USER_ERROR);
}
Either you need to define $mysqli as global or pass it as an argument.
After a return, no statement will be executed (mysqli_free).
Global:
function fetch($query) {
global $mysqli;
$sql = $mysqli->query($query);
$result = $sql->fetch_array(MYSQLI_BOTH);
return $result;
}
Parameter (and even better):
function fetch($myslqi, $query) {
$sql = $mysqli->query($query);
$result = $sql->fetch_array(MYSQLI_BOTH);
return $result;
}
Points to debug are
1) Is the connection to the database getting established?
2) Are your query strings correct?
Call to a member function query() on a non-object and Warning: mysqli_query() expects parameter 1 to be mysqli means that the $mysqli is null or isn't initialized properly. To get more information check the mysql error logs.
I've tried to look this up, but I'm not quite getting an answer that pertains directly to what I'm doing. I'm trying to retrieve values from a database that were previously submitted. I want to use these values to locate filepaths on the server and use the fillepath information to display the files. To do this, I need to separate the array that i have here. How would I separate this array?
$code = $_POST['codeInput'];
$code = htmlspecialchars($code);
$submitCodes = "SELECT story,video FROM `storycodes` WHERE `code` = $codeInput";
$files = mysql_fetch_array($submitCodes);
mysql_close($con);
print_r(array_values($files));
I appreciate any help guys. Ideally I would like to get a variable like $story or $storyPath and $video or $videoPath I need the video as a variable so I can play it with a video player.
EDIT:
I changed from mysql to mysqli and I'm getting all of these errors now that I can't seem to fix. I have the result set and it says I don't.
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home/content/98/10339998/html/scripts/stories.php on line 26
I think this warning should fix itself once the array gets sorted out:
Warning: extract() expects parameter 1 to be array, null given in /home/content/98/10339998/html/scripts/stories.php on line 28
Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given in /home/content/98/10339998/html/scripts/stories.php on line 30
$con = mysqli_connect("storycodes.db.10339998.hostedresource.com",$username,$password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysqli_select_db($con, "storycodes");
$code = $_POST['codeInput'];
$code = htmlspecialchars($code);
$query = "SELECT story,video FROM `storycodes` WHERE `code` = $codeInput";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_assoc($result);
extract($row);
mysqli_free_result($result);
mysqli_close($con);
echo $story . $video;
use it like this
$files = mysql_fetch_array($submitCodes);
extract( $files );
// now you can use it like this
echo $story . $video; // as variables
$files = mysql_fetch_assoc( $submitCodes );
extract( $files );
I try to write own database class.I have some problem.
When I run it , it gives these errors.
Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in /Users/emrecetin/www/ayrox/m/db.php on line 75
Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean given in /Users/emrecetin/www/ayrox/m/db.php on line 76
Warning: mysqli_stmt_bind_result() expects parameter 1 to be mysqli_stmt, boolean given in /Users/emrecetin/www/ayrox/m/db.php on line 77
Warning: mysqli_stmt_fetch() expects parameter 1 to be mysqli_stmt, boolean given in /Users/emrecetin/www/ayrox/m/db.php on line 78
on these line;
mysqli_stmt_bind_param($sor, 's', $hede);
mysqli_stmt_execute($sor);
mysqli_stmt_bind_result($sor, $bas);
mysqli_stmt_fetch($sor);
I will give short version of databse class.It is not exactly, I write just what I use.
class db{
public $durum;
protected $server = 'localhost';
protected $suser = 'root';
protected $spass = 'root';
public $db = 'members';
public $durum; // mysqli durumu
public $sor; // mysql_query ile birleştirilen hali
public $kacsonuc;
function db(){
$this->durum = mysqli_connect($this->server,$this->suser,$this->spass);
$this->dbchoose($this->db)
}
function dbchoose($db){
if(!mysqli_select_db($this->durum,$db)){
$this->errors[] = 'Database cant choose';
$this->hata=True;
}
function see(){
$id = 'select * from uyeler where id=?';
$sor = mysqli_prepare($this->durum, $id);
$hede = '3';
mysqli_stmt_bind_param($sor, 's', $hede);
mysqli_stmt_execute($sor);
mysqli_stmt_bind_result($sor, $bas);
mysqli_stmt_fetch($sor);
}
}
what is the problem ? I cant understand.Thank you (and sorry for my grammer).
Anytime you see "boolean given" in a mysql error message, it means that some previous mysql function call has failed, returning a boolean FALSE value. You're trying to use that boolean false in the current mysql call, and get this message.
You have no error handling in your code, which means those FALSE values will propagate throughout your code, spitting out those errors everywhere it goes.
At minimum, your code should look something like this, everywhere you do a mysql/mysqli call:
function see(){
$id = 'select * from uyeler where id=?';
$sor = mysqli_prepare($this->durum, $id);
if ($sor === FALSE) {
die(mysqli_error($this->durm));
}
etc...
}
How you handle the error is up to you - in this case it'll simply abort the script and tell you why.