Can someone spot what's wrong with this query?

Discussion in 'PHP' started by Mitchell, Jun 21, 2010.

  1. #1
    I am using a prepared statement to get information from a database. It doesn't echo anything. Thanks.

    
    $query = 'SELECT * FROM mammals WHERE postid = ?';
        $stmt = $db->stmt_init();
        if ($stmt->prepare($query)) {
            $stmt->bind_result('d', $postid);
            $stmt->execute();
            $data = $stmt->fetch_assoc();
            echo $data['postid'];
            }
    
    PHP:
     
    Mitchell, Jun 21, 2010 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    Do you have any records with a postid of "?"? :)
     
    digitalpoint, Jun 21, 2010 IP
  3. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for responding.

    Yes. Just above this statement I posted info and it worked. Now I am trying to display the information that was just entered into the form and database.

    
    $query = "insert into mammals05 (postid, title, price, discription, email) values (?, ?, ?, ?, ?)";
    	$stmt = $db->stmt_init();
    	if ($stmt->prepare($query)) {
    		$stmt->bind_param('dsdss', $postid, $title, $price, $discription, $email);
    		$stmt->execute();
    		}
    
    $query = 'SELECT * FROM mammals WHERE postid = ?';
        $stmt = $db->stmt_init();
        if ($stmt->prepare($query)) {
            $stmt->bind_result('d', $postid);
            $stmt->execute();
            $data = $stmt->fetch_assoc();
            echo $data['postid'];
            }
    
    PHP:
     
    Mitchell, Jun 21, 2010 IP
  4. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #4
    If postid is an integer (as defined within your database), it's going to be saved as a 0 when you insert it. If it's a text field, you need to wrap the values within quotes (both for inserting and querying).
     
    digitalpoint, Jun 21, 2010 IP
  5. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    postid is a 10 digit time stamp number. Perhaps I should of called it something else.

    create table mammals05
    (
    postid int unsigned not null primary key,
    title char(50) not null,
    price int unsigned not null,
    discription char(20) not null,
    email char(50) not null
    );
     
    Mitchell, Jun 21, 2010 IP
  6. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #6
    Well... ? is not an integer... there is no way you can store an integer value of "?" in your database, nor can you query for an integer of "?", because there is no such thing.

    You might as well be asking the database server to give you a random NUMBER between h and e. :)
     
    digitalpoint, Jun 21, 2010 IP
  7. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    When I bind parameters the (?) is replaced with (d) which stands for Double (floating point number) and will represent $postid and $price. (s) stands for string, and will represent $title, $description and $email. I am new at this. Am I not understanding you correctly?

    The 10 digit postid number inputs into the database correctly. I removed auto-increment from the database table, maybe that's why.

    $stmt->bind_param('dsdss', $postid, $title, $price, $discription, $email);
     
    Mitchell, Jun 21, 2010 IP