Jump to content

Problem in SQL Statement

- - - - -

  • Please log in to reply
24 replies to this topic

#1
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts
Good day!

When I change my code to separate html and php code and I used template and config which is not my code . I got a lot of problems and error. And sad to say I have no rights to edit or change the code in template and config sender and also I cannot dispplay those code here in forum.

here is my old code of machine1:

<?php

session_start(); 

if(empty($_SESSION['logged_in'])) {

    header('Location:index.php');

    die();

}

?>

<html>

<head>

<meta>

</head>

 <body>

 <form name="machine1" action="machine1.php" method="post">

 

 <p>

  <?php

 

   $sort = "ASC";

  $data_sort = "Emp_ID";

  

  if(isset($_GET['sorting']))

	{

		if($_GET['sorting'] == 'ASC'){

			$sort = "DESC";

		}

		else{

			$sort = "ASC";

		}

	}

	

	

	if (isset($_GET['field_name'])) {

		if($_GET['field_name']  == 'Emp_ID'){

			$data_sort = "Emp_ID";

		}

		elseif($_GET['field_name'] == 'Last_Name'){

			$data_sort = "Last_Name";

		}

		elseif($_GET['field_name'] == 'First_Name'){

			$data_sort = "First_Name";

		}

		elseif($_GET['field_name'] == 'Birthday'){

			$data_sort = "Birthday";

		}

	}

 ?>

 

 <table border="1">

   <tr>

 <td><a href="machine1.php?sorting=<?php echo $sort; ?>&field_name=Emp_ID">Emp ID</a></td>

 <td><a href="machine1.php?sorting=<?php echo $sort; ?>&field_name=Last_Name">Last Name</a></td>

 <td><a href="machine1.php?sorting=<?php echo $sort; ?>&field_name=First_Name">First Name</a></td>

 <td><a href="machine1.php?sorting=<?php echo $sort; ?>&field_name=Birthday">Birthday</a></td>

 <td>Option</td>

 </tr>

 

<?php 

include 'connection.php';


if (isset($_GET['pageno'])) {

   $pageno = $_GET['pageno'];

} else {

   $pageno = 1;

} 

$query = "SELECT count(*) FROM tbl_machine1";

$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);

$query_data = mysql_fetch_row($result);

$numrows = $query_data[0];


$rows_per_page = 5;

$lastpage      = ceil($numrows/$rows_per_page);


$pageno = (int)$pageno;

if ($pageno > $lastpage) {

   $pageno = $lastpage;

} 

if ($pageno < 1) {

   $pageno = 1;

} 


$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;




$query = "SELECT * FROM tbl_machine1 ORDER BY $data_sort $sort $limit";

$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);



while($info = mysql_fetch_array( $result ))

{

	$emp_id = $info['Emp_ID'];

	$lname = $info['Last_Name'];

	$fname = $info['First_Name'];

	$bday = $info['Birthday'];

	$date = date('d-m-Y', strtotime($bday));

	?>

	<tr>

	<td><?php echo $emp_id;?> </td>

    <td><?php echo htmlentities($lname, ENT_QUOTES); ?> </td>

    <td><?php echo htmlentities($fname, ENT_QUOTES);?> </td>

    <td><?php echo htmlentities($date, ENT_QUOTES);?> </td>

   	<td><a href = 'edit.php?id=<?php echo $emp_id; ?>'>Edit</a> <a href='delete.php?id=<?php echo $emp_id; ?>' onClick="return confirm('Are you sure you want to delete?')">Delete</a></td>

	</tr>

    <?php

}

?>

	</table>

     <A HREF="javascript:void(0)" onClick="window.open('add.php','welcome','width=300,height=200')">

<input type="button" name="add" value="ADD"> </A>  


<?php


 if(isset($_GET['sorting']))

	{

		if($_GET['sorting'] == 'ASC'){

			$sort = "ASC";

		}

		else{

			$sort = "DESC";

		}

	}

				

