基于php中的Id查询表中的所有行(Query All Rows Within a Table based on Id's in php)

编程入门 行业动态 更新时间:2024-10-28 19:32:54
基于php中的Id查询表中的所有行(Query All Rows Within a Table based on Id's in php)

我的数据库中有一个包含53行的表。 行中的变量是INT id,VARCHAR var和INT votes。 我无法弄清楚如何将表中的所有行查询到数组中。 我希望能够在引用id时获得'var'变量。 这是我到目前为止,它确实只会查询第一行。 (因此id ='1')。

<?php include_once "code.php"; $new_array = array(); $sql = mysql_query("SELECT * FROM Variables WHERE id='1'"); while($row = mysql_fetch_array($sql)){ $new_array[ $row['id']] = $row; };

我尝试使用for循环并将变量放在'1'的位置,但这也不起作用。 如果你还可以请告诉我如何让var变量引用任何非常好的id。 谢谢

I have a table which includes 53 rows, in my database. The variables in the rows are INT id, VARCHAR var, and INT votes. I can't figure out how to query all the rows within the table into an array. I want to be able to get the 'var' variable while referencing the id. Here is what I have so far, which indeed will only query the first row. (hence id = '1').

<?php include_once "code.php"; $new_array = array(); $sql = mysql_query("SELECT * FROM Variables WHERE id='1'"); while($row = mysql_fetch_array($sql)){ $new_array[ $row['id']] = $row; };

I tried using a for loop and sticking a variable where the '1' is, but that's not working either. If you could please also show me how to get the var variable from referencing any of the id's that be great. Thanks

最满意答案

你为每一行声明$new_array 。 你应该把它移到while循环之外。 另外,尽量不要使用mysql_ *函数,因为它们已被弃用。 尝试mysqli_ *甚至更好的PDO。

include_once "code.php"; $sql = mysql_query("SELECT * FROM Variables"); $new_array = array(); //This array should be not inside while while($row = mysql_fetch_array($sql)){ $new_array[$row['id']] = $row; //Do something else here if you want };

使用PDO

try { $link= new PDO("mysql:host=$hostname;dbname=mysql", $username, $password); echo "PDO connection object created"; } catch(PDOException $e) { echo $e->getMessage(); die(); } $sql = "SELECT * FROM users"; //Prepare your query $stmt = $link->prepare($sql); //Execute your query binding variables $result = $stmt->execute(array()); $result = $stmt->fetchAll(); $new_array = array(); foreach ($result as $row) { $new_array[$row['id']] = $row; }

评论后编辑:

从查询中删除WHERE id = '1' ,您将获得所有行。 然后你可以使用id引用的id:

echo $new_array[1]['var']; echo $new_array[2]['var'];

You declare $new_array for each row. You should move it outside the while loop. Also, try not to use mysql_* functions because they are deprecated. Try mysqli_* or even better PDO.

include_once "code.php"; $sql = mysql_query("SELECT * FROM Variables"); $new_array = array(); //This array should be not inside while while($row = mysql_fetch_array($sql)){ $new_array[$row['id']] = $row; //Do something else here if you want };

Using PDO

try { $link= new PDO("mysql:host=$hostname;dbname=mysql", $username, $password); echo "PDO connection object created"; } catch(PDOException $e) { echo $e->getMessage(); die(); } $sql = "SELECT * FROM users"; //Prepare your query $stmt = $link->prepare($sql); //Execute your query binding variables $result = $stmt->execute(array()); $result = $stmt->fetchAll(); $new_array = array(); foreach ($result as $row) { $new_array[$row['id']] = $row; }

Edit after the comment:

Remove the WHERE id = '1' from you query and you will have all your rows. Then you can take the var referenced by id using:

echo $new_array[1]['var']; echo $new_array[2]['var'];

更多推荐

本文发布于:2023-07-07 13:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1063925.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Query   Id   php   based   Table

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!