Jump to content

Recommended Posts

Below is my code

 

<?php

for($i=1; $i<=4; $i++) {
$query = "SELECT * FROM `people` WHERE `id` = {$i}  LIMIT 0, 30 ";
$result = mysql_query($query);


if($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
   if ($row['id'] > 1 || $row['id'] < 5 )
   echo $row['id'] . "<br>";
}

}

?>

The output is:

 

Connected Successfully

1

2

Now I want to get only the second value. Can anyone one HELP?

 

 

Link to comment
https://forums.phpfreaks.com/topic/145168-solved-cant-solve/
Share on other sites

pardon me for being nitpicky but try changing all that completely

 

You should always avoid looping a select statement....whenever possible.

Besides

SELECT * FROM `people` WHERE `id` = {$i}  LIMIT 0, 30

is only going to return ONE row anyway so why limit with a range to 30

 

Another note

if($row = 

should be while($row =

That's the only loop you need.....to loop through your results

$query = "SELECT * FROM `people` WHERE `id` IN (1,2,3,4)";
$result = mysql_query($query);


while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
   if ($row['id'] == 2)
       echo $row['id'] . "
";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/145168-solved-cant-solve/#findComment-761930
Share on other sites

Many Thanks.

Actually it was my fault that I did not elaborate what I wanted.

I want to read the end value. Suppose, there are number of  'id's in 'people' that I don't know.

In such case,  I want to read the last one which I could not do. I tried end function but it did not work.

Link to comment
https://forums.phpfreaks.com/topic/145168-solved-cant-solve/#findComment-761944
Share on other sites

I elaborate the problem a little bit, so that if anyone can help!

My code of new_user.php page is like this:

<?php
$my_connection = mysql_connect('localhost', 'root', '');

if (!$my_connection)
{
  die('Could not conncet: ' . mysql_error());
}

echo 'Connected Successfully' . '<br><hr>';

$my_database = mysql_select_db('a');

if (!$my_database)
{
  die('Could not find database: ' . mysql_error());
}
?>

<?php

    function mysql_prep( $value ){
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0
if( $new_enough_php ){ // PHP v4.3.0 or higher
	/*undo any magic quote efects so mysql_real_escape_string can do the work */
	if( $magic_quotes_active ){ $value = stripslashes( $value ); }
	$value = mysql_real_escape_string( $value );
} else { // before PHP v4.3.0
// if magic quotes aren't already on them add slashes manually
if( !$magic_quotes_active ){ $value = addslashes( $value ); }
// if magic quotes are active, then the slashes already exist
}
    return $value;
}
?>
<?php
function check_required_fields($required_array){
$field_errors = array();
foreach($required_array as $fieldname){
	if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)){
	 $field_errors[] = $fieldname;
	}
}
return $field_errors;
}

function check_max_field_lengths($field_length_array){
$field_errors = array();
foreach($field_length_array as $fieldname => $maxlength){
	if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength){	$field_errors[] = $fieldname; }
}
return $field_errors;
}


function display_errors($error_array){
echo "<p class=\"errors\">";
echo "Please review the following fields:<br />";
foreach($error_array as $error) {
	echo " - " . $error . "<br />";
}
echo "</p>";
}
?>

<?php
// START FORM PROCESSING
if (isset($_POST['submit'])) { // form has been submitted.
        $errors = array();
        
        // perform validation on the form data
        $required_fields = array('first_name', 'last_name', 'url', 'email');
        $errors = array_merge($errors, check_required_fields($required_fields, $_POST));    
    	 
        $fields_with_lengths = array('first_name' => 30, 'last_name' => 30, 'url' => 50, 'email' => 30);
        $errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));        

        $first_name = trim(mysql_prep($_POST['first_name']));
    $last_name = trim(mysql_prep($_POST['last_name']));
    $url = trim(mysql_prep($_POST['url']));
    $email = trim(mysql_prep($_POST['email']));
    
    if(empty($errors)) {
	$query = "INSERT INTO people ( first_name, last_name, url, email ) VALUES ( '{$first_name}', '{$last_name}', '{$url}', '{$email}' )";
    $result = mysql_query($query);
    if (mysql_affected_rows() == 1) {
	// Success
	$message = "The user was successfully created. " . "<a href=\"login.php\">Now you can Login</a>";
	echo $message;
} else {
	$message = "The user could not be created. ";
	$message .= "<br />" . mysql_error();
}
    } else {
	if (count($errors) == 1){
		$message = "There was one error in the form.";
	} else {
		$message = "There were " . count($errors) . " errors in the form.";
	}
}
// END FORM PROCESSING




} else { // form has not been submitted
$first_name = "";
$last_name = ""; 
$url = ""; 
$email = ""; 
} 

?>

<form action="new_user.php" method="post">
<table>
<tr>
<td>First Name</td>
<td><input type="text" name="first_name" maxlength="30" value="<?php echo htmlentities($first_name); ?>" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="last_name" maxlength="30" value="<?php echo htmlentities($last_name); ?>" /></td>
</tr>
<tr>
<td>Favourite Website</td>
<td><input type="text" name="url" maxlength="50" value="<?php echo htmlentities($url); ?>" /></td>
</tr>
<tr>
<td>Your E-mail</td>
<td><input type="text" name="email" maxlength="30" value="<?php echo htmlentities($email); ?>" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="Create user" /></td>
</tr>
</table>
</form>
</td>
</tr>
</table>



        
<?php mysql_close($my_connection); ?>    	

The code of login.php is like this:

<?php
$my_connection = mysql_connect('localhost', 'root', '');

if (!$my_connection)
{
  die('Could not conncet: ' . mysql_error());
}

echo 'Connected Successfully' . '<br><hr>';

