Jump to content

Getting Mysql Error when I echo data from my database?


mcc_22ri

Recommended Posts

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;
    }
  }
}

?>

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"];
		}
	}
}
}

?>

post-104609-13482403552909_thumb.jpg

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"];
		}
	}
}
}

?>

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

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.