This question already has an answer here:
Check if a value exists in a result_array()
(1 answer)
Closed 8 years ago.
Which function can I use to check if a value exists in a result array?
I tried it the following way but it failed.
$query = $this->db->query($query);
$result = $query->result_array();
if (in_array('Receive', $result)) {
echo 'this array contains Receive';
}
try this one
echo $this->db->last_query();
it will show the last query you executed. Just put it at the end of your function.
EDIT:
Sorry I thought you were asking about the query, my bad. However, if you want to know if your query returned a value in the form of an array, you can use:
$result = $query->result_array();
print_r($result);
Related
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 3 years ago.
php data filtering with URL
I am developing an online shop, where I want to display category wise data. But I can't! Is there anything wrong to my code or URL?
I pass the URL like below
http://localhost/e-bookshop/users/all_books?ctg=1
My Model:
public function get_books()
{
$this->db->select('*');
$this->db->from('category');
$this->db->join('books', 'books.categoryId = category.id');
if(isset($_GET['ctg']))
{
$a = $_GET['ctg'];
$query = $this->db->where('category.id', '$a');
$query = $this->db->get();
return $query->result();
}
$this->db->order_by('books.id', 'DESC');
$query = $this->db->get();
return $query->result();
}
This doesn't show me any error, It just shows blank or nothing. How I can fix this problem? Thanks advance.
You are very much close to what you have desired. I think your URL is okay.
The problem is here. You can not pass an array like that. You pass the array in the string format. For the apostope coma, URL can't read your array. You have to change this line.
$query = $this->db->where('category.id', '$a');
Instead of this line, write like the below line
$query = $this->db->where('category.id', $a);
I think this above line could solve your problem. Just copy this line and enjoy.
This question already has answers here:
CodeIgniter - return only one row?
(11 answers)
Closed 9 years ago.
Having trouble with Codeigniter. I am trying to fetch the value of a product from mysql. The mysql table contain a field called cost_price. I am trying to fetch the result of a single product using the product ID. Here is the code im using,
$query = $this->db->query('SELECT * FROM items WHERE id="$id"');
$row = $query->result();
echo $row['cost_price'];
But nothing is happening ! What is the problem with my code?
I tried some wrong code to check whether the database is responding or not, but it seems that the database is working fine. Can't understand why my query can't find any result !
Try something like this:
$row = $this->db->get_where('items', array('id' => $id))->row();
Or, if you just need the price:
$price = $this->db->select('cost_price')
->get_where('items', array('id' => $id))
->row()
->cost_price;
EDIT
Your method was ok up to a point, look:
$query = $this->db->query('SELECT * FROM items WHERE id="$id"');
$res = $query->result(); // this returns an object of all results
$row = $res[0]; // get the first row
echo $row['cost_price']; // we have an object, not an array, so we need:
echo $row->cost_price;
Alternatively, if you want an array, you can use result_array() instead of result().
My way, row(), returns only the first row. It's also an object, and if you need an array, you can use row_array().
I suggest you read more about all that here.
This question already has answers here:
Get sum of MySQL column in PHP
(9 answers)
Closed 9 years ago.
how do I get the output from this:
$VisitorsProfile = $mysql->query("SELECT SUM(views) FROM visitors_profile WHERE publisher = '123456'");
I tryed this but nothing prints me out!
<?php echo $VisitorsProfile['sum'] ?>
Any suggestions?
you need to name the column, I know it sounds weird but it should be
$VisitorsProfile = $mysql->query("SELECT SUM(views) as totals FROM visitors_profile WHERE publisher = '123456'");
and as indicated as below you need the mysql_fetch_assoc function on the query so you can reference it by name.
Then you can reference it by
<?php echo $VisitorsProfile['totals'] ?>
you just need to reference it "as" something. whatever you put in after the word as is what you can refer to it by, i wouldn't use something like "sum" as that is a mysql function and can cause problems.
PHP allows you to get the information by column name or by column index. The recommended way is to do it by column name, because this will allow you to change your query's column order without affecting your PHP code.
So to get it you need to add a column alias to your query:
$rs = mysql_query("SELECT SUM(value) AS total FROM visitors_profile WHERE publisher = '123456'");
if ($row = mysql_fetch_assoc($rs)) {
echo $row['total'];
}
On the first line you are actually running the query. Check the column alias: "total".
On the second line you are asking for an array or specifically a "map" from the result set. In other words, by calling mysql_fetch_assoc you are asking for an array whose elements are available by name. That array represents a row from your result set.
On the third line you are printing the actual sum.
Check the "if" in the $row assignment. This will help you validate there is a result row before trying to print it. I recommend you to do the same on the line where you call mysql_query.
EDIT:
Sorry, I think you are using objects, so it should look like this:
$rs = $mysql->query("SELECT SUM(value) AS total FROM visitors_profile WHERE publisher = '123456'");
if ($row = $mysql->fetch_assoc($rs)) {
echo $row['total'];
}
This question already has answers here:
How can I loop through a MySQL result set more than once using the mysql_* functions?
(7 answers)
Closed 9 months ago.
I want to use a query result multiple times.
$result=mysql_query("Select field from tables;");
while($result_details = mysql_fetch_array($result))
{ echo $result_details[0]; //Returns 10 Rows..
}
but if I use mysql_fetch_array($result) again its not returning rows..
I don't want to execute the same query multiple times Please let me know how to use the results of a query multiple times.
First fetch all records using a while loop:
$result=mysql_query("Select field from tables;");
while($result_details = mysql_fetch_array($result))
{
$var[]=$result_details;
}
Then display all using a foreach loop and a single variable $var anywhere in the same page where you used query or put query in a functions.php file and include and use anywhere:
foreach ($var as $value) {
$value['field'];
}
If you do not want to make multiple queries then simply unwind the pointer to the desired position.After you execute your code.You should call function to seek to initial (0) position:
$result=mysql_query("Select field from tables;");
while($result_details = mysql_fetch_array($result))
{ echo $result_details[0]; //Returns 10 Rows..
}
mysqli_data_seek($result ,0);
You need to "rewind" result pointer using: http://php.net/manual/en/function.mysql-data-seek.php
This question already has answers here:
Checking if mysqli_query returned any values?
(2 answers)
Closed 4 months ago.
I wrote a php search for a table in mysqli and it works fine but i want to show the correct message to user if no result were found.
here is my current code:
$search_string=$_GET["design"];
$connect= mysqli_connect("mysql.myhost.com","abc","123456","mydb_db");
$query="select * from product where product_design like '%$search_string%'";
$rows= #mysqli_query($connect,$query) or die("Error: ".mysqli_error($connect));
if ($rows!=null)//I put this if to check if there is any result or not but its not working
{
while(($record=mysqli_fetch_row($rows))!=null)
{
.
.//i have working code for showing the result here
.
}
mysqli_close($connect);
}
else{
echo"no result found";
}
could you please help me what is wrong , even when i search for something which is not exist in the db still the program not displaying "no result found"
Thank you
What you need is mysqli_num_rows specifically the mysqli_result::num_rows bit. This adds a num_rows property to mysqli result sets. This means you can do
$rowCount = $rows->num_rows
There's also a non-OO equivalent ...
$rowCount = mysqli_num_rows($rows);
(The difference is purely one of coding style)
Use one of these to determine how many records are returned and output the appropriate messages.
The following line doesn't make sense, that might be the issue.
while(($record=mysqli_fetch_row($rows))!=null)
However, $row wouldn't return 'null' if it was empty, but be not set. Do it like this:
if ($rows) { echo 'works';} else { echo 'no rows'; }