N-Bomb(Nerd) Posted June 17, 2009 Share Posted June 17, 2009 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 } } Quote Link to comment https://forums.phpfreaks.com/topic/162496-showing-output/ Share on other sites More sharing options...
MadTechie Posted June 17, 2009 Share Posted June 17, 2009 depends on the class, but at a guess your still need fetch them ie while ($row = $db->fetch_row()) { var_dump($row[0]); } whats the Connection function ? Quote Link to comment https://forums.phpfreaks.com/topic/162496-showing-output/#findComment-857658 Share on other sites More sharing options...
Ken2k7 Posted June 17, 2009 Share Posted June 17, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/162496-showing-output/#findComment-857663 Share on other sites More sharing options...
MadTechie Posted June 17, 2009 Share Posted June 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/162496-showing-output/#findComment-857668 Share on other sites More sharing options...
Ken2k7 Posted June 17, 2009 Share Posted June 17, 2009 But it has to be sequential right? Hmm... can't people think of a more intuitive way to do something like that so I don't have to use sequential binds. I mean an associative array or something would be nice. Quote Link to comment https://forums.phpfreaks.com/topic/162496-showing-output/#findComment-857701 Share on other sites More sharing options...
MadTechie Posted June 17, 2009 Share Posted June 17, 2009 Meah.. you get used to it, infect I make less mistakes as I'm used to sprintf() ing my queries Quote Link to comment https://forums.phpfreaks.com/topic/162496-showing-output/#findComment-857722 Share on other sites More sharing options...
Ken2k7 Posted June 17, 2009 Share Posted June 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/162496-showing-output/#findComment-857731 Share on other sites More sharing options...
N-Bomb(Nerd) Posted June 17, 2009 Author Share Posted June 17, 2009 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/162496-showing-output/#findComment-857767 Share on other sites More sharing options...
MadTechie Posted June 17, 2009 Share Posted June 17, 2009 Ahh MySQLi try this $db->execute(); $db->store_result(); $db->bind_result($Username); //bind result $db->fetch(); //fetch row echo $Username; //display Quote Link to comment https://forums.phpfreaks.com/topic/162496-showing-output/#findComment-857824 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.