if ($pageno == 1) {

   echo " FIRST PREV ";

} else {

 ?>

 <a href="machine1.php?pageno=1&field_name=<?php echo $data_sort; ?>&sorting=<?php echo $sort; ?>">FIRST</a>

<?php

   $prevpage = $pageno-1;

?>

   <a href="machine1.php?pageno=<?php echo $prevpage;?>&field_name=<?php echo $data_sort; ?>&sorting=<?php echo $sort; ?>">PREV</a>

 

<?php

}

echo " ( Page $pageno of $lastpage ) ";


if ($pageno == $lastpage) {

   echo " NEXT LAST ";

} else {

   $nextpage = $pageno+1;


 ?>

 

	<a href="machine1.php?pageno=<?php echo $nextpage; ?>&field_name=<?php echo $data_sort; ?>&sorting=<?php echo $sort; ?>">NEXT</a>

   	<a href="machine1.php?pageno=<?php echo $lastpage; ?>&field_name=<?php echo $data_sort; ?>&sorting=<?php echo $sort; ?>">LAST</a> 

 <?php

}


?>

 </body>

 </html>


and when I edit it

<html>

<head>

<meta>


</head>


<body>

<?php


error_reporting(E_ERROR | E_WARNING | E_PARSE);

include('includes/config.sender.php');

include('includes/template.inc');



/*Sorting of Data*/

 $sort = "ASC";

  $data_sort = "Emp_ID";

  

  if(isset($_GET['sorting']))

	{

		if($_GET['sorting'] == 'ASC'){

			$sort = "DESC";

		}

		else{

			$sort = "ASC";

		}

	}

	

	

	if (isset($_GET['field_name'])) {

		if($_GET['field_name']  == 'Emp_ID'){

			$data_sort = "Emp_ID";

		}

		elseif($_GET['field_name'] == 'Last_Name'){

			$data_sort = "Last_Name";

		}

		elseif($_GET['field_name'] == 'First_Name'){

			$data_sort = "First_Name";

		}

		elseif($_GET['field_name'] == 'Birthday'){

			$data_sort = "Birthday";

		}

	}

	

	

/*Pagination, Sorting and Limit*/


if (isset($_GET['pageno'])) {

   $pageno = $_GET['pageno'];

} else {

   $pageno = 1;

} 

//$query = "SELECT count(*) FROM tbl_machine1";


$sql_select = "SELECT COUNT(

					Emp_ID,

					Last_Name,

					First_Name,

					Birthday)

			   FROM

					machine_problem_rhoda";

					

$result = $_DB->opendb($sql_select);


//var_dump($sql_select);



//$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);

//$query_data = mysql_fetch_row($result);

//$numrows = $query_data[0];

$numrows = $result[0];


$rows_per_page = 5;

$lastpage      = ceil($numrows/$rows_per_page);


$pageno = (int)$pageno;

if ($pageno > $lastpage) {

   $pageno = $lastpage;

} 

if ($pageno < 1) {

   $pageno = 1;

} 


$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;




$sql_select = "SELECT

					Emp_ID,

					Last_Name,

					First_Name,

					Birthday

				FROM

					machine_problem_rhoda

				ORDER BY $data_sort $sort $limit

				";

$rows = $_DB->opendb($sql_select);


//$query = "SELECT * FROM tbl_machine1 ORDER BY $data_sort $sort $limit";

//$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);



//while($info = mysql_fetch_array( $result ))


$tpl->set_block('handle', 'block_list', 'tag_list');

foreach($rows as $row) {

	$tpl->set_var(array('id'=> $row['Emp_ID'],

						'lastname' => $row['Last_Name'],

						'firstname' => $row['First_Name'],

						'birthday' => $row['Birthday'],

	));

	$tpl->parse('tag_list', 'block_list', true);

}

/*while($info = countdata( $result ))

{

	$emp_id = $info['Emp_ID'];

	$lname = $info['Last_Name'];

	$fname = $info['First_Name'];

	$bday = $info['Birthday'];

	$date = date('d-m-Y', strtotime($bday));

	?>

	<tr>

	<td><?php echo $emp_id;?> </td>

    <td><?php echo htmlentities($lname, ENT_QUOTES); ?> </td>

    <td><?php echo htmlentities($fname, ENT_QUOTES);?> </td>

    <td><?php echo htmlentities($date, ENT_QUOTES);?> </td>

   	<td><a href = 'edit.php?id=<?php echo $emp_id; ?>'>Edit</a> <a href='delete.php?id=<?php echo $emp_id; ?>' onClick="return confirm('Are you sure you want to delete?')">Delete</a></td>

	</tr>

    <?php

}*/

