Jump to content

Showing Output


N-Bomb(Nerd)

Recommended Posts

Hello, I have the following code, however I'm not quite sure as to how I can get the values to display properly.

	if ($db = $this->Connection->prepare("SELECT * FROM Users WHERE Id=?")) {
		$db->bind_param("s", $this->ID);
		$db->execute();
		$db->store_result();

		if ($db->num_rows > 0) {
			// not sure what to do here to make it so I can use $row['Username'] and have it output the actual username
            // of course there's other values I want to use from the row.. Username was just example
		}
	}

Link to comment
Share on other sites

Your just add another ? and another bind

if ($db = $this->Connection->prepare("SELECT * FROM Users WHERE Id=? AND Group=?")) {
$db->bind_param("s", $this->ID);
$db->bind_param("s", $this->Group);

 

or you can use an array

$db->prepare($conn, 'SELECT * FROM Users WHERE Id=? AND Group=?');
$result = $db->execute($db, array($this->ID, $this->Group));

but it really depends on what your using

 

Link to comment
Share on other sites

I'm really sorry to hi-jack this topic with this discussion.

 

MadTechie - with sprintf you can at least do argument swapping. There, you can't.

 

Example -

<?php
$e = sprintf('this is %1$s random text and here are some %1$s random numbers - %2$d, %3$d, %2$d', 'some', 4, 5);

 

Right? I use sprintf a lot as well. Even with echo I don't like interpolating variables and concatenating variables is too messy.

Link to comment
Share on other sites

depends on the class, but at a guess your still need fetch them

ie

while ($row = $db->fetch_row())
{
   var_dump($row[0]);
}

 

I've just tried that and I'm getting this as a result:

Fatal error: Call to undefined method mysqli_stmt::fetch_row() in db.php on line 16

 

whats the Connection function ?

 

I don't know if this is the proper way to handle things, but I just put this in the construct of my class to attempt to make things a bit neater:

$this->Connection = new mysqli('host', 'username', 'password', 'db');

 

How does the bind_param methods work in prepares? Does it just replace the first ? with it? What if I have 2? Do I have to use bind_param sequentially?

 

I do the following:

$E->bind_param("sss", $this->Value1, $this->Value2, $this->Value3);

 

And as you already know that handles it sequentially ( left to right )

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.