3 Replies - 674 Views - Last Post: 29 August 2015 - 02:44 AM Rate Topic: -----

#1 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 615
  • Joined: 30-April 15

Issues with SELECT query

Posted 28 August 2015 - 11:59 PM

I am having a difficult time getting this SELECT query to work properly.

What I am trying to do is allowing a user to click a button that brings up a message form that they can send a message. The only thing I am sending via AJAX is the message. Then in my php file I am getting the user's id via their session. My query looks like this...

 
$userid = ( isset( $_SESSION['user'] ) ? $_SESSION['user'] : "" );
$help_message = $_POST['help_message'];

$con = mysqli_connect("localhost", "", "", "");
 $stmt = $con->prepare("SELECT id, firstname, lastname, email, phone_number, username FROM users WHERE id=?");
    if ( !$stmt || $con->error ) {
        // Check Errors for prepare
         die('Admin Help Email SELECT prepare() failed: ' . htmlspecialchars($con->error));
    }
    if(!$stmt->bind_param('i', $userid)) {
        // Check errors for binding parameters
        die('Admin Help Email SELECT bind_param() failed: ' . htmlspecialchars($stmt->error));
    }
    if(!$stmt->execute()) {
        die('Admin Help Email SELECT execute() failed: ' . htmlspecialchars($stmt->error));
    }

    while($row = mysqli_fetch_assoc($stmt)) { 
       $firstname = $row['firstname'];
       $lastname = $row['lastname'];
       $email = $row['email'];
       $phone_number = $row['phone_number'];
       $username = $row['username'];
    }


I have echoed out $userid and I am getting the correct id, so that is working.

I have even tried doing this
$row = $stmt->fetch();
print $row;



I only get the userid, nothing else.

I am getting this error -- Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, object given

I researched this error and see that is comes up when something is wrong with the query. None of my other errors are showing besides this (except for undefined variable errors because of the other error I mentioned).

What would be causing this? Does anything in my prepared statement look wrong? All of the table columns are correct. I checked 6 times.

I appreciate any help.

Is This A Good Question/Topic? 0
  • +

Replies To: Issues with SELECT query

#2 rodiongork   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 61
  • Joined: 28-August 15

Re: Issues with SELECT query

Posted 29 August 2015 - 12:20 AM

I may be mistaken but I think error comes from the fact you could not use mysqli_stmt with fetch_assoc this way.

http://stackoverflow...array-in-mysqli
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Issues with SELECT query

Posted 29 August 2015 - 02:31 AM

The main problem is that you are switching between the procedural and OOP approaches to mysqli. You need to stick with one or the other. See the examples from the docs. And here.

So for OOP you shouldn't be using mysqli_connect or mysqli_fetch_assoc.

This post has been edited by andrewsw: 29 August 2015 - 02:33 AM

Was This Post Helpful? 1
  • +
  • -

#4 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Issues with SELECT query

Posted 29 August 2015 - 02:44 AM

Slightly off topic..

It doesn't help when, for example, the first example for bind_param uses mysqli_connect_errno() when there is mysqli::$connect_errno.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1