?>

	<!--</table>-->

     <A HREF="javascript:void(0)" onClick="window.open('add.php','welcome','width=300,height=200')">

<input type="button" name="add" value="ADD"> </A>  


<?php


 if(isset($_GET['sorting']))

	{

		if($_GET['sorting'] == 'ASC'){

			$sort = "ASC";

		}

		else{

			$sort = "DESC";

		}

	}

				

if ($pageno == 1) {

   echo " FIRST PREV ";

} else {

 ?>

 <a href="machine1.php?pageno=1&field_name=<?php echo $data_sort; ?>&sorting=<?php echo $sort; ?>">FIRST</a>

<?php

   $prevpage = $pageno-1;

?>

   <a href="machine1.php?pageno=<?php echo $prevpage;?>&field_name=<?php echo $data_sort; ?>&sorting=<?php echo $sort; ?>">PREV</a>

 

<?php

}

echo " ( Page $pageno of $lastpage ) ";


if ($pageno == $lastpage) {

   echo " NEXT LAST ";

} else {

   $nextpage = $pageno+1;


 ?>

 

	<a href="machine1.php?pageno=<?php echo $nextpage; ?>&field_name=<?php echo $data_sort; ?>&sorting=<?php echo $sort; ?>">NEXT</a>

   	<a href="machine1.php?pageno=<?php echo $lastpage; ?>&field_name=<?php echo $data_sort; ?>&sorting=<?php echo $sort; ?>">LAST</a> 

 <?php

}


?>






<?php



/*$sql_select = "SELECT

					Emp_ID,

					Last_Name,

					First_Name,

					Birthday

			   FROM

					machine_problem_rhoda";

					

$rows = $_DB->opendb($sql_select);*/



$tpl = new Template('.', 'keep');

$tpl->set_file(array('handle' => 'html/machine1.html'));


/*$tpl->set_block('handle', 'block_list', 'tag_list');

foreach($rows as $row) {

	$tpl->set_var(array('id'=> $row['Emp_ID'],

						'lastname' => $row['Last_Name'],

						'firstname' => $row['First_Name'],

						'birthday' => $row['Birthday'],

	));

	$tpl->parse('tag_list', 'block_list', true);

}*/



$tpl->parse('handle', array('handle'));

$tpl->p('handle');


?>

</body>

</html>


and I got this error:

SELECT COUNT(
Emp_ID,
Last_Name,
First_Name,
Birthday)
FROM
machine_problem_rhoda
query 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 '
Last_Name,
First_Name,
Birthday)
FROM
machine_pr' at line 2

I tried to search in internet regarding this error and I cant find the answer to sove my problem.:confused:

I hope somebody can help me..because I dont have any companion only forum

Thank you

#2
ghost_x47

ghost_x47

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts

Quote

SELECT COUNT(
Emp_ID,
Last_Name,
First_Name,
Birthday)
Whoa, what are you trying to do here?
Count - single argument function as i know.

#3
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts

ghost_x47 said:

Whoa, what are you trying to do here?
Count - single argument function as i know.

Sorry i really don't know..
How can i solved it

before that is code is like this:

$query = "SELECT count(*) FROM machine_problem_rhoda";


and now I change it as what my boss said

$sql_select = "SELECT COUNT(

					Emp_ID,

					Last_Name,

					First_Name,

					Birthday)

			   FROM

					machine_problem_rhoda";

					

$result = $_DB->opendb($sql_select);

he told me that its better to put all the field name rather than * because * is slow.

#4
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts
When i tried to used this code:

$sql_select = "SELECT COUNT(*)

				FROM 

					machine_problem_rhoda";

					

$result = $_DB->opendb($sql_select);


I got this fatal error:

Fatal error: Unsupported operand types in /opt/zeva/releases/ZEVA.sandbox/machine_problem/rhoda/machine1.php on line 84

