i am running this PDO Query in PHP:
$stmt = $pdo_conn->prepare("SELECT * from contacts where email = :email ");
$stmt->execute(array(':email' => $from ));
$contact = $stmt->fetchAll(PDO::FETCH_ASSOC);
if(count($contact) > 0) {
echo $contact["email"];
}
but its not echoing the email column from the contacts table
i know there is defiantly a value there as if i do echo 'yes'; inside the if statement it shows
what have i done wrong here?
var_dump($contact); shows
array(1) { [0]=> array(22) { ["sequence"]=> string(3) "266" ["newsletter"]=> string(3) "yes" ["company_sequence"]=> string(3) "278" ["title"]=> string(2) "Mr" ["forename"]=> string(7) "Forename" ["surname"]=> string(4) "Surname" ["email"]=> string(22) "user#domain.com" ["password"]=> string(32) "**********" ["phone"]=> string(0) "" ["mobile"]=> string(11) "00000000000" ["notes"]=> string(0) "" ["contactstatus"]=> string(0) "" ["logintries"]=> string(1) "0" ["dob"]=> string(10) "0000-00-00" ["receive_allticketupdates"]=> string(3) "yes" ["receive_accountsemails"]=> string(3) "yes" ["can_edit_contacts"]=> string(3) "yes" ["view_all_tickets"]=> string(3) "yes" ["receive_orderemails"]=> string(0) "" ["can_login_online"]=> string(3) "yes" ["last_satisfaction_survey_received"]=> string(10) "0000-00-00" ["receive_domainemails"]=> string(0) "" } }
Because you're using fetchAll(), you're receiving a 2-dimensional array of results even when you expect just one.
To get your single result from this, you can access it via $contact[0] instead:
echo $contact[0]['email'];
Alternatively, if you want/expect a single row, you can use fetch() instead of fetchAll():
$contact = $stmt->fetch(PDO::FETCH_ASSOC);
if(count($contact) > 0) {
echo $contact["email"];
}
It seems that $contact will contain an array of rows. So you'll need to access the email field of a particular row. Something like this:
echo $contact[0]['email'];
Or use a loop:
if (!empty($contact)) {
foreach ($contact as $thisContact) {
echo $thisContact['email'];
}
}
Or, using fetchAssoc instead of fetchAll:
while ($contact = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $contact['email'];
}
fetchAll fetches all rows in an array, so the result a multi-dimensional array.
You are probably looking for echo $contact[0]["email"];.
Related
So I have this code:
<?php
$main = new Main;
$conn = $main->db_connect();
$module = $conn->prepare("SELECT * FROM modules");
$modulelist = $module->execute();
$modulelist = mysqli_fetch_array($module->get_result());
var_dump($modulelist);
And it returns this instead of the normal values in the table:
array(12) {
[0]=> int(2) ["ID"]=> int(2) 1=> string(7)
"anderes" ["Naam"]=> string(7) "anderes" [2]=> string(4)
"0.00" ["Beschrijving"]=> string(4) "0.00" [3]=>
string(4) "0.00" ["Prijs"]=> string(4) "0.00" [4]=>
string(0) "" ["Prijsweergave"]=> string(0) "" [5]=>
int(2) ["Termijn"]=> int(2) }
This is the table:
I assume your issue is that the way you have written your code, you only ever get One result printed from a multiple result query.
The $module->get_result() can only be called once as it gets the whole resultset the first time, so the second call will return nothing.
$module = $conn->prepare("SELECT * FROM modules");
$modulelist = $module->execute();
$results = $module->get_result();
while ( $row = $results->fetch_assoc() ){
var_dump($row);
}
I have a little question.
I have few stored values in session Array. These values are ID's of products.
After that i want to display products from my database, but this is not working propertly for me.
Can someone help me a little? :) (I am still learning :) )
<?php
include 'includes/dbconnect.php';
$orderid = $_SESSION['order'];
foreach ($orderid as $value) {
$sql="SELECT * FROM product WHERE productID LIKE '%$value%'";
$result=$conn->query($sql);
while($row=$result->fetch_assoc()){
echo '<tr>';
echo '<td>'.$row["tag"].'</td>';
echo '<td>'.$row["price"].',- Kč</td>';
echo '<td><img src="images/'.$row["tag"].'.jpg" width=70"></td>';
echo '<td>1</td>' ;
echo '<td>X</td>';
}
}
?>
var_dump($orderid); shows:
array(1) {
["order"]=> array(10) {
[0]=> string(2) "44"
[1]=> string(2) "46"
[2]=> string(2) "44"
[3]=> string(2) "54"
[4]=> string(1) "1"
[5]=> string(2) "44"
[6]=> string(1) "1"
[7]=> string(2) "44"
[8]=> string(2) "47"
[9]=> string(2) "74"
}
}
Just for the purposes of SO, I'll make my comment as an answer:
In the $sql query instead of using LIKE '%$value%'"; use this: LIKE '". $value ."'";
This ensures that we actually get the value of the variable.
I have this array of objects that is looking like this:
Done: array(2) {
[0]=>
object(stdClass)#107 (19) {
["id"]=>
string(1) "6"
["apartament"]=>
string(1) "1"
["nume"]=>
string(5) "SURCA"
["persoane"]=>
string(1) "2"
["mp"]=>
string(4) "37.7"
["retim"]=>
string(5) "19.19"
["incalzire"]=>
string(5) "74.74"
["apacaldamc"]=>
string(1) "3"
["apacaldalei"]=>
string(5) "47.48"
["apacaldadif"]=>
string(4) "3.72"
["aparecemc"]=>
string(1) "8"
["aparecelei"]=>
string(5) "54.73"
["aparecedif"]=>
NULL
["curent"]=>
string(3) "345"
["gaz"]=>
string(3) "2.5"
["administrator"]=>
string(5) "17.01"
["cheltuieliadministrare"]=>
string(4) "2.05"
["acoperis"]=>
string(4) "62.5"
["timp"]=>
string(19) "2017-04-28 10:04:28"
}
[1]=>
object(stdClass)#108 (19) {
["id"]=>
string(1) "7"
["apartament"]=>
string(1) "2"
["nume"]=>
string(8) "ENACHIUC"
["persoane"]=>
string(1) "1"
["mp"]=>
string(5) "37.07"
["retim"]=>
string(3) "9.6"
["incalzire"]=>
string(4) "73.5"
["apacaldamc"]=>
string(1) "3"
["apacaldalei"]=>
string(5) "15.83"
["apacaldadif"]=>
string(4) "3.72"
["aparecemc"]=>
string(1) "2"
["aparecelei"]=>
string(5) "13.68"
["aparecedif"]=>
string(1) "0"
["curent"]=>
string(4) "0.66"
["gaz"]=>
string(4) "1.25"
["administrator"]=>
string(5) "17.01"
["cheltuieliadministrare"]=>
string(4) "2.05"
["acoperis"]=>
string(4) "62.5"
["timp"]=>
string(19) "2017-04-28 10:11:25"
}
}
I get this from a standard query:
$sql ="SELECT * FROM consum WHERE timp BETWEEN '".$fromDate."' AND '".$toDate."'";
$query = $this->db->query($sql);
$databaseOject = $query->result();
I was trying to do something like this:
if ($databaseOject->num_rows > 0) {
echo "<table><tr><th>id</th><th>apartament</th><th>nume</th><th>apartament</th><th>persoane</th><th>mp</th><th>retim</th><th>incalzire</th><th>apacaldamc</th><th>apacaldalei</th><th>apacaldadif</th><th>aparecemc</th><th>aparecelei</th><th>aparecedif</th><th>curent</th><th>gaz</th><th>administrator</th><th>cheltuieliadministrare</th><th>acoperis</th><th>timp</th></tr>";
// output data of each row
while($row = $databaseOject->fetch_assoc()) {
echo "<tr><td>".$row["id"]."</td><td>".$row["apartament"]."</td><td>".$row["nume"]."</td><td>".$row["persoane"]."</td><td>".$row["mp"]."</td><td>".$row["retim"]."</td><td>".$row["incalzire"]."</td><td>".$row["apacaldamc"]."</td><td>".$row["apacaldalei"]."</td><td>".$row["apacaldadif"]."</td><td>".$row["aparecemc"]."</td><td>".$row["aparecelei"]."</td><td>".$row["aparecedif"]."</td><td>".$row["curent"]."</td><td>".$row["gaz"]."</td><td>".$row["administrator"]."</td><td>".$row["cheltuieliadministrare"]."</td><td>".$row["acoperis"]."</td><td>".$row["timp"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
But I get:
Trying to get property of non-object
Indeed $databaseOject is not a object, it's a array of object, probably that's why I get that error, but how I can make the table in my case - array of objects ? Thank you so much for your time!
The result of $query->result() is an array of StdObjects, you can loop through them using a simple foreach loop.
To access their properties, as they are stdObjects and not array, you need to replace $row['id'] with $row->id, or replace result() with result_array()
I strongly advise you to read the doc on the subject here.
Very simple, you should understand that result helper function for num_rows() should be implemented before the query result() like below:
$sql ="SELECT * FROM consum WHERE timp BETWEEN '".$fromDate."' AND '".$toDate."'";
$query = $this->db->query($sql);
if($query->num_rows() > 0)
{
$databaseObject = $query->result();
}
So you can check either the result was existed or not before fetch it.
For your references:
https://www.codeigniter.com/userguide3/database/results.html
Hope it helps!
I've been playing around with this one for a few days, I'm not a professional coder but I'm trying to put a nice basic system in place to manage checkin/out at a local volunteer rescue organization.
Basically I'm trying to get all members member with the ISLOGGEDIN value set as 1
$loggedin = mysql_query("SELECT * FROM members WHERE ISLOGGEDIN=1");
$loggedinarray=mysql_fetch_array($loggedin);
And present their information (name, etc) into a table:
foreach($loggedinarray as $key=>$value) {
echo "
<tr>
<td>".$value[firstname]." ".$value[lastname]."</td>
<td>".$value[timeloggedin]."</td>
</tr>
";
}
However the results I'm getting are quite random and not what I'm looking for, at all!
var_dump($loggedinarray) output:
array(12) { [0]=> string(5) "5395" ["SES_ID"]=> string(5) "5395" [1]=> string(7) "Anthony" ["FIRST"]=> string(7) "Anthony" [2]=> string(8) "LastName" ["LAST"]=> string(8) "Willison" [3]=> string(1) "1" ["ISLOGGEDIN"]=> string(1) "1" [4]=> string(1) "0" ["TRAINER"]=> string(1) "0" [5]=> string(1) "1" ["OFFICER"]=> string(1) "1" }
Any help would be greatly appreciated!
You have to use while; it was random, because it fetched only one row from table.
$loggedin = mysql_query("SELECT * FROM members WHERE ISLOGGEDIN = 1");
if (mysql_num_rows($loggedin) > 0) {
echo "<table>";
while ($row = mysql_fetch_array($loggedin)) {
echo "
<tr>
<td>".$row['FIRST']." ".$row['LAST']."</td>
<td>".$row['timeloggedin']."</td>
</tr>
";
}
echo "</table>";
}
This code will output every row from table, where ISLOGGEDIN = 1.
PS: about data $row['timeloggedin'], you dont have it in your query (as seen in var_dump), so it will be an empty string.
I am trying to create a query from a form that passes ID and Details fields, the post array looks like this:
array(2) {
["ID"]=> array(9)
{
[0]=> string(3) "10a"
[1]=> string(3) "10b"
[2]=> string(3) "10c"
[3]=> string(3) "10d"
[4]=> string(3) "10e"
[5]=> string(3) "10f"
}
["Details"]=> array(9)
{
[0]=> string(19) "This is textfield 1"
[1]=> string(17) "This is textfield 2"
[2]=> string(0) ""
[3]=> string(0) ""
[4]=> string(0) ""
[5]=> string(0) ""
}
}
The ID is hardcoded into the page and always passed, the details can be filled out by the user as needed.
if ($_POST['OthProb']['Details'] != '') {
// Catch issue text and assign to variable
foreach ( $_POST['OthProb']['Details'] as $key => $value) {
$id = $_POST['OthProb']['ID']->$key;
$dets = $value;
// Focus on fields with values and join the array values together in a query string
if ($dets != ''){
$string = "INSERT into $table (AccountID, OtherID, ID, Details) VALUES ($_POST[AccountID], $_POST[OtherID], '".$id.", '".$dets."')";
$sql = $DBH->prepare($string);
// $sql->execute();
// var_dump ($id);
}
}
}
creates the following
"INSERT into tbl_problems (AccountID, OtherID, ID, Details) VALUES (80, 80, '10f, 'This is Title issue')"
The 'Details' are outputting correctly, but its cycling all the way through the ID's to the last one, is that because I'm nesting a foreach? What would be the best way to structure the loop?
Try to replace this line:
$id = $_POST['OthProb']['ID']->$key;
by
$id = $_POST['OthProb']['ID'][$key];