Jump to content

[SOLVED] Simple Query


iceblox

Recommended Posts

Hi Guy this is probably going to be the blondest post of the day but i cant for the life of me get this simple query right. Ive change the format ive changed everything.

 

Can anyone see anything wrong with this code?

 

<?php

$connection = mysql_connect('localhost','user','pass');

mysql_select_db('name');

$query = "SELECT * FROM Cars WHERE CarID = '$CarID'";

$result = mysql_query($query);

if (mysql_num_rows($result) > 0)
{

while($row = mysql_fetch_row($result))
{

echo '>  <a href=' . str_replace(" ","-",$row['1'] ) . '-Cars-' . str_replace(" ","-",$row['3'] ) . '.html><font class="traillinks">' . $row['2'] . '</a>  >  <font class="traillinks">' . $row['3'] . '</font>';

}

}
else
{
echo 'No rows found!';
}


?>

Link to comment
Share on other sites

try this:

<?php
$connection = mysql_connect('localhost','user','pass');

mysql_select_db('name');

$query = "SELECT * FROM `Cars` WHERE `CarID` = '$CarID'";

if($result = mysql_query($query)){
	if(mysql_num_rows($result) > 0){
		while($row = mysql_fetch_row($result)){*/
			echo '>  <a href="' . str_replace(" ","-",$row['1'] ) . '-Cars-' . str_replace(" ","-",$row['3'] ) . '.html"><font class="traillinks">' . $row['2'] . '</a>  >  <font class="traillinks">' . $row['3'] . '</font>';
		}
	}else{
		echo 'No rows found!';
	}
}else{
	echo mysql_error();
}
?>

 

you didn't have any error handling the the actual mysql_query itself. Also, you should surround table name etc in a query with `.

Another small error with the output was that you had no " around the url, so it may have caused problems when outputting.

Link to comment
Share on other sites

or you can try this..

 


<?php
$connection = mysql_connect('localhost','user','pass');
mysql_select_db('name');

$result = mysql_query("SELECT * FROM Cars WHERE CarID = '$CarID'");
$num = mysql_num_rows($result);
if ($num > 0){
$content = '';
while($row = mysql_fetch_array($result)){
	$content .= '><a href=';
	$content .= str_replace(" ","-",$row['column_name']).'-Cars-'; // changed "column_name" to real column name
	$content .= str_replace(" ","-",$row['column_name']).'.html>';
	$content .= '<font class="traillinks">'.$row['column_name'].'</a>>';
	$content .= '<font class="traillinks">'.$row['column_name'].'</font>';
	echo $content;
}
}
else{
echo 'No rows found!';
}
?>

Link to comment
Share on other sites

may be there is something wrong with the query...

try this things to help you find what is wrong

 

$connection = mysql_connect('localhost','user','pass') or die('connection failed');

 

mysql_select_db('name') or die('DB selection failed');

 

if(!mysql_query($query)) {

die('Query Failed');

}

 

Link to comment
Share on other sites

or you can try this..

 


<?php
$connection = mysql_connect('localhost','user','pass');
mysql_select_db('name');

$result = mysql_query("SELECT * FROM Cars WHERE CarID = '$CarID'");
$num = mysql_num_rows($result);
if ($num > 0){
$content = '';
while($row = mysql_fetch_array($result)){
	$content .= '><a href=';
	$content .= str_replace(" ","-",$row['column_name']).'-Cars-'; // changed "column_name" to real column name
	$content .= str_replace(" ","-",$row['column_name']).'.html>';
	$content .= '<font class="traillinks">'.$row['column_name'].'</a>>';
	$content .= '<font class="traillinks">'.$row['column_name'].'</font>';
	echo $content;
}
}
else{
echo 'No rows found!';
}
?>

 

if he's getting "No rows found" as output then there must be something wrong with the query

Link to comment
Share on other sites

try changing your query to this:

