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

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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