$my_database = mysql_select_db('a');

if (!$my_database)
{
  die('Could not find database: ' . mysql_error());
}
?>
<?php
  function redirect_to( $location = NULL ){
if ($location != NULL){
	header("Location: {$location}");
	exit;
}
    }
?>
    
<?php

    function mysql_prep( $value ){
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0
if( $new_enough_php ){ // PHP v4.3.0 or higher
	/*undo any magic quote efects so mysql_real_escape_string can do the work */
	if( $magic_quotes_active ){ $value = stripslashes( $value ); }
	$value = mysql_real_escape_string( $value );
} else { // before PHP v4.3.0
// if magic quotes aren't already on them add slashes manually
if( !$magic_quotes_active ){ $value = addslashes( $value ); }
// if magic quotes are active, then the slashes already exist
}
    return $value;
}
?>

<?php
function check_required_fields($required_array){
$field_errors = array();
foreach($required_array as $fieldname){
	if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)){
	 $field_errors[] = $fieldname;
	}
}
return $field_errors;
}

function check_max_field_lengths($field_length_array){
$field_errors = array();
foreach($field_length_array as $fieldname => $maxlength){
	if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength){	$field_errors[] = $fieldname; }
}
return $field_errors;
}

function display_errors($error_array){
echo "<p class=\"errors\">";
echo "Please review the following fields:<br />";
foreach($error_array as $error) {
	echo " - " . $error . "<br />";
}
echo "</p>";
}
?>

<?php
// START FORM PROCESSING
if (isset($_POST['submit'])) { // form has been submitted.
        $errors = array();
        
        // perform validation on the form data
        $required_fields = array('first_name', 'last_name', 'url', 'email');
        $errors = array_merge($errors, check_required_fields($required_fields, $_POST));    
    	 
        $fields_with_lengths = array('first_name' => 30, 'last_name' => 30, 'url' => 50, 'email' => 30);
        $errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));
              
        $first_name = trim(mysql_prep($_POST['first_name']));
    $last_name = trim(mysql_prep($_POST['last_name']));
    $url = trim(mysql_prep($_POST['url']));
    $email = trim(mysql_prep($_POST['email']));
    
    if(empty($errors)) {
    	$query = "SELECT id, first_name ";
	$query .= "FROM people ";
	$query .= "WHERE first_name = '{$first_name}' ";
	$query .= "AND last_name = '{$last_name}' ";
	$query .= "AND url = '{$url}' ";
	$query .= "AND email = '{$email}' ";
	$query .= "LIMIT 1";
	$result_set = mysql_query($query);

    if (mysql_num_rows($result_set) == 1) {
	// username/password authinticated
	// and only 1 match
	// echo "You can login";
	redirect_to("ab.php");		

        } else {
	if (count($errors) == 1){
		$message = "There was 1 error in the form.";
	} else {
		$message = "There were " . count($errors) . " errors in the form.";
	}
	}

} else { // form has not been submitted.
if (isset($_GET['logout']) && $_GET['logout'] == 1) {
$message = "You are logged out.";
} echo $maessage;
$first_name = "";
$last_name = ""; 
$url = ""; 
$email = ""; 
}
} 
?>


<form action="login.php" method="post">
<table>
<tr>
<td>First Name</td>
<td><input type="text" name="first_name" maxlength="30" value="<?php echo htmlentities($first_name); ?>" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="last_name" maxlength="30" value="<?php echo htmlentities($last_name); ?>" /></td>
</tr>
<tr>
<td>Favourite Website</td>
<td><input type="text" name="url" maxlength="50" value="<?php echo htmlentities($url); ?>" /></td>
</tr>
<tr>
<td>Your E-mail</td>
<td><input type="text" name="email" maxlength="30" value="<?php echo htmlentities($email); ?>" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="LogIn" /></td>
</tr>
</table>
</form>
</td>
</tr>
</table>

<?php mysql_close($my_connection); ?>

:-\

Now if anyone filled up the form and logged in, the datas go directly to the the database in the table "people".

I want to read the 'id' of people which is auto-incremented with every entry. If I use "SELECT * FROM people", the whole bunch of 'id's come out. I don't want the whole thing. I want only the 'end' 'id'.

Any IDEA?

 

 

Link to comment
https://forums.phpfreaks.com/topic/145168-solved-cant-solve/#findComment-762277
Share on other sites

 

Now if anyone filled up the form and logged in, the datas go directly to the the database in the table "people".

I want to read the 'id' of people which is auto-incremented with every entry. If I use "SELECT * FROM people", the whole bunch of 'id's come out. I don't want the whole thing. I want only the 'end' 'id'.

Any IDEA?

 

 

 

Yeah this is easy. Just "SELECT * FROM people...." and order by ID DESC".

 

This will get you all the records but return the last entry to the top.

 

Then you don't even have to do a loop, you just output your return query and it will only give you

the last entry.

 

 

Link to comment
https://forums.phpfreaks.com/topic/145168-solved-cant-solve/#findComment-762295
Share on other sites

Many Many thanks. I found another way where every user find their 'id' along with 'first_name' and 'last_name' .

The code is:

<?php
$result = mysql_query("SELECT id, first_name, last_name FROM people", $my_connection);
while (list($id, $first_name, $last_name) = mysql_fetch_row($result)) {
    echo " <tr>\n" .
          "  <td><a href=\"content.php?id=$id\">$first_name . $id</a></td>\n" .
          "  <td>$last_name</td>\n" .
          " </tr>\n";
}


?>

It gives a nice link where anyone can click the his/her name and author_id link and land up in the main page.

 

Link to comment
https://forums.phpfreaks.com/topic/145168-solved-cant-solve/#findComment-762311
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.