iceblox Posted July 26, 2007 Share Posted July 26, 2007 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!'; } ?> Quote Link to comment Share on other sites More sharing options...
Dragen Posted July 26, 2007 Share Posted July 26, 2007 are you getting any errors? Quote Link to comment Share on other sites More sharing options...
iceblox Posted July 26, 2007 Author Share Posted July 26, 2007 Just No Rows found but ive checked the CarID the name everything. its a bit weird Quote Link to comment Share on other sites More sharing options...
Dragen Posted July 26, 2007 Share Posted July 26, 2007 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. Quote Link to comment Share on other sites More sharing options...
iceblox Posted July 26, 2007 Author Share Posted July 26, 2007 Hmmm, Still no rows found. I really dont get it Quote Link to comment Share on other sites More sharing options...
DeadEvil Posted July 26, 2007 Share Posted July 26, 2007 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!'; } ?> Quote Link to comment Share on other sites More sharing options...
vbnullchar Posted July 26, 2007 Share Posted July 26, 2007 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'); } Quote Link to comment Share on other sites More sharing options...
vbnullchar Posted July 26, 2007 Share Posted July 26, 2007 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 Quote Link to comment Share on other sites More sharing options...
Dragen Posted July 26, 2007 Share Posted July 26, 2007 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 Quote Link to comment Share on other sites More sharing options...
vbnullchar Posted July 26, 2007 Share Posted July 26, 2007 try to output the value of $CarID.. Quote Link to comment Share on other sites More sharing options...
iceblox Posted July 26, 2007 Author Share Posted July 26, 2007 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'); Quote Link to comment Share on other sites More sharing options...
vbnullchar Posted July 26, 2007 Share Posted July 26, 2007 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'); Quote Link to comment Share on other sites More sharing options...
iceblox Posted July 26, 2007 Author Share Posted July 26, 2007 OK, we are getting there when i changed this SELECT * FROM Cars WHERE CarID = '1'); it outputting the query but wont allow me to add a $vairable. Im not sure why Quote Link to comment Share on other sites More sharing options...
DeadEvil Posted July 26, 2007 Share Posted July 26, 2007 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. Quote Link to comment Share on other sites More sharing options...
vbnullchar Posted July 26, 2007 Share Posted July 26, 2007 therefore there is a problem with $CarID, try print its value Quote Link to comment Share on other sites More sharing options...
iceblox Posted July 26, 2007 Author Share Posted July 26, 2007 I excuted this script $query = "SELECT * FROM Cars WHERE CarID = '$CarID'"; print $query; exit; and got this as an output SELECT * FROM Cars WHERE CarID = '' Quote Link to comment Share on other sites More sharing options...
vbnullchar Posted July 26, 2007 Share Posted July 26, 2007 that is why your are getting 0 rows $CarID has no value Quote Link to comment Share on other sites More sharing options...
ViN86 Posted July 26, 2007 Share Posted July 26, 2007 where is $CarID declared? Quote Link to comment Share on other sites More sharing options...
iceblox Posted July 26, 2007 Author Share Posted July 26, 2007 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 Quote Link to comment Share on other sites More sharing options...
Dragen Posted July 26, 2007 Share Posted July 26, 2007 okay. having globals off is an incredibly good thing! How are you setting the $CarID variable? you simply need to carry it for use in your table Quote Link to comment Share on other sites More sharing options...
ViN86 Posted July 26, 2007 Share Posted July 26, 2007 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" Quote Link to comment Share on other sites More sharing options...
iceblox Posted July 26, 2007 Author Share Posted July 26, 2007 carid is specified in the url so its cars.php?carid=1 Quote Link to comment Share on other sites More sharing options...
Dragen Posted July 26, 2007 Share Posted July 26, 2007 what's line 28? Quote Link to comment Share on other sites More sharing options...
ViN86 Posted July 26, 2007 Share Posted July 26, 2007 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. Quote Link to comment Share on other sites More sharing options...
Dragen Posted July 26, 2007 Share Posted July 26, 2007 unless he's set his query as this: $query = "SELECT * FROM Cars WHERE CarID = '" . $_GET['carid'] . "'"; Quote Link to comment 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.