Jump to content

Problem with this code is not executed with php


sonita

Recommended Posts

hello Is there something like this in your website.w3schools Fourm -->Server Scripting -->PHP I wish I had something like this on my website. and I have written the code as follows:

  • 	<?php	function DisplayPath($cat_id){	mysql_connect('localhost','keyvandb','') or die('connection error');	mysql_select_db('keyvandb') or die('Database error');	mysql_query('SET NAMES\'utf8\'');	mysql_set_charset('utf8');	$cat_id=mysql_real_escape_string($cat_id);	$category=query("SELECT * FROM 'categories' WHERE('id'='{cat_id}') LIMIT 1");	$nav='';	if($category && mysql_num_rows($category)>0){	$category=mysql_fetch_assoc($category);	do {	$nav= '<a herf=" '.ADDR.'/categories/'.$category['id'].'/'.$category['name'].' ">'.$category['name'] .'</a> -> '.$nav;	$parent=mysql_real_escape_string($category['parent']);	$category =query("SELECT * FROM 'categories' WHERE('id'='{$parent}') LIMIT 1");	if($category && mysql_num_rows($category)>0){	$category=mysql_fetch_assoc($category);	}	} while (is_array($category));	//remove the last ->	$nav=mb_substr($nav,0,mb_strlen($nav,'utf-8')-7,'utf-8');	}	echo '<b>دسته بندی:</b>'.$nav.PHP_EOL();	}	?>	

    but don`t exe? why? and i save myinformation in DataBase . please help me?

Link to comment
Share on other sites

What happens when you run that? Are there error messages? Possible problems I see are things like not having a space after SET NAMES, calling a function called query when you're using mysql_query in other places, and not checking for errors when running the queries.

Link to comment
Share on other sites

It might be because of the issues I mentioned above. Add this code to the top of the function to make sure error messages are enabled: ini_set('display_errors', 1);error_reporting(E_ALL);

Link to comment
Share on other sites

I add that code(you say) to the top of the function but show nothing error or message .how do i now? :(I put a code sample that I might test runs,please

Link to comment
Share on other sites

I put a space in the SET NAMES & again run but don`t show.i write this query because :I am iranian & my language is presion ,So it should accept my language & utf8`s language . and show they.but i don`t why change this query? how?What do I have to run this code? What changes can I add to it? Until I see the results on my website.please help me & explain with example for me.

Edited by sonita
Link to comment
Share on other sites

I suspect you are not calling the function displayPath(). are you calling it somewhere? if it does not get called it wont execute so it wont show any runtime error or do anything.

Link to comment
Share on other sites

Look, here you use a function called mysql_query: mysql_query('SET NAMES \'utf8\''); But here, you use a function called query: $category=query("SELECT * FROM 'categories' WHERE('id'='{cat_id}') LIMIT 1"); The mysql_query function is built-in, here it is: http://www.php.net/manual/en/function.mysql-query.php But there's no function called query: http://www.php.net/manual-lookup.php?pattern=query&scope=quickref So, did you create a function called query? If not, then it's going to be an error to try and use a function that doesn't exist. That's what I'm trying to tell you.

Link to comment
Share on other sites

  • 3 weeks later...

helloi change above code as follows:this is code:

<?php require_once 'config.php'; ?><?phpfunction DisplayPath($cat_id) {    error_reporting(E_ALL);    mysql_connect('localhost', 'root', '') or die('Connection error');    mysql_select_db('keivandb') or die('Database error');    mysql_query('SET NAMES\'utf8\'');    mysql_set_charset("utf8");    $cat_id = mysql_real_escape_string($cat_id);    $category = mysql_query("SELECT * FROM categories WHERE id={$cat_id} LIMIT 1");    $nav = '';$name="";$id="";$parent="";while($x = mysql_fetch_array($category))    {   	 $name= $x['name'];	 $id=$x['id'];	 $parent=$x['parent'];     }$cat="categories";    if($category && mysql_num_rows($category) > 0) {	    $category = mysql_fetch_assoc($category);	    do {		    //$nav = '<a href=\" ' . ADDR . '/categories/' . $category['id'] . '/' . $category['name'] . ' \">' . $category['name'] . '</a> -> ' . $nav;     $nav = "<a href=\"http://localhost/test1/{$cat}/{$id}/{$name}\" >{$name}<a>";   echo $nav;  		    $parent = mysql_real_escape_string($category['parent']);		    $category = mysql_query("SELECT * FROM categories WHERE id={$parent} LIMIT 1");		    if($category && mysql_num_rows($category) > 0) {			    $category = mysql_fetch_assoc($category);		    }		    else {			    echo mysql_error();		    }	    } while (is_array($category));	    $nav = mb_substr($nav, 0, mb_strlen($nav, 'utf-8') - 7, 'utf-8');    }    else {	    echo mysql_error();    }  echo '<b>دسته بندی:</b>' . $nav;    //echo '<b>دسته بندی:</b>' . $nav . PHP_EOL();}?>

when exe it show this error (error: you have an error in your SQL syntax; check the manual that corresponds to your MYSQL server version for the right syntax to use near 'LIMIT1' at line 1) what`s meaning error? What can I do to avoid error and run this code?please help me.

Link to comment
Share on other sites

I think the {$parent} should be with no curly brackets... Correct me if i'm wrong...
It should work with curly braces too. it is called interpolation, if a variable enclosed in curly braces inside double quotes it will evaluate its value.
Link to comment
Share on other sites

  • 2 weeks later...

hello,i change above code again , this code exe in myweb page but don`t show The w3schools Fourm -->Server Scripting -->PHP and ArrangedBut as messy as follows (in picture box drawn around it and my language is Persian.)servr scripting->php->w3schools(in persian:طرح آجری-->دسته بندی->طرح) why?this is code :

<?php require_once 'config.php'; ?><?phpfunction DisplayPath($cat_id) {	error_reporting(E_ALL);	mysql_connect('localhost', 'root', '') or die('Connection error');	mysql_select_db('keivandb') or die('Database error');	mysql_query('SET NAMES\'utf8\'');	mysql_set_charset("utf8");	$cat_id = mysql_real_escape_string($cat_id);	$category = mysql_query("SELECT * FROM categories WHERE id={$cat_id} LIMIT 1");	$nav = ''; $name="";$id="";$parent=""; while($x = mysql_fetch_array($category))	{  	 $name= $x['name'];	 $id=$x['id'];	 $parent=$x['parent'];			}$category = mysql_query("SELECT * FROM categories WHERE id={$cat_id} LIMIT 1");while($x = mysql_fetch_array($category))	{  	 $name= $x['name'];  	} $cat="categories";	if($category && mysql_num_rows($category) > 0) {		$category = mysql_fetch_assoc($category);		do {			//$nav = '<a href=\" ' . ADDR . '/categories/' . $category['id'] . '/' . $category['name'] . ' \">' . $category['name'] . '</a> -> ' . $nav;     $nav = "<a href=\"http://localhost/Test2/{$cat}/{$id}/{$name}\" >{$name}<a>";   echo $nav;  			$parent = mysql_real_escape_string($category['parent']);			$category = mysql_query("SELECT * FROM categories WHERE id=parent LIMIT 1");			if($category && mysql_num_rows($category) > 0) {				$category = mysql_fetch_assoc($category);			}			else {				echo mysql_error();			}		} while (is_array($category));		$nav = mb_substr($nav, 0, mb_strlen($nav, 'utf-8') - 7, 'utf-8');	}	else {		echo mysql_error();	}  echo '<b>دسته بندی:</b>' . $nav;	//echo '<b>دسته بندی:</b>' . $nav . PHP_EOL();}?>

how i do Properly and regularly runs? please help me.

Link to comment
Share on other sites

whats the problem you are having with it now? Are you getting any errors? Are you debugging to find where you might have logic errors? Its hard to know what to help you with when you don't tell us what's wrong.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...