line 84 is

$lastpage      = ceil($numrows/$rows_per_page);

:(

#5
ghost_x47

ghost_x47

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
Yeah - in a simple select query, In this query you are trying to COUNT how many entries in database are.
COUNT can't rely on several fields, so you can only count for one field - basically you will count on ID field if you want all entries from database.
Or count(*) - has the same perfomance.Since count returns only 1 number;
Basically - You can't count apples,oranges,gastanks and spoons together...

#6
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts

ghost_x47 said:

Yeah - in a simple select query, In this query you are trying to COUNT how many entries in database are.
COUNT can't rely on several fields, so you can only count for one field - basically you will count on ID field if you want all entries from database.
Or count(*) - has the same perfomance.Since count returns only 1 number;
Basically - You can't count apples,oranges,gastanks and spoons together...

What should I do now?I used * but i got a fatal error:confused:

#7
ghost_x47

ghost_x47

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts

Quote

line 84 is

$lastpage      = ceil($numrows/$rows_per_page);

:(

hmm...this says that you might need to check what you have in $numrows and $rows_per_page variables.
By the way var_dump - is a great way to do this.Since it write a type of variables

#8
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts

ghost_x47 said:

hmm...this says that you might need to check what you have in $numrows and $rows_per_page variables.
By the way var_dump - is a great way to do this.Since it write a type of variables

uhmm...

this is the old code

$query = "SELECT count(*) FROM tbl_machine1";

$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);

$query_data = mysql_fetch_row($result);

$numrows = $query_data[0];


$rows_per_page = 5;

$lastpage      = ceil($numrows/$rows_per_page);


and now that I need to used the function opendb i change my code like this

$sql_select = "SELECT COUNT(*)

				FROM 

					machine_problem_rhoda";

					

$result = $_DB->opendb($sql_select);


print_r($result);


//var_dump($sql_select);



//$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);

//$query_data = mysql_fetch_row($result);

//$numrows = $query_data[0];

$numrows = $result[0];


$rows_per_page = 5;

$lastpage      = ceil($numrows/$rows_per_page);


and i got fatal error

sorry if I cannot display the code for function opendb but it goes like this:
opendb is a function
for select statement
connect if there's no active connection
then if $result(mysql_query == false)
the error appear
then there is a while loop for mysql_fetch_array
then a foreach for the col name => value

as you can see in my old code i used mysql_fetch_row and on the function that is not my code ised mysql_fetch_array.

#9
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
The count function requires an expression and not a delimited list of columns. If you are using a MyISAM table (which should be default), your boss is incorrect and * is very optimized without a WHERE column, this is due to the fact that the row counts are stored with the table already.

If you are using InnoDB, then the most simple solution would be:
SELECT 

    SUM(COUNT(Emp_ID) + COUNT(Last_Name) + COUNT(First_Name) + COUNT(Birthday))

FROM

    machine_problem_rhoda


Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#10
ghost_x47

ghost_x47

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
you printed out a $result - does it have a [0] element?
if opendb returns name=>value , then you need to "SELECT COUNT(*) as count" and retrieve like $result['count'] or something like that. if you put <pre> tag before print_r - you will have a good look at arrays.

#11
ghost_x47

ghost_x47

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
                     

$result = $_DB->opendb($sql_select); 

echo "<pre>"; 

print_r($result); 

echo "</pre>"; 


$numrows = $result['count']; 


Here is what i talked about, you must misunderstood me.
this error with set_block says that a $tpl must be an object, probably you lost it somewhere on the road..
past here a print_r output of result next time, plz.

#12
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts
I tried what you said:
like this:


$sql_select = "SELECT COUNT(*) as numrows

				FROM 

					machine_problem_rhoda";

					

$result = $_DB->opendb($sql_select);


echo "<pre>";

print_r($result);

echo "</pre>";



$numrows = $result['count'];


and the output is

Array
(
[0] => Array
(
[numrows] => 20
)

)


Fatal error: Call to a member function set_block() on a non-object in /opt/zeva/releases/ZEVA.sandbox/machine_problem/rhoda/machine1.php on line 121

Attached Files






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users