我的数据库中有一个包含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'];更多推荐
发布评论