How to fetch a single result from mysql using code igniter? [duplicate] - php

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.

Related

Get values from the database by url parameters [duplicate]

This question already has answers here:
How to include a PHP variable inside a MySQL statement
(5 answers)
Closed 2 years ago.
I'm trying to get values ​​from the database depending on the url. So, in my db I have a column with a keystring that is a unique number that I am generating, what I want to do is: From the URL, like "example.com/?keystring=dss76da1" I can get the column value 'email' associated with this keystring and use it in the current page with a variable.
My sql table
I started the code that way but I don't know if it's right.
(URL: example.com/?keystring=dss76da1):
<?php
$key = intval($_GET['keystring']);
$results = mysql_query("SELECT * FROM users WHERE keystring='".mysql_real_escape_string($key)."'");
while ($row = mysql_fetch_array($results))
What should I do to get the value of the email column that is associated with the keystring and put it in a variable? So I can use on the current page..
<?php
$key = $_GET['keystring'];
$results = mysql_query("SELECT * FROM users WHERE keystring='".mysql_real_escape_string($key)."'");
while ($row = mysql_fetch_array($results)) {
}
?>

php array to string conversion odbc_exec count

i'm still new towards php but after searching through multiple topics i can't seem to figure this one out.
$query2 = "SELECT COUNT(MAP_CODE2) FROM TCODE_MAPPING WHERE MAP_CODE2 = 'ABC123'";
$result2 = odbc_exec($connect,$query2);
echo $result2;
i have a query above that i'd like to use to get the total number of rows within the query that i've set, however for some reason i keep getting hit by the error
Array to string conversion in /var/www/html/xxx.php on line 85
Would highly appreciate if anyone could help out on what i'm doing wrong. Thank you!
Problem is:-
You are trying to echo a result-set object of array type.
Solution (check the code comments):-
$query2 = "SELECT COUNT(MAP_CODE2) AS MYCOUNT FROM TCODE_MAPPING WHERE MAP_CODE2 = 'ABC123'"; // given a name to the count
$result2 = odbc_exec($connect,$query2); //prepare and execute query
while (odbc_fetch_row($result2)) { //iterate over result-set object
echo odbc_result($result, "MYCOUNT"), "\n"; // echo count
}
What is happening here is you are echoing an array object type that is also a resource type and php echo only string and other primitive type variables.
so you need to use a loop to access the row or to get row count and access row just use a foreach($results as $result)
to get row count visit this sqlsrv-num-rows . on this official php link you can get more example how to fetch data.

How to add title in JPGraph from mysql database

I generate chart using jpgraph. I succesfully get data for the plot from database and i want the title also take from database. i already use this script
$sql = $this->db->select("title from table")->get()->first_row();
$title = $sql->title;
$graph->title->Set($title);
But that not work. can anyone solve this issue? thank you
This will help to get first_row along with field:
$this->db->select('title'); // your column
$this->db->from('table'); // your table
$result = $this->db->get()->result(); // get result
$title = $result->first_row()->title; // get ist row using first_row with your field name
$graph->title->Set($title); // as you are using for graph
Also note that, in CI function row() also return the ist row of your query in object form.
From the manual:
You can walk forward/backwards/first/last through your results using
these variations:
$row = $query->first_row()
$row = $query->last_row()
$row = $query->next_row()
$row = $query->previous_row()
Or if you want to print result in array instead of object than you can use a word 'array' as param, like:
$query->first_row(‘array’)
You can also follow the CI manual for better understanding: http://www.codeigniter.com/userguide3/database/results.html

Codeigniter search for a value inside result array [duplicate]

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);

Sum a field in mysql [duplicate]

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'];
}

Categories