This is the first time I learn class in PHP, I tried to make a simple search in database.
here is some script from my class:
class DB {
...
function list_query($query) {
$ns = array();
$q = mysqli_query($this->con_(), $query);
while($n = mysqli_fetch_assoc($q)) {
$ns[] = $n;
}
return $ns;
}
...
function num_query($q) {
$num = mysqli_num_rows($q);
return $num;
}
...
}
search script :
$key = "foo";
$qsearch = $db->list_query("SELECT * FROM posts WHERE content LIKE '%".$db->escape_query($key)."%'");
$num = $db->num_query($qsearch);
if ($num == 0) {
echo "<h2>not found</h2>";
} else {
echo "<h2>result for : ".$key."</h2>";
foreach($qsearch as $val) {
echo "<h4>".$val['title']."</h2>";
echo strip_tags($val['content']);
}
}
but there is an error with the num_query() function.
with warning :
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, array given in class/db.php on line 30.
I have checked it with manual mysqli_query() then use the num_query() function, it's work well.
sorry for my english
Your list_query() returns array. And your num_query expects mysqli_result as a parameter.
So, when you write $qsearch = $db->list_query(), you're getting an array; and then you pass that array to num_query.
Perhaps, your num_query should be like:
function num_query($result) {
return count($result);
}
Related
<?php
include("Emp.php");
$Email = $_GET["id"];
User::FileLoader();
$content = "";
foreach (User::$userlist as $user) {
if ($user->get_Email() == $Email) {
if ($user->get_State() == 1) {
$user->set_State() = 0;
}
else if ($user->get_State() == 0) {
$user->set_State() = 1;
}
}
}
header("location:liste.php");
The error message may be a little obtuse, but it's not that hard to understand. The return value of a function call is just that -- a value. Unlike a variable, it is not backed by persistent storage, so you cannot write to it by, for example, using it as the left-hand operand of an assignment.
Thus, both this ...
$user->set_State() = 0;
... and this ...
$user->set_State() = 1;
... are wrong.
You have not presented your User class to inform an answer, but surely the set_State() method expects you to specify the new state via an argument. For example,
$user->set_State(0);
public function InitButton($Name, $Group, $T=0, $L=0, $W=1, $H=1, $BStyle=null, $Text='', $Expire = 0, $Repeat=true)
{
$OldButton = ButtonManager::getButtonForKey($this->UCID, $Name);
//Line 4 below:
if(get_class($OldButton) == 'Button') {
$Button = $OldButton;
} else {
$Button = new Button($this->UCID, $Name, $Group);
}
$Button->T($T)->L($L)->W($W)->H($H);
$Button->BStyle($BStyle);
if(is_array($Text)) {
$Button->Text($Text[0]);
$this->bTexts[$Name] = $Text;
} else {
$Button->Text($Text);
$this->bTexts[$Name][0] = $Text;
}
$Button->Send();
$this->bState[$Name]['ID'] = 0;
$this->bState[$Name]['timestamp'] = time() - 1;
$this->bState[$Name]['override'] = false;
if($Expire > 0) {
$this->bState[$Name]['expire'] = time() + $Expire;
} else {
$this->bState[$Name]['expire'] = -1;
}
$this->bState[$Name]['repeatText'] = $Repeat;
}
PHP Warning: get_class() expects parameter 1 to be object, null given in C:\Users\HP\Desktop\test\test.php on line 4
How do i fix this?
it means that ButtonManager::getButtonForKey($this->UCID, $Name); returns null. If this is expected behavior perhaps change the if-statement to
if ($OldButton && get_class($OldButton) == 'Button')
this checks to see if the button is not null and then if the class is 'Button'
If it is not expected behavior, something is going wrong in ButtonManager
The problem might be that $OldButton can be null, and you can't call get_class on a null value. Apparently, ButtonManager::getButtonForKey($this->UCID, $Name) can return null, and that's your problem. You could fix it by doing this before line 4:
if (!is_null($OldButton)) {
if(get_class($OldButton) == 'Button') {
...
}
}
Please help me with my problem.. can i call once Mysql Select Query from different function from two different function too... Sorry i don't know how to explain.. but below my sample what i want to archive..
I want use single query for 2 function.. so maybe i have to write code like below?
function sqlSelect ($db, $id, $id2) {
$sql = mysqli_query($db, "SELECT * FROM xxx WHERE id='$id' AND id2='$id2'");
$row = mysqli_num_rows($sql);
$field = mysqli_fetch_object($sql);
return $row;
}
but how to use $field inside 2 diff function?
function aaa ($a,$b,$c) {
if($row >= 1){
//Do something with $a $b and $c
//$reget field with $field['column'];
$field['column']; //???
}
return $result;
}
function bbb ($a,$b,$c) {
if($row >= 1){
//Do something with $a $b and $c
//get field with $field['column'];
$field['column']; //???
}
}
what i do right now is
function aaa ($db,$id,$id2,$a,$b,$c) {
$sql = mysqli_query($db, "SELECT * FROM xxx WHERE id='$id' AND id2='$id2'");
$row = mysqli_num_rows($sql);
$field = mysqli_fetch_object($sql);
if($row >= 1){
//Do something with $a $b and $c
//get field with $field['column'];
}
}
function bbb ($db,$id,$id2,$a,$b,$c) {
$sql = mysqli_query($db, "SELECT * FROM xxx WHERE id='$id' AND id2='$id2'");
$row = mysqli_num_rows($sql);
$field = mysqli_fetch_object($sql);
if($row >= 1){
//Do something with $a $b and $c
//get field with $field['column'];
}
}
But if i use what i write and code right now i think it's have to call same query twice. I cannot use query outside function so i have to write query on each function but i want to just write query once and can use in two different function..
Sorry for stupid explanation..
Thank you for help
One implementation can be as follow by passing an array as argument,
<?php
$arg['db']="database";
$arg['tabe']="table";
$arg['search']['id1']="id1";
$arg['search']['id2']="id2";
$arg['do_something']['a']="a";
$arg['do_something']['b']="b";
$arg['do_something']['c']="c";
function searchAndDoSomethingAndReturnResult($arg)
{
$return = NULL;
$query="SELECT * FROM ".$arg['table'];
$flag=false;
foreach($arg['search'] as $key=>$value)
{
if($flag)
$query.=" AND ";
else
$flag=true;
$query.= $key." = '".$value."' ";
}
$row = mysqli_num_rows($query);
$field = mysqli_fetch_object($query);
if($row >= 1)
{
foreach($arg['do_something'] as $job=>$value)
{
// $return[] = "some result"
// do something
}
}
return $return;
}
?>
let me know if this solve your problem
You can either pass in the $field variable as a function argument
<?
$field = ...;
function sql1($field,...,...){
// Use $field here
}
function sql2($field,...,...){
// Use $field here
}
?>
Alternatively, you can use the global keyword to access variables from outside of the function
<?
$field = ...;
function sql1(){
global $field;
// Use $field here
}
function sql2(){
global $field;
// Use $field here
}
?>
I have the following function:
function backtrace($Object=false)
{
$x = 0;
foreach((array)debug_backtrace($Object) as $aVal)
{
$row[$x]['file'] = $aVal['file'];
$row[$x]['line'] = $aVal['line'];
$row[$x]['function'] = $aVal['function'];
$row[$x]['class'] = $aVal['class'];
$row[$x]['args'] = $aVal['args'];
++$x;
}
return $row;
}
But when I use it, I'm getting an error like below:
Warning: debug_backtrace() expects parameter 1 to be long, string given in /mypath/ on line 717 ---> foreach((array)debug_backtrace($Object) as $aVal)
What's causing the error? How can I fix it?
The first parameter of debug_backtrace() is a bitmask of options (i.e. a long). It is a simple boolean true/false in PHP versions prior to 5.3.6.
To fix it, either don't pass in the $Object variable you're currently passing in or update it to be any combination of the supported options that you want to be used.
Example:
$Object = DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT;
If you want to add a pre-condition to your current block of code that will set a default value if $Object is invalid, you could try something like:
function backtrace($Object = false) {
if (!is_long($Object) || (!($Object & DEBUG_BACKTRACE_PROVIDE_OBJECT) && !($Object & DEBUG_BACKTRACE_IGNORE_ARGS))) {
$Object = 0;
}
$x = 0;
foreach((array)debug_backtrace($Object) as $aVal) {
$row[$x]['file'] = $aVal['file'];
$row[$x]['line'] = $aVal['line'];
$row[$x]['function'] = $aVal['function'];
$row[$x]['class'] = $aVal['class'];
$row[$x]['args'] = $aVal['args'];
++$x;
}
return $row;
}
for php >= 5.3.6, you should use bitmask options
function backtrace($Object=false) {
$x = 0;
foreach((array)debug_backtrace($Object ? DEBUG_BACKTRACE_PROVIDE_OBJECT : 0) as $aVal)
{
$row[$x]['file'] = $aVal['file'];
$row[$x]['line'] = $aVal['line'];
$row[$x]['function'] = $aVal['function'];
$row[$x]['class'] = $aVal['class'];
$row[$x]['args'] = $aVal['args'];
++$x;
}
return $row;
}
I'm trying to get a parent category for a given sub category using the following function in PHP.
require_once("Connection.php");
$flag=true;
function get_parent_id($cat_id, $parent_id)
{
if ($parent_id==0)
{
return($cat_id);
}
else if ($flag==true)
{
$data1=mysql_query("select parent_id from category where cat_id=" + $cat_id);
while($row = mysql_fetch_assoc($data1))
{
$parent_id=$row['parent_id'];
}
$flag = false;
}
else if ($flag==false)
{
$data2=mysql_query("select cat_id from category where cat_id=" + $parent_id);
while($row = mysql_fetch_assoc($data2)) //The warning comes from here.
{
$cat_id=$row['cat_id'];
}
$flag = true;
}
$cat_id = get_parent_id($cat_id, $parent_id);
return($cat_id);
}
}
echo get_parent_id($ed_id, $parent_id); //Call the above function.
It always prompts the following warning.
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL
result resource in C:\wamp\www\zoom\Category.php on line 492
Even though there is no error in SQL. The included file Connection.php also works fine on all other pages. I didn't understand at all why this happens.
Strings are concatenated with ., not +.
String Operators