mcc_22ri Posted May 28, 2012 Share Posted May 28, 2012 Hi Everyone, I'm attempting to extract data from my mysql database and use that data in my URLS. I'm almost getting the desired result I need but I'm getting a nasty error when I try the code (the below code is below). I've noticed when I leave out if (mysql_num_rows($data===1)); I'm not getting an error. Should I just simply leave this coding out? but if I do won't it be less secure? Thanks! http://whatsmyowncarworth.com/auto/Houston http://whatsmyowncarworth.com/auto/Seattle code that's not working <<-- I'm getting an error message with this code but it does echo out the desired result. <?php ob_start(); // handle redirects include('init.php'); // connection to database if (isset($_GET['u'])) { $city = mysql_real_escape_string($_GET['u']); if (ctype_alnum($city)) // protection against mysql injection { $data = mysql_query("SELECT State, City FROM cars WHERE City='$City'" ); if (mysql_num_rows($data===1)); { echo $city; } } } ?> >>>>>>>>>>>> Code that's working <?php ob_start(); // handle redirects include('init.php'); // connection to database if (isset($_GET['u'])) { $city = mysql_real_escape_string($_GET['u']); if (ctype_alnum($city)) // protection against mysql injection { $data = mysql_query("SELECT State, City FROM cars WHERE City='$City'" ); { echo $city; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/263267-getting-mysql-error-when-i-echo-data-from-my-database/ Share on other sites More sharing options...
trq Posted May 28, 2012 Share Posted May 28, 2012 Firstly, $data will never equal 1. It will be a result resource if your query succeeds, or the bool false if it fails. Secondly, no where in your code do you attempt to use the resultant data from your query. Quote Link to comment https://forums.phpfreaks.com/topic/263267-getting-mysql-error-when-i-echo-data-from-my-database/#findComment-1349190 Share on other sites More sharing options...
mcc_22ri Posted May 28, 2012 Author Share Posted May 28, 2012 Hi thorpe, I tried the below code. I'm not getting any errors but it's not echoing out the code. I've also attached a pic of my mysql database where I'm extracting the data. <?php ob_start(); // handle redirects include('init.php'); // connection to database if (isset($_GET['u'])) { $city = mysql_real_escape_string($_GET['u']); // protection against mysql injection if (ctype_alnum($city)) { $data = mysql_query("SELECT State, City FROM cars WHERE City='$city'" ); if (mysql_num_rows($data) > 0) { while ($row = mysql_fetch_assoc($data)) { echo $row["City"]; } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/263267-getting-mysql-error-when-i-echo-data-from-my-database/#findComment-1349197 Share on other sites More sharing options...
Zane Posted May 28, 2012 Share Posted May 28, 2012 You are using an output buffer, you must flush it to get any output.. using ob_end_flush Quote Link to comment https://forums.phpfreaks.com/topic/263267-getting-mysql-error-when-i-echo-data-from-my-database/#findComment-1349198 Share on other sites More sharing options...
mcc_22ri Posted May 28, 2012 Author Share Posted May 28, 2012 Brillant. It's working! Below is my code. All I did was simply add ob_end_flush(); at the top of the page and it's working. Should I have done this another way or was I correct in how I did it? Thanks again! <?php ob_start(); // handle redirects ob_end_flush(); include('init.php'); // connection to database if (isset($_GET['u'])) { $city = mysql_real_escape_string($_GET['u']); // protection against mysql injection if (ctype_alnum($city)) { $data = mysql_query("SELECT State, City FROM cars WHERE City='$city'" ); if (mysql_num_rows($data) > 0) { while ($row = mysql_fetch_assoc($data)) { echo $row["City"]; } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/263267-getting-mysql-error-when-i-echo-data-from-my-database/#findComment-1349201 Share on other sites More sharing options...
mrMarcus Posted May 28, 2012 Share Posted May 28, 2012 Placing ob_end_flush() immediately following ob_start() is pointless. Your code, as it stands, does not require any buffering. You can safely remove ob_start() and ob_end_flush() from your code. Here's more info: ob_start && ob_end_flush Quote Link to comment https://forums.phpfreaks.com/topic/263267-getting-mysql-error-when-i-echo-data-from-my-database/#findComment-1349225 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.