How to take value from array into integer (PHP) - php

Input :
$sql1 = "SELECT COUNT(*) FROM matchTrip where userTripId = :tripId";
$stmt1 = $this->db->prepare($sql1);
$stmt1->bindParam(':tripId', $trip, PDO::PARAM_INT);
$temp = $stmt1->fetchObject();
echo(json_encode($temp));
Output:
How to take value from array :
of which json_encode looks like this: {"COUNT(*)":"7"}
Any help will be appreciated.

Why don't you just give the column an alias in the SQL itself?
$sql1 = "SELECT COUNT(*) as myCount FROM matchTrip where userTripId = :tripId";
Makes the rest easier to work with.

Do you mean json_decode? You can just put it between quotes and it should work; $array["COUNT(*)"].
But you can also add "AS myCount" to your SQL.

if to get rid of all the useless stuff from your code
$sql = "SELECT COUNT(*) FROM matchTrip where userTripId = ?";
$stmt = $this->db->prepare($sql);
$stmt->execute(array($table_of_user[$i]));
$count = $stmt->fetchColumn();
echo $count;

So why not just fecth as array?
$temp = $stmt1->fetch(PDO::FETCH_ASSOC);
echo $temp['COUNT(*)'];

Just use like this:
$json = json_encode($temp);
echo $json->{'COUNT(*)'}; // 7

Related

PHP PDO count and sum value from MySQL table

Well, I'm pretty sure this is just a novice question, so please forgive me for that, but I feel like I'm losing my mind.
I have a simple MySQL rating table and I need to count rows and to sum rates values (int) with PHP PDO
$sql = "SELECT rate FROM rating_table";
$query = $db->query($sql);
$rate_times = count($query->fetchAll()); // it works!
echo '<p>'.$rate_times.'</p>';
$sum_rates = array_sum($query->fetchAll()); // it doesn't work!
echo '<p>'.$sum_rates.'</p>';
Thank you in advance for any suggestion
If I understand you right, all you have to do is to modify your sql request, this will return a single row
sql = "SELECT sum(rate) as rate_sum, count(*) as record_count FROM rating_table";
$query = $db->query($sql);
$row = $query->fetch(PDO::FETCH_ASSOC);
if ($row) {
$sum = $row['rate_sum'];
$count = $row['record_count'];
}

Mysqli count not working properly

$sql = "SELECT count(id),soft_name from table_name GROUP BY soft_name";
$d = mysqli_fetch_assoc(mysqli_query($db_name, $sql));
$c = array_shift($d);
The result is always 2, but the database contains more than 3000 items. What could be the problem?
Records are not pulled the way you are pulling for that you have to use similar to following code:
if($result = mysqli_query($db_name, $sql)){
while($d = mysqli_fetch_assoc($result){
echo $d['count'];
}
}
More reference here.

printing the value of a query using php

I'm trying to print out the value of a query but what appears on the screen is the query itself!
mysql_select_db($database_databasestudents, $databasestudents);
$result = mysql_query("Select name from country where id = '$s';",$databasestudents);
$r = mysql_fetch_array($result) ;
echo $r;
where $s is an integer
This is what I get on the screen:
SELECT name FROM country WHERE id='3'
3 is the value of $s
You need to run the query first
http://www.php.net/manual/en/mysqli.query.php
and then loop though the results
http://www.php.net/manual/en/mysqli-result.fetch-array.php
You need to run the query in order to get the result. Maybe this will get you started:
$pdo = new PDO('mysql:host=YOURHOST;port=YOURPORT;dbname=YOURDB', 'YOURUSER', 'YOURPASSWORD');
$sql = 'SELECT name FROM `country` WHERE id=?';
$stmt = $pdo->prepare($sql);
$stmt->execute(array($s));
while ($result = $stmt->fetchObject())
{
echo $result->name;
}
$r is an array, not a string. You need print_r($r); or var_dump($r);, not echo
You have created a string containing your query.
You must instantiate a database connection and then execute your query.

How To Store Array Variable In Select Query

I wanted to know how I can store an array variable into a select query, and if its okay to have the following inside a while loop. Thanks in advance.
For example:
$roww = array();
while ($roww = $resultt->fetch_assoc()) {
$uery = $dbconn->prepare("SELECT user_ids FROM t_friendship WHERE friend_ids = $roww['id'] AND status = 'Pending'");
$uery->execute();
$uery->store_result();
$rows = $uery->num_rows;}
Use curly braces around the array variable like this {$roww['id']}
Also you could construct a string of ids like ('1','2','3') inside the while loop and in one SELECT outside the while you can use IN of MySQL to select the results.
Another alternatives for curly brackets would be use of double quotes then a concatenate it.
$query = $dbconn->prepare("SELECT user_ids FROM t_friendship WHERE friend_ids = ".$roww['id']." AND status = 'Pending'");
// Now mysql result set is stored in php variable.
$result = mysql_query($querystring, $dbconn)
$array = array();
while($row = mysql_fetch_object($result))
{
$array[] = $row;
}
Use within single quotes
$uery = $dbconn->prepare("SELECT user_ids FROM t_friendship WHERE friend_ids = '".$roww['id']."' AND status = 'Pending'");
$uery->execute();
first check out id value just echo $roww['id']. If you getting that value means use above the code

Issue with mysql query when changing value to a variable

$results = mysql_query("select * from doctorlist where assignednumber = '1231231234' ");
I need to change the number 1231231234 to a variable. If I change it to the code below it does not work. I have displayed the variable on the page so I know it is set.
$results = mysql_query("select * from doctorlist where assignednumber = '$phoneNumber' ");
Could someone please help. I know it is a small issue, but have been unable to fix it.
Perhaps split it like this
$sql_query = "select * from doctorlist where assignednumber='$phoneNumber'";
$results = mysql_query($sql_query);
or
$sql_query = "select * from doctorlist where assignednumber='".$phoneNumber."' ";
$results = mysql_query($sql_query);
First check your variable type with var_dump($phoneNumber) than do the following:
$results = mysql_query("select * from doctorlist where assignednumber = '".$phoneNumber."' ");
to improve readability and last if you expect an Integer cast your variable like:
(int)$phoneNumber
or if string do
mysql_real_escape_string($phoneNumber)
Try using the variable inside the query like this:
'{$phoneNumber}'

Categories