$query = "SELECT * FROM `Cars` WHERE `CarID` = '" . $CarID . "'";

just to check it's not reading the variable as it's name.

If it's not dinding the rows, than your query must be incorrect. Are you sure 'Cars' isn't 'cars' or 'CarID' isn't 'carid'?

 

or do this:

echo "SELECT * FROM `Cars` WHERE `CarID` = '" . $CarID . "'";

and see what it outputs

Link to comment
Share on other sites

Ive tried all your ideas and nothing. it just says no rows found..

 

this is my table

 

CREATE TABLE `Cars` (
  `CarID` int(10) NOT NULL auto_increment,
  `MakeName` varchar(255) NOT NULL,
  `CarName` varchar(255) NOT NULL,
  `MakeID` varchar(255) NOT NULL,
  `CarType` varchar(255) NOT NULL,
  `Text` text NOT NULL,
  `Img1` varchar(255) NOT NULL,
  `Img2` varchar(255) NOT NULL,
  `Img3` varchar(255) NOT NULL,
  `Img4` varchar(255) NOT NULL,
  `Img5` varchar(255) NOT NULL,
  `Img6` varchar(255) NOT NULL,
  `ActiveImg` varchar(255) NOT NULL,
  PRIMARY KEY  (`CarID`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

-- 
-- Dumping data for table `Cars`
-- 

INSERT INTO `Cars` (`CarID`, `MakeName`, `CarName`, `MakeID`, `CarType`, `Text`, `Img1`, `Img2`, `Img3`, `Img4`, `Img5`, `Img6`, `ActiveImg`) VALUES (1, 'Ford', 'Fiesta', '2', 'Supermini', 'Displayed below are links to find your perfect car! Simply click on a logo and it will take you to a page full of cars by that manufacturer. We currently have X cars stored in the database. If there is one that is missing and you feel deserves a place on the site then just send us an email via this link and we will add it asap!', 'Ford-Fiesta-1.jpg', 'Ford-Fiesta-2.jpg', 'Ford-Fiesta-3.jpg', 'Ford-Fiesta-4.jpg', 'Ford-Fiesta-5.jpg', 'Ford-Fiesta-6.jpg', 'Ford-Fiesta-1.jpg');

Link to comment
Share on other sites

try to output your $query

 

$query;

then check the value of $CarID then see your table if you really have  this record.

 

your sql dump says you only have 1 row

 

then your query should look like this to print 1 row

 

### SELECT * FROM Cars WHERE CarID = '1');

 

Link to comment
Share on other sites

it means no problem with the connection.. huh? I think you should print the query first.

 

$query = "SELECT * FROM Cars WHERE CarID = '$CarID'";
print $query; exit;

 

.. copy the output of the above statement then paste it on your pma query window.

Link to comment
Share on other sites

Just found out from my hosts that they moved my server and is no running PHP5 wih register globals switched off?

So i need to structure the vairalbe like this.

 

$_GET['carid']

 

but im getting this error

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /usr/local/psa/home/vhosts/freecarreviews.com/httpdocs/cars.php on line 28

Link to comment
Share on other sites

Just found out from my hosts that they moved my server and is no running PHP5 wih register globals switched off?

So i need to structure the vairalbe like this.

 

$_GET['carid']

 

but im getting this error

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /usr/local/psa/home/vhosts/freecarreviews.com/httpdocs/cars.php on line 28

 

first and foremost,  where are you declaring $CarID?

 

secondly, $_GET['carid'] wont work unless your URL looks like pagename.php?carid=value

in which case $_GET['carid'] would return a value of "value"

Link to comment
Share on other sites

carid is specified in the url so its cars.php?carid=1

 

 

so where do you declare

$CarID = $_GET['carid']   

???

 

just cause its in the url doesnt mean the value is automatically set.  you still need to declare the $CarID variable and set it equal to the $_GET['carid'] value thats passed.

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.