简体   繁体   English

选择带有变量的语句,但不起作用

[英]Select statement with variables, but not work

i try to select statement with variable, but not work maybe i miss something. 我尝试选择带有变量的语句,但不起作用,也许我错过了一些东西。

<!--Get data from user--> 
    <?php while ($row_settings = mysql_fetch_array($rs_settings4)) {
            $get_id_friend=$row_settings['friend_id'];
    } ?>
<!--Get data from user--> 


  <div id="wrap-box"><!--Loop-box-->
<?php $results  = mysql_query("select * from users where id='$get_id_friend'");  ?>  
  <?php while ($row_friend_loop = mysql_fetch_array($results)) {?>
        <div class="img">
            <img src="<?php echo $row_friend_loop['img']; ?>" width="49"/>
        </div>

        <div id="request">
            <div class="name">
            <p><?php echo $row_friend_loop['user_name']; ?></p>
            </div>
            <div class="btn">
            <a href="#"><img src="images/btn-confirm.jpg" class="hover" /></a><a href="#">
            <img src="images/btn-ignore.jpg" class="hover" /></a>
            <div class="clear"></div>
            </div>
        </div>
       <?php } ?><!--Loop-box-->

i try to change from 我尝试从

where id='$get_id_friend'"); 

to

where id='.$get_id_friend.'"); 

but still not work, sorry i'm still newbie here 但仍然无法正常工作,对不起,我还是这里的新手

first i want get id from table friend, and if id from table friend is related with table user , all data with related id will be loop . 首先,我想从表格好友获取ID,如果表格好友的ID与表格用户相关,则所有具有相关ID的数据都将为loop。

I recommend you read up on sql injection and the PDO library. 我建议您阅读有关SQL注入和PDO库的信息。 Also, I have a feeling you do not have a very efficient database structure. 另外,我感觉您没有非常有效的数据库结构。

I'm just guessing on your motive and database here, but you should start with something like this: 我只是在这里猜测您的动机和数据库,但您应该从以下内容开始:

TABLE friends
int id PRIMARY
int user_id
int friend_id

TABLE users
int user_id
VARCHAR(64) img
VARCHAR(24) user_name

This way, one table holds all the user information, and the second table holds all the 'friend' data. 这样,一个表保存了所有用户信息,而第二个表保存了所有“朋友”数据。 The correct way to access this info would be to use prepared statements and then you can loop through the resulting array, ie: the following: 访问此信息的正确方法是使用准备好的语句,然后可以遍历结果数组,即:以下内容:

$sql = "SELECT 
           friends.friend_id AS friend_id,
           users.img AS friend_img,
           users.user_name AS friend_user_name,
        FROM friends
        LEFT JOIN users ON (friend.user_id = users.user_id)
        WHERE friend.user_id = :user_id";
$sth = PDO->prepare($q);
$sth->bindParam(":user_id",$user_id_to_get_friends_for);

try{
     $sth->execute();
     $friends = $sth->fetchAll();
} catch (PDOException $e) {
     //  Do something with the error
}

// Now loop through the array
foreach($friends as $f) {
     echo "<td><img src='".$f['friend_img']."' alt='' />".$f['friend_user_name']."</td>";
}

Now, this is oversimplified i'm sure, but if you follow this format, and do a little research on the functions i'm using here (namely LEFT JOIN and PDO stuff), you should be off to a good start. 现在,我敢肯定这过于简化了,但是如果您遵循这种格式,并对我在这里使用的功能(即LEFT JOIN和PDO东西)进行一些研究,那么您应该是一个好的开始。

I hope this helps you. 我希望这可以帮助你。

Syntactically there's absolutely no difference in how your two versions of building the where clause work - if neither works, then $get_id_friend is not set properly. 从语法$get_id_friend ,构建where子句的两个版本的工作方式绝对没有区别-如果两个版本都不起作用,则$get_id_friend的设置不正确。 Neither of your queries have any error handling, and you don't show the query string for the initial query, only how you fetch data from it. 您的两个查询都没有任何错误处理,并且您不显示初始查询的查询字符串,而仅显示如何从中获取数据。

A proper minimalist 'safe' query structure is: 适当的极简“安全”查询结构为:

$sql = "SELECT ...";
$result = mysql_query($sql);
if ($result === FALSE) {
    die(mysql_error . "<br />$sql");
}

Since your last comment indicates you're looping over multiple user records and displaying data on those users, you'd be better of using a single joined query, rather than an "outer" query that causes repeated inner queries. 由于您的最后一条评论表明您要遍历多个用户记录并在这些用户上显示数据,因此最好使用单个联接的查询,而不要使用会引起内部查询重复的“外部”查询。

Something like this: 像这样:

SELECT friends.img, friends.user_name
FROM users AS friends
LEFT JOIN users ON users.friend_id = users.id
WHERE users = $userID;

That's just a guess, since we have very little information about your table structure. 这只是一个猜测,因为我们几乎没有关于您的表结构的信息。 But basically: given some userID, fetch the images and user_names of all their friends. 但基本上:给定一些userID,获取其所有朋友的图像和用户名。 Now you've reduced your code to: 现在,您已将代码简化为:

$sql = "select query from above";
$result = mysql_query($sql);
if ($result === FALSE) {
    die(mysql_error() . "<br />$sql");
}
while($row = mysql_fetch_assoc($result)) {
    ... your html stuff as before ...
}

One loop, one query, less fuss and muss. 一循环,一查询,大惊小怪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM