php mysqli will not get results - php

I cant reslly fidn the answer to this anywhere as its quite unique to my situation.
I have the following code
$accountid = "%%GLOBAL_accountcustomer%%";
echo $accountid;
$results = $mysqli->query("SELECT * FROM exhibitor_list WHERE companyid='$accountid' ");
When I echo $accountid I get the right the id from the database. (in this case number 1)
But when trying to use $accountid in the WHERE query it displays nothing.
If i manualy change the the WHERE query to
WHERE companyid='1'
It displays the row I want to display.
I have also tried stripping $accountid of any whitespace to see if it helps but it doesnt.
Any help appreciated

In your question
but when trying to use $accountid in the WHERE query it displays nothing.
Simply it means No Data passing to this $accountid
so if $accountid is empty below query will not work
$results = $mysqli->query("SELECT * FROM exhibitor_list WHERE companyid='$accountid' ");

When you use query function, it cant be easier to debug your sql ;)
For example:
$sql = "SELECT * FROM exhibitor_list WHERE companyid='$accountid' ";
// $results = $mysqli->query($sql);
echo $sql; die();
And you have your error. You dont parse %%GLOBAL_accountcustomer%% so dont use it.

Related

PHP variable is not working with WHERE clause

My query is not working when I use the variable in the WHERE clause. I have tried everything. I echo the variable $res, it shows me the perfect value, when I use the variable in the query the query is not fetching anything thus mysqli_num_rows is giving me the zero value, but when I give the value that the variable contains statically the query executes perfectly. I have used the same kind of code many times and it worked perfectly, but now in this part of module it is not working.
Code:
$res = $_GET['res']; // I have tried both post and get
echo $res; //here it echos the value = mahanta
$query = "SELECT * FROM `seller` WHERE `restaurant` = '$res'"; // Here it contains the problem I have tried everything. Note: restaurant name is same as it is in the database $res contains a value and also when I give the value of $res i.e. mahanta in the query it is then working.
$z = mysqli_query($conn, $query);
$row2 = mysqli_fetch_array($z);
echo var_dump($row2); // It is giving me null
$num = mysqli_num_rows($z); // Gives zero
if ($num > 0) {
while ($row2 = mysqli_fetch_array($z)) {
$no = $row2['orders'];
$id = $res . $no;
}
}
else {
echo "none selected";
}
As discussed in the comment. By printing the query var_dump($query), you will get the exact syntax that you are sending to your database to query.
Debugging Tip: You can also test by pasting the var_dump($query) value in your database and you will see the results if your query is okay.
So update your query syntax and print the query will help you.
$query = "SELECT * FROM `seller` WHERE `restaurant` = '$res'";
var_dump($query);
Hope this will help you and for newbies in future, how to test your queries.
Suggestion: Also see how to write a mysql query syntax for better understanding php variables inside mysql query
The problem is the way you're using $res in your query. Use .$res instead. In PHP (native or framework), injecting variables into queries need a proper syntax.

Trying to get property of non-object (PHP CodeIgniter)

My code just for save the data session.
This is my code:
$idfb = 12121983918 // just sample id.
$sql = $this->db->query('SELECT * from user WHERE facebookid = "'.$idfb.'" LIMIT 1');
$datalogin = $sql->row();
$loginsession= array(
'jenis_user' =>$datalogin->jenis_user, // the problem is here, i got eror in here, the errors is Trying to get property of non-object .
'photo'=>$datalogin->photo,
'facebookid'=>$datalogin->facebookid,
'id'=>$datalogin->id,
'email'=>$datalogin->email,
'username'=>$datalogin->username,
'nama'=>$datalogin->nama,
// 'jenis_user'=>$datalogin->jenis_user,
'alamat'=>$datalogin->alamat,
'no_telpon'=>$datalogin->no_telpon,
);
$this->session->set_userdata('loginsession',$loginsession);
redirect($this->agent->referrer());
I hope someone can solved this problem. Thanks! :)
It seems you are going right
Please double check your sql, make sure that you are using right table and column names.
and try replacing
$sql = $this->db->query('SELECT * from user WHERE facebookid = "'.$idfb.'" LIMIT 1');
with
<?php
$this->db->where('facebookid', $idfb);
$this->db->limit(1);
$sql = $this->db->get_where('user');
?>
if same error, try echo $sql->num_rows() to see if you are getting row or not.
Since database structure is not given, I can assume the database query is returning 0 rows. Always try to handle exception.
$idfb = 12121983918; // just sample id.
$sql = $this->db->query('SELECT * from user WHERE facebookid = "'.$idfb.'" LIMIT 1');
if($sql->num_rows()>0){
$datalogin = $sql->row();
$loginsession= array(
'jenis_user' =>$datalogin->jenis_user, // the problem is here, i got eror in here, the errors is Trying to get property of non-object .
'photo'=>$datalogin->photo,
'facebookid'=>$datalogin->facebookid,
'id'=>$datalogin->id,
'email'=>$datalogin->email,
'username'=>$datalogin->username,
'nama'=>$datalogin->nama,
// 'jenis_user'=>$datalogin->jenis_user,
'alamat'=>$datalogin->alamat,
'no_telpon'=>$datalogin->no_telpon,
);
$this->session->set_userdata('loginsession',$loginsession);
redirect($this->agent->referrer());
}
else
{
Your Code
}

Displaying users with a php, sql count?

I have a database with users input and was wanting to output a user table (id, username) as a count on a page. The following piece of code is what I've been trying to work with but I've been having no luck and it keeps getting more and more complex - the SQL works perfectly so I'm not sure what's wrong.
mysqli_select_db($db);
$result = $_POST ['$result'] ;
$result = mysqli_query("SELECT COUNT( * )
FROM users");
$row = mysqli_real_escape_string($result,$db);
$total = $row[0];
echo "Total rows: " . $total;
I'm still learning how to properly link SQL in with PHP. The warnings tell me to add an extra parameter however when I do so it still complains.
I originally wanted a simple COUNT but will change the count to a table array if need be. I understand this maybe a little basic and I may have been going about it the wrong way, but I've hit a wall with it and any help on fixing the COUNT would be greatly appreciated
Replace the call to mysqli_real_escape_string to mysqli_fetch_array and your code will works.
mysqli_real_escape_string is only useful for string escaping when you INSERT or UPDATE data to MySQL.
$row = mysqli_fetch_array ($result);
Please try this code:
$sql="SELECT * FROM users";
$result=mysqli_query($con,$sql);
// Numeric array
$row=mysqli_fetch_array($result,MYSQLI_NUM);
$number = count($rows);
Hope this works.

error grabbing ad code from database using mysql

I am trying to grab ad code from my database and echo it on to the page, but for some reason it is not showing up?
$getad = ("SELECT * FROM ads WHERE place='non-mobile' AND who='adbrite' ");
while($rows = mysql_fetch_array($getad))
{
$code = $rows['code'];
}
$ad1 = $code;
later down the page i print it like this.
<?php print $ad1 ?>
I think your problem is that you don't actually execute the query, you just have saved it in a variable ($getad) and then try to do a fetch af an array containing a string as I see it. If I remeber correctly you have to save you query in a variable, as you did, and then type
$getad = "SELECT * FROM ads WHERE place='non-mobile' AND who='adbrite' ";
$q = $db->query($getad);
// generate results:
while ($q->fetchInto($row)) {
//display or store
}
You should also include checks, for example that this code has extracted at least one row, or that database connection is working, etcetera.

How to assign a MySQL statement to a PHP variable

I have looked all over the web and cannot seem to find a proper way to assign the result of a MySQL query to a PHP variable. The code that I currently have returns a "resource id #3" error. Here is what it looks like.
//Select the 'aQID' of the question that has it's BOOL set to "true"
$currentQ = mysql_query("SELECT aQID FROM approvedQuestions WHERE status='1'");
$cuQ = mysql_result($currentQ,1,"status");
echo $cuQ;
I know that the query will only ever return one record (the active question). But I cannot seem to figure out what function to use.
$currentQ = mysql_query("SELECT aQID FROM approvedQuestions WHERE status='1'");
$rs = mysql_fetch_array($currentQ);
var_dump($rs);

Categories