SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help in a query error

    I am getting the following error

    Error in query: SELECT * FROM menu WHERE id = LIMIT 1 ;. 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 'LIMIT 1' at line 1

    PHP Code:
    if(isset($_GET['id']))
    {
        
    $id $_GET['id'];
    }
    else
    {
        
    $id ""//You give it its default value here.
    }  

    $query 'SELECT * FROM menu WHERE id = '.$id.'  LIMIT 1 ;'
    // execute query 
    $result mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

    // see if any rows were returned 
    if (mysql_num_rows($result) > 0) { 

  2. #2
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like the ID isn't being provided in the URL. It needs to be something like:

    Code:
    yourpage.php?id=1
    If you don't have a default, you should probably remove that else block, and throw an error instead, or reidrect back to another page.

    Also you better use intval() on that value in the MySQL query, so there's no risk of SQL injection.

  3. #3
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you are right the ID isn't being provided in the URL. it is coming up like this

    i took out the the else statement and put the intval function before the value like this, but still.

    itemdetails.php
    PHP Code:
    if( isset($_GET['id']))
    {
        
    $id $_GET['id'];
    }


    $query 'SELECT * FROM menu WHERE id = '.intval($id). '  LIMIT 1 ;'

    // execute query 
    $result mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

    // see if any rows were returned 
    if (mysql_num_rows($result) > 0) { 
    echo 
    '<table width="100%"  border="0" cellspacing="0" cellpadding="0" class="itemdetails">
    <tr>
    <td width="1100" height="417" bgcolor="#FFFFFF" class="tento"> 

    The url in itemdetails.php is coming from a page called menu2prueba.php

    menu2prueba.php
    PHP Code:
    <?php 


    $query 
    "SELECT * FROM menu WHERE id IN ( 1, 2, 3, 4)";  

    // execute query 
    $result mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

    // see if any rows were returned 
    if (mysql_num_rows($result) > 0) { 
        
    // yes 
        // print them one after another 
       
    echo '<table  class="felo">'
       echo 
    "<tr>";
    echo
    '<td width="800" height="100%" bgcolor="#FFFFFF" class="quelo">'

    while(
    $row3 mysql_fetch_row($result)) { 
    echo 
    '
    <table class="trelo" >
     <tr>
     <td width="152" rowspan="2" class="cebo">
     
     <h3 align="center"><a href="itemdetails.php?id='
    .$row3['id'].'">
    something should be wrong in the menu2page.php that it's not passing the id to the url in itemdetails.php.

    what could that be help please.

    it is returning false instead of

    yourpage.php?id=1

  4. #4
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you use mysql_fetch_row(), it indexes the array numerically. So you should either change $row3['id'] to $row3[0], or change your call to mysql_fetch_row() to mysql_fetch_assoc().

  5. #5
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would like to change the call but only on the <h3> tags to locate the respective id set by a selection made by the user and pass it to URL. then from there continue with the mysql_fetch_row already assinged as you see in the script I have done several tries but with not sucess.

  6. #6
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by co.ador View Post
    I would like to change the call but only on the <h3> tags and then from there continue with the mysql_fetch_row already assinged as you see in the script I have done several tries but with not sucess.
    I don't understand. Why do you want to switch back and forth? Or perhaps I am misunderstanding.

    Either change mysql_fetch_row() to mysql_fetch_assoc(), or change $row3['id'] to $row3[0].

  7. #7
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use mysql_fetch_row to print fields in the first page before I link to itemdetail.php that's why I have been killing my head to fit mysql_fetch_assoc to pass the I'd value by association instead of by fields $row3['id']

  8. #8
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by co.ador View Post
    I use mysql_fetch_row to print fields in the first page before I link to itemdetail.php that's why I have been killing my head to fit mysql_fetch_assoc to pass the I'd value by association instead of by fields $row3['id']
    I still don't understand.

    If you use mysql_fetch_row in that while loop, then just use all indices in that loop, like $row3[0]. If you use mysql_fetch_assoc in that while loop, then use all associative keys.

    There's mysql_fetch_array, but I really don't recommend it as it just wastes elements. I would choose one or the other and be consistent, within the same loop.

  9. #9
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    problem solved I was confused with the id variable in the url and the value of the id in the database some how I thought the variable in the url was the key of the key=>value pair of the array. But now I assigned the id='.$row[1] .' " indexing the array numerically instead. I didn't undertood that term. by the way in the database the id field is in the array 1 not in the 0. I don't know why I assigned in there I think lack of experienced. does the id column is mostly located in the array [0]?

    Now the url string is
    Now I am getting an error in the itemdetails.php which is the second page where the user is linked to.

    Notice: Uninitialized string offset: 3 in C:\wamp\www\restaurants\itemdetails.php on line 25
    itemdetails.php
    PHP Code:
    <?php 
    if( isset($_GET['id']))
    {
        
    $id $_GET['id'];
    }

    $query 'SELECT * FROM menu WHERE id = '.intval($id). '  LIMIT 1 ;'

    // execute query 
    $result mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

    // see if any rows were returned 
    if (mysql_num_rows($result) > 0) { 
    echo 
    '<table width="100%"  border="0" cellspacing="0" cellpadding="0" class="itemdetails">
    <tr>
    <td width="1100" height="417" bgcolor="#FFFFFF" class="tento">

    <table class="cafe"><tr><td width="547">

    <a href="#"><h3 align="justify" style="position:relative; top: 10px;">'
    ,$id[3] ,'</h3></a>
    </td>
    </tr>
    </table>

    <table width="1215" height="609" class="chencho" >

     <td class="cheche" rowspan="8" align="center">
    <a href="#"><img src="../images/default.jpg" width="270" height="160" alt="coloe"/></a></td>
    <td width="544" rowspan="8" >&nbsp;</td>
    <tr>
      <td width="224" height="40" rowspan="3"><strong>Details:</strong></td>
    </tr>
    <tr>
      <td width="106" height="28"><img src="../images/add to Car.gif" alt="df" width="99" height="28" /></td>
    </tr><tr>
      <td height="25"><img src="../images/viewcart.gif" alt="rt" width="99" height="28" /></td>
    </tr>
    <tr>
      <td width="224" height="29"><ul>
        <li>coloso mentiroso</li>
      </ul></td>
    </tr>
    <tr>
      <td width="224" height="29"><ul>


    </td>
    </tr>
    </table>'
    ;

    }

  10. #10
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes usually id is the first column of any table, therefore would be in index 0.

    Why are you trying to call $id[3]? $id is an integer.

    I think what you want to do is call:

    PHP Code:
    $row mysql_fetch_row($result); 
    Before your table, and then use:

    PHP Code:
    $row[3

  11. #11
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That will fetch the row found in result and then call it's index $row[3].

    Must of the time a some kind of loop is used before the $row=mysql_fetch_row function and to call just one row is not as common. Now I see how one row is call
    Last edited by co.ador; Jun 29, 2009 at 13:07.

  12. #12
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you devbanana...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •