Bir veritabanındaki tüm verileri ayıklamak

3 Cevap php

Hey, ben bir sayfada (users.php) bir tablo üzerine bir veritabanındaki bir tablodan veri ayıklamak için nasıl merak ediyorum

For example: I want to be able to get all of the usernames and all the id's from my database onto a table. So if I have in my database:

1 - Fred

2 - Frank

3 - Margret

Ben veritabanında onları kullanıcı ve id olduğunu görmek ve bir masanın üzerine onları basacaktır.

Any help would be great, Thanks.

3 Cevap

Veritabanınıza bağlayın. Sunucu localhost gibi, konumu kendi bilgisayarınıza ya da kod olarak aynı sunucuda eğer. Kullanıcı ve Şifre kendini açıklayıcıdır.

mysql_connect("host", "user", "pass");

Erişmek istediğiniz veritabanının adı.

mysql_select_db("database");

Gerçek mysql sorgu.

$result = mysql_query('SELECT `User_Name`, `User_ID` FROM TABLE');

Bir diziye bu sıralamayı

while($temp = mysql_fetch_array($result)
{
    $id = $temp['User_ID'];
    $array[$id]['User_ID'] = $id;
    $array[$id]['User_Name'] = $temp['User_Name'];
}

Bir tabloya dizi çevirin. (Bu son adımı atlayın ve sağ bu birine gidebiliriz.

$html ='<table><tr><td>User ID</td><td>User Name</td></tr>';
foreach($array as $id => $info)
{
    $html .= '<tr><td>'.$info['User_ID'].'</td><td>'.$info['User_Name'].'</td></tr>';
}
echo $html . '</table>';

Yoksa, istediğin biçimlendirme

$html ='User Id - User Name';
foreach($array as $id => $info)
{
    $html .= $info['User_ID'].' - '.$info['User_Name'].'<br>';
}
echo $html;

, - (For this answer, I will use the mysqli uzantısı da PDO kullanmak istiyor olabilir; ) mysql uzantısı eski ve yeni uygulamalar için kullanılmaması gerektiğini unutmayın


You first have to connect to your database, using mysqli_connect (And you should test if the connection worked, with mysqli_connect_errno and/or mysqli_connect_error).

Daha sonra, mysqli_select_db ile, çalışmak istediğiniz veritabanı ile specifiy gerekecek.


Now, you can send an SQL query that will select all data from your users, with
mysqli_query (And you can check for errors with mysqli_error and/or mysqli_errno).

SQL sorgusu büyük olasılıkla böyle bir şey gibi görünecektir:

select  id, name
from your_user_table
order by name


And, now, you can fetch the data, using something like mysqli_fetch_assoc -- or some other function that works the same way, but can fetch data in some other form.

Verilerinizi getirilen sonra, bunları kullanabilirsiniz - örneğin, ekran için.


Read the pages of the manual I linked to : many of them include examples, that will allow you to learn more, especially about the way those functions should be used ;-)

^ ^ Ülkeler kullanıcıların insteand ile, ama fikir oldukça aynıdır - Örneğin, mysqli_fetch_assoc , istediğiniz yapar tam olarak ne bunun sayfasında tam bir örnek var

Sen (yerleşik PHP MySQL fonksiyonları kullanarak) aşağıdaki gibi bir şey yapabilirsiniz:

// assuming here you have already connected to the database

$query = "SELECT id,username FROM users";
$result = mysql_query($query, $db);
while ($row = mysql_fetch_array($result))
{
  print $row["id"] . " - " . $row["username"] . "\n";
}

(örneğin) size verecektir:

1 - Fred
2 - Frank
3 - Margret

Ben baskı beyanı koyduk nereye, örn vb standart HTML kullanarak bir tabloya onu oraya koymak gibi hissediyorum ne yapabilirim