-1

I'm new at php and I followed this youtube : https://www.youtube.com/watch?v=LQ8DpIbD9Ps in order to build the conversations.php script. I made a littel adjustment's from mysql to mysqli. I don't get the same result as in minute 10:06 of the video. It doesn't display me the conversations list. I can't find where my error is.

   <?php
        if(isset($_GET['hash']) && !empty($_GET['hash'])){
          echo "Show messages!";  
        }  
        else {
            echo "<b>Select Conversation:</b>";
            $sql_k = "select 'hash', 'user_one','user_two' from message_group 
            where user_one='$my_id' OR user_two='$my_id'";
            $get_con = mysqli_query($db,$sql_k);
            while ($run_con = mysqli_fetch_array($get_con)){
                 $hash = $run_con['hash'];
                 $user_one = $run_con['user_one'];
                 $user_two = $run_con['user_two'];
                 if ($user_one==$my_id) 
                 {
                     $select_id = $user_two;
                 } else {
                     $select_id = $user_one;
                 } 
                      $sqli = "Select 'username' from users where 
                      id='$select_id'";
                      $user_get = mysqli_query($db,$sqli);
                      $run_user = mysqli_fetch_array($user_get);
                      echo $run_user;
                      $select_username = $run_user['username'];
                      echo "<p><a href ='conversations.php?hash=$hash'>$select_username</a></p>";
                 }
            }   
        ?>    
Jordan1200
  • 478
  • 1
  • 5
  • 20
  • Use `mysqli_fetch_object` instead of `mysqli_fetch_array` – Hamza Rashid Apr 08 '17 at 19:53
  • @HamzaRashid he's accessing the result as an array. Why would he want to fetch it as an object? – miken32 Apr 08 '17 at 20:03
  • where did I quate my table names? message_group or users? – Jordan1200 Apr 08 '17 at 20:13
  • I've added explanation as well as example for understanding. – Hamza Rashid Apr 08 '17 at 20:19
  • `$sql_k = "select 'hash', 'user_one','user_two'` - You're still doing the same error you did in another of your questions where you quickly ignored and didn't bother replying to that comment or any other comment I posted for past questions. If you want to make your code run; simple; check for errors, you have many. Btw; I ended up deleting that comment about the `'hash'` column; I had my reasons. Maybe you have something against me; I don't know. – Funk Forty Niner Apr 08 '17 at 20:22
  • @Fred-il, I have taken your words to my attention. I'm just new at php and asking questions about this. Of course I have nothing against you. Thank you. – Jordan1200 Apr 08 '17 at 20:26

1 Answers1

0

To answer your question:

  1. Use mysqli_fetch_assoc instead of mysqli_fetch_array. It is recommended syntax to avoid getting extra data in query result. Improves efficiency in case of heavy weight queries. Moreover it avoids confusion for early learners.
  2. Use back ticks on column names instead of single quotes.

    SELECT `hash`, `user_one`, `user_two` FROM message_group WHERE user_one='$my_id' OR user_two='$my_id'

  3. When fetching data in case of mysqli_fetch_assoc use single quotes like you are already doing i.e. $run_con['hash']


Usage of `mysql_fetch_*

Using mysqli_fetch_row, data is available in indexed array.

$hash = $run_con[0];
$user_one = $run_con[1];
$user_two = $run_con[2];

Using mysqli_fetch_assoc, data is available in associated array.

$hash = $run_con['hash'];
$user_one = $run_con['user_one'];
$user_two = $run_con['user_two'];

Using mysqli_fetch_array, data is available in indexed as well as associative array. Following lines of code will result in same value storing in $hash.

$hash = $run_con[0];
$hash = $run_con['hash'];

Using mysqli_fetch_object, data is available as object.

$hash = $run_con->hash;
$user_one = $run_con->user_one;
$user_two = $run_con->user_two;

Side note: mysqli_fetch_arary is not recommended as it brings back extra data, which is same data in indexed array as well associated array format (which is usually not required.)

Hamza Rashid
  • 1,329
  • 15
  • 22
  • 2
    If you want the result as an associative array you should use mysqli_fetch_assoc, not mysqli_fetch_object – Andrew Larsen Apr 08 '17 at 19:59
  • And another thing, this is probably not were the problem is, because as default mysqli_fetch_array will return the results both indexed and with the column name as key, so both indexed and associative. So changing to mysqli_fetch_assoc would not change anything, other then a smaller array without the indexed (0, 1....) data. You should try to var_dump($run_user); and see what result you get from that query. Does it return the expected result? – Andrew Larsen Apr 08 '17 at 20:04
  • Well, you right. I've changed array to object and the list of conversations still doens't display in page. – Jordan1200 Apr 08 '17 at 20:10