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

 

 

 

Link to comment
Share on other sites

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.

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.