How to get specified value from DB? - php

How can I get this value from DB as string or int?
PHPMyAdmin http://img200.imageshack.us/img200/6388/gr55.jpg
I've tried this:
$var = $this->db->query("SELECT SUBSTRING(`category_id`, 1, 3)
AS `category_id` FROM " . DB_PREFIX . "category
WHERE merlion_id = 'Q1'
ORDER BY category_id ASC");
And then I've checked it with var_dump and receive this:
var_dump($var);
object(stdClass)#26 (3) { ["row"]=> array(1) { ["category_id"]=> string(3) "859" } ["rows"]=> array(1) { [0]=> array(1) { ["category_id"]=> string(3) "859" } } ["num_rows"]=> int(1) }

No need to do SUBSTRING:
$var = $this->db->query("`category_id`FROM " . DB_PREFIX . "category
WHERE merlion_id = 'Q1'
ORDER BY category_id ASC");
Then, you have it, you don't "convert" the object, you take what you need from it:
$category_id = $var->row["category_id"];
I suggest you dive into basic PHP before you continue with your database-driven application. This is all trivial stuff. There are many good tutorials out there, also about PHP object oriented programming. Most notably the PHP documentation site.

How about this:
echo $var->row['category_id'];
You can also simplify your query:
$var = $this->db->query("SELECT `category_id`
AS `category_id` FROM `" . DB_PREFIX . "category`
WHERE merlion_id = 'Q1'
LIMIT 1");
Added backticks around the table name
Removed your ORDER clause. You do not need it if you only have one dataset whose *merlion_id* is Q1.
Added LIMIT clause since you only want one dataset.

Related

SQL query not returning results could Type be the problem?

The DB has a 'transaction' with a value of 217 and another of 1 in the clientID column the date and dateEntered columns have entries from 2021 and 2022.
The query returns results when I search by date and dateEntered but as soon as the query includes the value for the clientID I get no results.
php:
var_dump($params); // This returns the Array shown below
echo('<h2>' . $query . '</h2>'); // This returns the Query shown below
$query = $this->query($query, $params);
return $query->fetchAll(PDO::FETCH_ASSOC);
Query:
SELECT * FROM`transaction` WHERE `date` LIKE :date AND `dateEntered` LIKE :dateEntered AND `affiliateID` LIKE :affiliateID AND `clientID` LIKE :clientID AND `supplierID` LIKE :supplierID
Array:
array(5) { ["date"]=> string(4) "%20%" ["dateEntered"]=> string(4) "%20%" ["affiliateID"]=> string(2) "%%" ["clientID"]=> string(5) "%217%" ["supplierID"]=> string(2) "%%" }
I've noticed that the array contains type of "string" and the DB is typed as INT, could this be my problem? I've never run into type problems in DB searching before. I make the array with the following code:
$value = '%' . $paramaters['values'][0] . '%';
$query .= ' `' . $paramaters['column'] . '` LIKE :' . $paramaters['column'] . ' AND';
$params[$paramaters['column']] = $value;
If the typing is my problem, how would I change the type in the array I'm constructing here? If typing is not my problem, where else should I look?

Getting right value from $wpdb

Before creating new post programmatically I want to check if there is a post with the same headline in my database.
I am checking that like this:
$results = $wpdb->get_results("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'candidates' AND post_title = '$name'");
Example of value I get when I var_dump($results):
array(1) { [0]=> object(stdClass)#85 (1) { ["COUNT(*)"]=> string(1) "8" } }
Now I want to get that string(1) "8", add it to variable and convert it to number, but I am not sure how to get it.
I tried something like
$number = (int)$results[0]->COUNT(*)
but that isn't right. I also tried some other combinations but couldn't figure it out
Your very problem is the (*) in $number = (int)$results[0]->COUNT(*). You cannot use these special characters simply as 'variable'. You're better off using a column alias using AS your_column_alias.
So you want to use
SELECT COUNT(*) as post_count FROM $wpdb->posts WHERE post_type = 'candidates' AND post_title = '$name'
as query and
$number = (int) $results[0]->post_count;
to fetch the aliased column from your resultset.

Wordpress - wpdb query does not list same result as sql query

I have a very weird issue with wpdb which is freaking me out. My database has the following data:
INSERT INTO `wp_clb_likes` (`id`, `post_id`, `user_ip`, `time`)
VALUES
(5,11511,'::1','2015-01-25 09:23:26'),
(6,11511,'::1','2015-01-25 09:23:34'),
(7,11511,'::1','2015-01-25 09:24:07'),
(8,11511,'::1','2015-01-25 09:24:44'),
(9,11511,'::1','2015-01-25 09:28:53'),
(10,11511,'::1','2015-01-25 09:29:23'),
(11,11511,'::1','2015-01-25 09:31:06'),
(12,11511,'::1','2015-01-25 09:31:30'),
(13,11172,'::1','2015-01-25 09:43:10'),
(14,11511,'::1','2015-01-25 20:23:31'),
(15,11315,'::1','2015-01-25 20:32:01'),
(16,11025,'::1','2015-01-25 20:32:59');
In WP I have the query:
public static function getAllObjects() {
global $wpdb;
global $clb_table;
$sql = $wpdb->get_results("SELECT COUNT(*) as likes, post_id from $clb_table GROUP BY post_id ORDER BY likes DESC", OBJECT_K);
return $sql;
}
My var_dump on the WP fronted shows this:
array(2) { [9]=> object(stdClass)#4818 (2) { ["likes"]=> string(1) "9" ["post_id"]=> string(5) "11511" } [1]=> object(stdClass)#4817 (2) { ["likes"]=> string(1) "1" ["post_id"]=> string(5) "11172" } }
Why I am only seeing post_id 11511 and 11172 and not 11315 and 11025 as well?
Does anybody know the reason? Thanks!
Solved it myself. Because of the OBJECT_K setting in the get_results() the function does not list all items of the database...I replaced OBJECT_K with the default OBJECT and it works.

2 identic functions do something different

Im doing two queries to a database, they are functional the same, but with other values.
CODE1 :
$sql= "SELECT sum(amount) as Samount, sum(add25) as Sadd25, sum(add50) as Sadd50, sum(deplacement) as Sdeplacement FROM workhours WHERE reportID = '$reportID'";
$sumWorkhoursData=mysqli_query($link,$sql);
unset($sql);
while($sWorhoursInfo=mysqli_fetch_array($sumWorkhoursData)){
$totalWork = $sumWorkhoursInfo['Samount'];
print_r(var_dump($sWorkhoursInfo));
}
the var_dump gives NULL
If the $sql is copied to database manually, then it gets a good result which is one row.
CODE2 :
$sql= "select sum(time) as Stime, sum(amount) as Samount FROM trip WHERE reportID = '$reportID'";
$sumTripData=mysqli_query($link,$sql);
unset($sql);
while ($sTripInfo=mysqli_fetch_array($sumTripData)){
$totalTrip = $sTripInfo['Stime'];
print_r(var_dump($sTripInfo));
}
in this case, var_dump gives array(4) { [0]=> string(2) "20" ["Stime"]=> string(2) "20" [1]=> string(3) "151" ["Samount"]=> string(3) "151" }
what is wrong here?
You made a typo $sWorhoursInfo in while($sWorhoursInfo where it should be $sWorkhoursInfo missing the k
while($sWorkhoursInfo...

How retrieve data from a 2D array on this particular case?

I have a array with this var_dump value:
$query = $wpdb->get_results("SELECT COUNT(*) FROM `$table` WHERE my mysql conditions");
var_dump($query); is this:
array(1) { [0]=> object(stdClass)#414 (1) { ["COUNT(*)"]=> string(3) "494" } }
How can I get the number 494 inside a variable?
$number = $query[0];
It'd be cleaner if you aliased your count so:
SELECT COUNT(*) AS `count`
then
$number = $query[0]->count;
If you only need to retrieve a single value, use $wpdb->get_var() instead of get_results(). This method exists specifically for situations like yours.
$number = $wpdb->get_var("SELECT COUNT(*) FROM `$table` WHERE my mysql conditions");

Categories