Jump to content

Need little help!


emarket

Recommended Posts

Hi,

 

I'm not a coder so I need a little help with this code.

 

What I baisicly want to do is to grab the last entry from a column from a table of a database and if that last entry is 1 that go to link1 and if not go to  second link.

 

This code doesn't give me error but is just redirecting me to the first link.

 

Thanks,

 

<?php

$db_host="";
$db_name="";
$username="";
$password="";
$db_con=mysql_connect($db_host,$username,$password);
$connection_string=mysql_select_db($db_name);
// Connection
mysql_connect($db_host,$username,$password);
mysql_select_db($db_name);

$sql = "SELECT id FROM table1 ORDER BY id DESC LIMIT 0,1";

if ($sql=1)
    {

header('Location: http://www.exemple.com/first.php');
    }
else
    {
header('Location: http://www.exemple.com/second.php');

}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/173643-need-little-help/
Share on other sites

Given your example $sql is a string. You need to actully execute that query using mysql_query. Something like....

 

$sql = "SELECT id FROM table1 ORDER BY id DESC LIMIT 0,1";
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    $row = mysql_fetch_assoc($result);
    if ($row['id'] == 1) {
      header('Location: http://www.exemple.com/first.php');
    } else {
      header('Location: http://www.exemple.com/second.php');
    }
  }
}

Link to comment
https://forums.phpfreaks.com/topic/173643-need-little-help/#findComment-915299
Share on other sites

Thanks for you answer.

I get this error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in... on line 16

 

so I think this part of my code is not good

SELECT id FROM table1 ORDER BY id DESC LIMIT 0,1

 

where "id" is my column

Link to comment
https://forums.phpfreaks.com/topic/173643-need-little-help/#findComment-915325
Share on other sites

<?php

$db_host="";
$db_name="";
$username="";
$password="";
$db_con=mysql_connect($db_host,$username,$password);
$connection_string=mysql_select_db($db_name);
// Connection
mysql_connect($db_host,$username,$password);
mysql_select_db($db_name);
$sql = "SELECT id FROM ap_form_4 ORDER BY id DESC LIMIT 0,1; 
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    $row = mysql_fetch_assoc($result);
    if ($row['id']==1); {
      header('Location: http://www.exemple.com/first.php');
    } else {
      header('Location: http://www.exemple.com/second.php');
    }
  }
}

?>

Link to comment
https://forums.phpfreaks.com/topic/173643-need-little-help/#findComment-915335
Share on other sites

 

You should get yourself a decent editor, these simple mistakes are easily picked up with syntax highlighting on.

 

You are right. I'm still working with the old Notepad.

 

Now I get another error:

Parse error: syntax error, unexpected T_ELSE in.. on line 18

<?php

$db_host="";
$db_name="";
$username="";
$password="";
$db_con=mysql_connect($db_host,$username,$password);
$connection_string=mysql_select_db($db_name);
// Connection
mysql_connect($db_host,$username,$password);
mysql_select_db($db_name);
$sql = "SELECT id FROM ap_form_4 ORDER BY id DESC LIMIT 0,1"; 
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    $row = mysql_fetch_assoc($result);
    if ($row['id']==1); {
      header('Location: http://www.exemple.com/first.php');
    }  else {
      header('Location: http://www.exemple.com/second.php');
}

}

}

?>

Link to comment
https://forums.phpfreaks.com/topic/173643-need-little-help/#findComment-915521
Share on other sites

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.