sonal Posted December 29, 2011 Share Posted December 29, 2011 I am trying to update my scripts using prepared statements. (Till now I was exercising regular queries.) My updated scripts work very well on local host, but gives hard times on live site. (I have created a live site just to know and practice how things work.) The following code prints only title of the article, while article content is not displayed. I tried to check different print statements, checking column names number of times and checking syntex of binding result set. But still not come up with the reason. (I have changed table and column names here. The live site is www.myreadinglog.net. This script is first two links on left bar.) if((isset($_GET['gg'])) && (is_numeric($_GET['gg']))) { //from a.php $a = (int)$_GET['gg']; //Typecast gg. } else { //No valid Id, kill the script. echo '<p> This page has been accessed in erro.</p>'; include('includes/newf.html'); exit(); } //Make a prepared statement query to select content for the given article ID: $q = "SELECT qt, qon FROM comma WHERE a = ? LIMIT 1"; //Prepare the statment, assigning result to a variable: if($stmt = mysqli_prepare($dbc, $q)) { //bind variable to query's placeholder: mysqli_stmt_bind_param($stmt,'i', $a); //Execute the statement: mysqli_stmt_execute($stmt); //Bind result variables: mysqli_stmt_bind_result($stmt, $title, $content); //store result: mysqli_stmt_store_result($stmt); if(mysqli_stmt_num_rows($stmt) > 0) { //if statement execution returns any row, then //fetch value and display: while(mysqli_stmt_fetch($stmt)) { echo "<big><b>$title</b></big>\n"; //This value is printed correctly. //echo nl2br($content); printf("%s\n", nl2br($content)); //But this value is not being printed. } } else { echo "There is no content here."; } //close statment: mysqli_stmt_close($stmt); } //End of IF($stmt). Link to comment Share on other sites More sharing options...
Jonathon Posted December 29, 2011 Share Posted December 29, 2011 (edited) Doesn't look like you declare a $stmnt variable just glancing at it.Ignore my comment I missed your if statemenr. Edited December 29, 2011 by Jonathon 1 Link to comment Share on other sites More sharing options...
Antonio Conte Posted January 2, 2012 Share Posted January 2, 2012 Use mysqli_stmt_bind_result. mysqli_stmt_execute($stmt); mysqli_stmt_bind_result($stmt, $title, $content); while (mysqli_stmt_fetch($stmt)) { printf ("%s (%s)\n", $ti, $content); } 1 Link to comment Share on other sites More sharing options...
sonal Posted January 2, 2012 Author Share Posted January 2, 2012 Thanks for reply. I am using mysqli_stmt_bind_result as already shown in my code. And it displays $title value, but not displaying $content value. May be this is some kind of display problem. I am still getting mad about this.. May be I need to use something different than "%s " in printf function. But why then echo is not working as well? How can I print error from prepared statements in this condition? Link to comment Share on other sites More sharing options...
Antonio Conte Posted January 3, 2012 Share Posted January 3, 2012 Sorry about that. Had to catch a train, and I must have overlooked that you used it. - A suggestion. Have you tried to run the query using something like phpmyadmin? - echo mysqli_stmt_errno($stmt) will give you any error messages or the string "zero" if none. Looked at your site. I can see both title and content on the links described. Did you solve this? 1 Link to comment Share on other sites More sharing options...
sonal Posted January 3, 2012 Author Share Posted January 3, 2012 Hi Antonio, thanks again for suggestions. (I have not yet solved the problem. The content on the site is displayed using old, simple query script.) mysqli_stmt_errono and mysqli_stmt_error are not showing any errors, because query is running properly. It shows title atleast. I also run the query multiple times in phpmyadmin through my hosting plan. It shows result correctly there. That's why it has been difficult to know the cause of problem. Anyway, thanks a lot for giving time to see all these things. Link to comment Share on other sites More sharing options...
Larry Posted January 4, 2012 Share Posted January 4, 2012 You definitely should NOT be using printf() this way. It's a waste of that function. How about just using echo $content first? Link to comment Share on other sites More sharing options...
Recommended Posts