shan2batman Posted September 16, 2018 Share Posted September 16, 2018 hi, i'm trying to fetch results for one of my projects but it isn't fetching results can anyone help pl. here is the code: if($friend_username !=$_SESSION['uname']){ $mark1="select os_id,parent,r_seen,rread,sread,receiver,sender from private_message where (receiver=:sess or sender=:sess) and has_replies='1' and rread='1' and sread='0'"; $stmt_m1=$conn->prepare($mark1); $stmt_m1->bindValue(":sess", $_SESSION['uname']); //$stmt_m->bindValue(":f_uname", $f_uname); $stmt_m1->execute(); $column1=$stmt_m1->fetchAll(); //this db query retrieves the pm_id var_dump($column1); $pmid=$column1['os_id']; if ($column1['r_seen']=='0'&&$column1['parent']==$_SESSION['uname']) { print_r($column1); echo '<a href="private_message.php?u='.$friend_username.'&r_seen=1&pmid='.$pmid.'">' . '<img class="friendpics" src="'.$friend_pic.'" height="20" width="20" alt="'.$friend_username.'" title="'.$friend_username.'">'.$fname.' '.$lname.'<br>(' .$friend_username.')</a><br><hr>'; } elseif ($column1['r_seen']=='1'&&$column1['parent']==$_SESSION['uname']) { echo '<span class="label label-pill label-success" style="color: white; float:left;">New</span><div style="background: #cce0cc; height:125px; width:100%;"><a style="text-align:center;" href="private_message.php?u='.$friend_username.'&r_seen=1&pmid='.$pmid.'">' . '<img class="friendpics" src="'.$friend_pic.'" height="20" width="20" alt="'.$friend_username.'" title="'.$friend_username.'">'.$fname.' '.$lname.'<br>(' .$friend_username.')</a></div><br><hr>'; echo 'old'; }elseif ($column1['r_seen']=='0'&&$column1['parent']!=$_SESSION['uname']) { echo '<span class="label label-pill label-success" style="color: white; float:left;">New</span><div style="background: #cce0cc; height:125px; width:100%;"><a style="text-align:center;" href="private_message.php?u='.$friend_username.'&r_seen=1&pmid='.$pmid.'">' . '<img class="friendpics" src="'.$friend_pic.'" height="20" width="20" alt="'.$friend_username.'" title="'.$friend_username.'">'.$fname.' '.$lname.'<br>(' .$friend_username.')</a></div><br><hr>'; echo'new'; }elseif ($column1['r_seen']=='1'&&$column1['parent']!=$_SESSION['uname']) { echo '<a href="private_message.php?u='.$friend_username.'&r_seen=1&pmid='.$pmid.'">' . '<img class="friendpics" src="'.$friend_pic.'" height="20" width="20" alt="'.$friend_username.'" title="'.$friend_username.'">'.$fname.' '.$lname.'<br>(' .$friend_username.')</a><br><hr>'; echo'new11'; } else { print_r($f_uname); echo '<a href="private_message.php?u='.$friend_username.'&r_seen=1&pmid='.$pmid.'">' . '<img class="friendpics" src="'.$friend_pic.'" height="20" width="20" alt="'.$friend_username.'" title="'.$friend_username.'">'.$fname.' '.$lname.'<br>(' .$friend_username.')</a><br><hr>'; } } } } echo '</div></div>'; } Quote Link to comment Share on other sites More sharing options...
benanamen Posted September 16, 2018 Share Posted September 16, 2018 (edited) For starters you have several mismatched curly braces. If you used a proper IDE you would spot basic errors like this right away. I would also suggest you learn one of the standard code formatting styles and stick to it. You also have a substantial amount of duplicate code. DRY - Don't Repeat Yourself. Edited September 16, 2018 by benanamen Quote Link to comment Share on other sites More sharing options...
shan2batman Posted September 16, 2018 Author Share Posted September 16, 2018 thats because i have copied the code from the middle of the doc Quote Link to comment Share on other sites More sharing options...
Barand Posted September 16, 2018 Share Posted September 16, 2018 2 hours ago, shan2batman said: where (receiver=:sess or sender=:sess) Try :sess1 and :sess2 and bind the session value to both. Quote Link to comment Share on other sites More sharing options...
shan2batman Posted September 16, 2018 Author Share Posted September 16, 2018 @barand still no luck Quote Link to comment Share on other sites More sharing options...
requinix Posted September 16, 2018 Share Posted September 16, 2018 Even if that didn't fix it, you still need to do it. fetchAll() will fetch all of the results. If you just want one row then you need to use a different method. Quote Link to comment Share on other sites More sharing options...
shan2batman Posted September 16, 2018 Author Share Posted September 16, 2018 @requinix i tried fetch() it returns with bool (false) as response. Quote Link to comment Share on other sites More sharing options...
requinix Posted September 16, 2018 Share Posted September 16, 2018 Sounds like your query didn't return anything. Does the uname have the value you think it does? Have you tried running the query manually? Quote Link to comment Share on other sites More sharing options...
shan2batman Posted September 16, 2018 Author Share Posted September 16, 2018 the session is working well in other areas but not here @requinix Quote Link to comment Share on other sites More sharing options...
requinix Posted September 16, 2018 Share Posted September 16, 2018 Not technically what I asked but okay. How about the other question? Quote Link to comment Share on other sites More sharing options...
Barand Posted September 16, 2018 Share Posted September 16, 2018 Have you called session_start() at the top of the script? try a echo '<pre>', print_r($_SESSION, true), '</pre>' ; to check what's there. Quote Link to comment Share on other sites More sharing options...
shan2batman Posted September 16, 2018 Author Share Posted September 16, 2018 (edited) @requinix the query is copied from another place where it is run plus i would like to know how to run queries manually. Edited September 16, 2018 by shan2batman Quote Link to comment Share on other sites More sharing options...
shan2batman Posted September 16, 2018 Author Share Posted September 16, 2018 @Barand the session is set and working well. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 16, 2018 Share Posted September 16, 2018 (edited) Have you set the PDO connection option to throw exceptions? I don't see any error checking in the code. BTW, if you don't like the idea of pecifying the parameter :sess twice, you could SELECT ... WHERE :sess IN (sender, receiver) AND ... Edited September 16, 2018 by Barand Quote Link to comment Share on other sites More sharing options...
requinix Posted September 16, 2018 Share Posted September 16, 2018 18 minutes ago, shan2batman said: @requinix the query is copied from another place where it is run plus i would like to know how to run queries manually. You've never used MySQL from the command line? How about phpMyAdmin? How are you writing these queries if you don't have a way to test that they work? 1 Quote Link to comment Share on other sites More sharing options...
shan2batman Posted September 16, 2018 Author Share Posted September 16, 2018 @requinix thank you it works now i tried the code in phpmyadmin Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.