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
https://forums.phpfreaks.com/topic/162496-showing-output/
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
https://forums.phpfreaks.com/topic/162496-showing-output/#findComment-857668
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
https://forums.phpfreaks.com/topic/162496-showing-output/#findComment-857731
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
https://forums.phpfreaks.com/topic/162496-showing-output/#findComment-857767
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.