Code problems

Discussion in 'PHP' started by shoapin, Mar 25, 2012.

  1. #1
    Using Dreamweaver, i am tryin to append my recordset depending on the username but i am getting the following 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 'LIKE '%user1%'' at line 1

    What i want to be able to do is if there is a user logged in to display their info.

    I only got the error when i added the following line of code that is bold. Can someone help please

    <?php require_once('Connections/PropertyConnect.php'); ?>
    <?php
    if (!isset($_SESSION)) {
    session_start();

    }

    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
    if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
    }

    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

    switch ($theType) {
    case "text":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "long":
    case "int":
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
    break;
    case "double":
    $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
    break;
    case "date":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "defined":
    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
    break;
    }
    return $theValue;
    }
    }

    mysql_select_db($database_PropertyConnect, $PropertyConnect);
    $query_rsTestSim = "SELECT information.custId, information.Username FROM information";
    if (isset($_POST['InfoID'])) {
    $Info = mysql_real_escape_string($_POST['InfoID']);
    $query_rsTestSim.= "WHERE custId LIKE '%$Info%'";
    }
    else if(isset($_SESSION['MM_Username'])){
    $logUser = mysql_real_escape_string($_SESSION['MM_Username']);
    $query_rsTestSim.= "WHERE Username LIKE '%$logUser%'";

    }$rsTestSim = mysql_query($query_rsTestSim, $PropertyConnect) or die(mysql_error());
    $row_rsTestSim = mysql_fetch_assoc($rsTestSim);
    $totalRows_rsTestSim = mysql_num_rows($rsTestSim);

    echo $query_rsTestSim;
    echo $totalRows_rsTestSim;

    ?>
     
    shoapin, Mar 25, 2012 IP
  2. yho_o

    yho_o Well-Known Member

    Messages:
    354
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    #2
    am not sure what is your problem but Why using like with certain values ?, if user is logged in so the values stored in the session should be correct then use 'WHERE `field` = '$value' instead of LIKE
     
    yho_o, Mar 25, 2012 IP
  3. Grit.

    Grit. Well-Known Member

    Messages:
    1,424
    Likes Received:
    22
    Best Answers:
    1
    Trophy Points:
    110
    #3
    For each of the MySQL run statements, add :
    
     or die(mysql_error());
    
    PHP:
    Just append it after you run the query, and it will tell you if there is any issues in your SQL
     
    Grit., Mar 25, 2012 IP
  4. yho_o

    yho_o Well-Known Member

    Messages:
    354
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    #4
    Using Dreamweaver, i am tryin to append my recordset depending on the username but i am getting the following 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 'LIKE '%user1%'' at line 1
     
    yho_o, Mar 25, 2012 IP
  5. Grit.

    Grit. Well-Known Member

    Messages:
    1,424
    Likes Received:
    22
    Best Answers:
    1
    Trophy Points:
    110
    #5
    I would recommend that you print out the SQL query via your function as it would be parsed via PHP. So have it get your SQL, but instead of running it, make it a string, and print it to your browser. This way, you can see if it's being modified by your hosting in any way, and if it appears fine, test it via PHPMyAdmin, on your hosting, or local environment.

    If all else fails, generate your SQL via PHPMyAdmin, and adjust it in your function.
     
    Grit., Mar 25, 2012 IP
  6. Grit.

    Grit. Well-Known Member

    Messages:
    1,424
    Likes Received:
    22
    Best Answers:
    1
    Trophy Points:
    110
    #6
    It also looks like there are no spaces in your SQL either...

    $query_rsTestSim = "SELECT information.custId, information.Username FROM information";
    if (isset($_POST['InfoID'])) {
    $Info = mysql_real_escape_string($_POST['InfoID']);
    $query_rsTestSim.= "WHERE custId LIKE '%$Info%'";
    }
    else if(isset($_SESSION['MM_Username'])){
    $logUser = mysql_real_escape_string($_SESSION['MM_Username']);
    $query_rsTestSim.= "WHERE Username LIKE '%$logUser%'";

    }$rsTestSim = mysql_query($query_rsTestSim, $PropertyConnect) or die(mysql_error());
    $row_rsTestSim = mysql_fetch_assoc($rsTestSim);
    $totalRows_rsTestSim = mysql_num_rows($rsTestSim);

    So it seems like this will print out

    SELECT information.custId, information.Username FROM informationWHERE custId LIKE '%$Info%'

    but just double check it first by printing it to your browser as I suggested
     
    Grit., Mar 25, 2012 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    append as follows: "WHERE Username LIKE '%".$logUser."%'"; and see if it works
     
    PoPSiCLe, Mar 27, 2012 IP