What's wrong with this string?

Discussion in 'PHP' started by mynameisdi, Mar 19, 2009.

  1. #1
    
    SELECT * FROM video WHERE vid={$item_arr[$i]['id']} 
    
    Code (markup):
    So in the same document I have this - <?=$item_arr[$i]['id']?> - it works perfectly fine when it's not in a php query

    But that string above is in php, so how do I convert this ={$item_arr[$i]['id']} into a MYSQL PHP query?

    Thanks in advance
     
    mynameisdi, Mar 19, 2009 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    First: both of those statements are in PHP - the second one does not contain a MySQL query, the first one does.

    try as follows:
    SELECT * FROM video WHERE vid='$item_arr[$i][id]'
     
    PoPSiCLe, Mar 19, 2009 IP
  3. mynameisdi

    mynameisdi Banned

    Messages:
    977
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the help, but it doesn't seem to work either.

    What else can I try?
     
    mynameisdi, Mar 19, 2009 IP
  4. steelaz

    steelaz Peon

    Messages:
    47
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Can you post slightly large portion of your code?
     
    steelaz, Mar 19, 2009 IP
  5. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #5
    $vid = $item_arr[$i][id];
    SELECT * FROM video WHERE vid='$vid'

    should work . cheat way to do it though.
    the syntax you need for the statement to work as is:
    SELECT * FROM video WHERE vid='".$item_arr[$i][id]."'

    I believe is correct. It should at the least put you on the right path.
     
    shallowink, Mar 19, 2009 IP
  6. abmathur

    abmathur Member

    Messages:
    211
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #6
    I don't know your complete script but why don't you put it in variable and then call fetch.

    $data = mysql_query("SELECT * FROM video WHERE vid=$item_arr[$i]['id']") ;
    $result = mysql_fetch_array($data);

    & than echo your results.
    use print_r($result) for list of arrays.
     
    abmathur, Mar 19, 2009 IP
  7. mynameisdi

    mynameisdi Banned

    Messages:
    977
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Strange, doesn't work either.
     
    mynameisdi, Mar 19, 2009 IP
  8. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #8
    not really, odds are you didn't copy it over exactly. which is why I avoid using the second syntax. if it didn't work with reassignment to a variable. did you echo out the variable to see what value it held? after that will be put the SQL into a string and echo it out until you eliminate any possible error.
     
    shallowink, Mar 19, 2009 IP
  9. Ilyes

    Ilyes Banned

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    To see what problem , do that:
    
    $sql = "SELECT * FROM video WHERE vid=$item_arr[$i]['id']" ;
    echo $sql;
    $data = mysql_query($sql) ;
    $result = mysql_fetch_array($data);
    
    PHP:
    and see what it echo's into screen, tell us what it echo's and what your are waiting.
     
    Ilyes, Mar 19, 2009 IP
  10. mynameisdi

    mynameisdi Banned

    Messages:
    977
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Well, this is strange.

    None of the strings that are previously mentioned in the same document seem to work via echo :(
     
    mynameisdi, Mar 19, 2009 IP
  11. Devant

    Devant Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Hmm... I think that somewhere in your code, you are either nullifying or unsetting the table.

    Putting

    echo $item_arr[$i]['id'];
    PHP:
    does show the proper value?
     
    Devant, Mar 19, 2009 IP
  12. abmathur

    abmathur Member

    Messages:
    211
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #12
    If you could provide a better part of code it would help finding the problem.
     
    abmathur, Mar 19, 2009 IP
  13. mynameisdi

    mynameisdi Banned

    Messages:
    977
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #13
    
    <?switch($vars[1]){case'id':?>
    	<div class="menuLightHeader">
    		<div class="l"><?=$text['addBy']?>: <a href="/user/<?=$item_arr['varr'][0]['uid']?>"><?=$item_arr['varr'][0]['username']?></a></div>
    		<div class="r"><?if((isLogined) && ($_SESSION['user']['id'] == $item_arr['varr'][0]['uid'])){?><a href="/video/del/<?=$item_arr['varr'][0]['id']?>" onClick="if(!confirm('<?=$text['sure']?>?')) return false;"><?=$text['delete']?></a><?}?></div>
    		<div class="clear"></div>
    	</div>
    	<div id="padder">
    
    		<table class="simpleTable" width="100%" cellspacing="0" cellpadding="0">
    			<tr class="selected">
    			<td><a href="<?=(($additional_arr['links']['global']['prev'] > 0) ? '/video/id/'.$additional_arr['links']['global']['prev'] : '#')?>">&laquo; <?=$text['prev']?></a></td>
    			<td align="center"><div class="menuHeader" style="text-align: center;"><?=$boxNames['video']?> &raquo;&nbsp;&raquo; <?=$text['all']?></div></td>
    			<td align="right"><a href="<?=(($additional_arr['links']['global']['next'] > 0) ? '/video/id/'.$additional_arr['links']['global']['next'] : '#')?>"><?=$text['next']?> &raquo;</a></td>
    			</tr>
    		</table>
    
    		<center>
    			<p id="player"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</p>
    			<script type="text/javascript">
    				var s2 = new SWFObject("/flvplayer.swf","single","500","400","7");
    				s2.addParam("allowfullscreen","false");
    				s2.addVariable("file","/=/<?=$item_arr['varr'][0]['filename']?>.flv");
    				s2.addVariable('linkfromdisplay','true');
    				s2.addVariable("displayheight","400");
    				s2.addVariable("backcolor","0x000000");
    				s2.addVariable("frontcolor","0xCCCCCC");
    				s2.addVariable("lightcolor","0x557722");
    				s2.addVariable("autostart","true");
    				s2.addVariable("width","500");
    				s2.addVariable("height","400");
    				s2.write("player");
    			</script>
    		</center>
    		
    		<?if(($additional_arr['links']['user']['next'] > 0) || ($additional_arr['links']['user']['prev'] > 0)){?>
    		<table class="simpleTable" width="100%" cellspacing="0" cellpadding="0">
    			<tr class="selected">
    			<td><a href="<?=(($additional_arr['links']['user']['prev'] > 0) ? '/video/id/'.$additional_arr['links']['user']['prev'] : '#')?>">&laquo; <?=$text['prev']?></a></td>
    			<td align="center"><div class="menuHeader" style="text-align: center;"><?=$boxNames['video']?> &raquo;&nbsp;&raquo; <?=$item_arr['varr'][0]['username']?></div></td>
    			<td align="right"><a href="<?=(($additional_arr['links']['user']['next'] > 0) ? '/video/id/'.$additional_arr['links']['user']['next'] : '#')?>"><?=$text['next']?> &raquo;</a></td>
    			</tr>
    		</table>
    		<?}?>
    
    	</div>
    
    echo $item_arr[$i]['id'];
    
    <?php
    
    
    
    // Create a connection to your database.
    
    // Query database and select the last 10 entries.
    $data = mysql_query("SELECT * FROM (SELECT a.username, a.id, b.vid, b.timestamp, b.text, b.uid  FROM users a INNER JOIN video_comments b WHERE a.id = b.uid) AS ttbl WHERE vid='.$id.' ORDER by timestamp ASC");
    while($row = mysql_fetch_array($data))
    
    {
    echo"
    
    <div class='menuLightHeader'>
    		
    			<div class='l'>: <a href=/user/".$row[id].">".$row[username]."</a></div>
    			
    			<div class='clear'></div>
    		</div>
    		<div class='menuComment'>
    		".$row[text]."
    		</div>
    </div>
    
    
    
    
    ";
    
    }
    
    ?>
    
    Code (markup):
    Here's the full code, but doesn't include the module itself.

    Any thoughts?
     
    mynameisdi, Mar 20, 2009 IP
  14. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #14
    Your syntax at mysql query is wrong + $id is empty.

    1. vid='.$id.' should be vid='$id' or vid=".$id." - because don't forget your mysql_query("") uses double quotes , not single quote

    2. $id is empty. You should add this line before the query: $id = $item_arr[$i]['id'];

    This should work. I never look at other part except the sql part.

    <?switch($vars[1]){case'id':?>
    	<div class="menuLightHeader">
    		<div class="l"><?=$text['addBy']?>: <a href="/user/<?=$item_arr['varr'][0]['uid']?>"><?=$item_arr['varr'][0]['username']?></a></div>
    		<div class="r"><?if((isLogined) && ($_SESSION['user']['id'] == $item_arr['varr'][0]['uid'])){?><a href="/video/del/<?=$item_arr['varr'][0]['id']?>" onClick="if(!confirm('<?=$text['sure']?>?')) return false;"><?=$text['delete']?></a><?}?></div>
    		<div class="clear"></div>
    	</div>
    	<div id="padder">
    
    		<table class="simpleTable" width="100%" cellspacing="0" cellpadding="0">
    			<tr class="selected">
    			<td><a href="<?=(($additional_arr['links']['global']['prev'] > 0) ? '/video/id/'.$additional_arr['links']['global']['prev'] : '#')?>">&laquo; <?=$text['prev']?></a></td>
    			<td align="center"><div class="menuHeader" style="text-align: center;"><?=$boxNames['video']?> &raquo;&nbsp;&raquo; <?=$text['all']?></div></td>
    			<td align="right"><a href="<?=(($additional_arr['links']['global']['next'] > 0) ? '/video/id/'.$additional_arr['links']['global']['next'] : '#')?>"><?=$text['next']?> &raquo;</a></td>
    			</tr>
    		</table>
    
    		<center>
    			<p id="player"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</p>
    			<script type="text/javascript">
    				var s2 = new SWFObject("/flvplayer.swf","single","500","400","7");
    				s2.addParam("allowfullscreen","false");
    				s2.addVariable("file","/=/<?=$item_arr['varr'][0]['filename']?>.flv");
    				s2.addVariable('linkfromdisplay','true');
    				s2.addVariable("displayheight","400");
    				s2.addVariable("backcolor","0x000000");
    				s2.addVariable("frontcolor","0xCCCCCC");
    				s2.addVariable("lightcolor","0x557722");
    				s2.addVariable("autostart","true");
    				s2.addVariable("width","500");
    				s2.addVariable("height","400");
    				s2.write("player");
    			</script>
    		</center>
    		
    		<?if(($additional_arr['links']['user']['next'] > 0) || ($additional_arr['links']['user']['prev'] > 0)){?>
    		<table class="simpleTable" width="100%" cellspacing="0" cellpadding="0">
    			<tr class="selected">
    			<td><a href="<?=(($additional_arr['links']['user']['prev'] > 0) ? '/video/id/'.$additional_arr['links']['user']['prev'] : '#')?>">&laquo; <?=$text['prev']?></a></td>
    			<td align="center"><div class="menuHeader" style="text-align: center;"><?=$boxNames['video']?> &raquo;&nbsp;&raquo; <?=$item_arr['varr'][0]['username']?></div></td>
    			<td align="right"><a href="<?=(($additional_arr['links']['user']['next'] > 0) ? '/video/id/'.$additional_arr['links']['user']['next'] : '#')?>"><?=$text['next']?> &raquo;</a></td>
    			</tr>
    		</table>
    		<?}?>
    
    	</div>
    
    echo $item_arr[$i]['id'];
    
    <?php
    
    
    
    // Create a connection to your database.
    $id = $item_arr[$i]['id'];
    // Query database and select the last 10 entries.
    $data = mysql_query("SELECT * FROM (SELECT a.username, a.id, b.vid, b.timestamp, b.text, b.uid  FROM users a INNER JOIN video_comments b WHERE a.id = b.uid) AS ttbl WHERE vid='$id' ORDER by timestamp ASC");
    while($row = mysql_fetch_array($data))
    
    {
    echo"
    
    <div class='menuLightHeader'>
    		
    			<div class='l'>: <a href=/user/".$row[id].">".$row[username]."</a></div>
    			
    			<div class='clear'></div>
    		</div>
    		<div class='menuComment'>
    		".$row[text]."
    		</div>
    </div>
    
    
    
    
    ";
    
    }
    
    ?>
    PHP:
     
    ads2help, Mar 20, 2009 IP
  15. mynameisdi

    mynameisdi Banned

    Messages:
    977
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Yup, placing it outside of the query did help.

    Thanks a lot!
     
    mynameisdi, Mar 20, 2009 IP
  16. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #16
    You're welcomed.

    Always check if the variable has been assigned a value either by
    1. Search using your text/php editor
    2. echo()ing its value

    Good luck.

    - ads2help
     
    ads2help, Mar 20, 2009 IP