Jump to content

help with some simple PHP codes please


Darkwoods

Recommended Posts

holla... im doing a php news system it is going good so far im tottaly new to PHP so i would need help if anyone could im trying to add data from mysql the data is showing fine but i cant figure out how to make a default $cat to show up im having this code in my index.php and it show up empty if i dont use the $_GET so my question is how can i make a default $cat and if i want to use this code ''ORDER BY id DESC'' how would i do that i tried it with the code below here but it gives me error 

 

please your help would be very nice i have been trying the whole day and i can say i have learned a lot in one day :) but i wont learn more if i dont ask for help :)

 

	 $cat = $_GET['cat'];

 	//load contents from the database
	$sql = "SELECT * FROM `contents` where `cat`='$cat'";
	$result = mysql_query($sql, $connect);
	while ($row = mysql_fetch_array($result))
  			{ 

Link to comment
https://forums.phpfreaks.com/topic/127316-help-with-some-simple-php-codes-please/
Share on other sites

first of all add this at the top of the page

 

error_reporting(E_ALL);

 

then to check if $_GET['cat']; is set you can do this

 

<?php
if(isset($_GET['cat'])) { 

//use it
} else { //assign a default value to $cat

?>

 

change this line also

 

      $result = mysql_query($sql, $connect);

 

to

      $result = mysql_query($sql, $connect) or trigger_error("Query Failed" . mysql_error());

 

you can see that we have added some error reporting in case something goes wrong.

 

 

 

I usually do:

<?php
$cat = isset($_GET['cat']) ? $_GET['cat'] : "";

 

It looks strange, but it says, if $_GET['cat'] is set, then $cat = $_GET['cat'], if not $cat = ""

if you want $cat to have a default value, then change "" to your default value.

 

As far as your query,

$sql = "SELECT * FROM `contents` where `cat`='$cat' ORDER BY `id` DESC";

should be fine.

 

I would use the error handling as posted by Bendude as well.  It will show you what is wrong, if anything, in your query.

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.