Low cost ecommerce web development India flash website design
How do I fetch data from a table?
$sql = "SELECT * FROM articles ORDER BY title";
It’s handy to keep it in a separate variable, as when we get into writing
more
complex queries and something goes wrong, we can double-check our query
with this one-liner:
echo $sql;
2. Next, tell MySQL to perform the query:
$queryResource = mysql_query($sql, $dbConn);
This can be confusing at first. When you tell MySQL to perform a query, it
doesn’t immediately give you back the results. Instead, it holds the results
in memory until you tell it what to do next. PHP keeps track of the results
with a resource identifier, which is what you get back from the mysql_query
function. In the code above, we’ve stored the identifier in $queryResource.
3. Finally, use mysql_fetch_array to fetch one row at time from the set of
results:
while ($row = mysql_fetch_array($queryResource, MYSQL_ASSOC))
This places each row of the results in turn in the variable $row. Each of these
rows will be represented by an array. By using the additional argument
MYSQL_ASSOC with mysql_fetch_array, we’ve told the function to give us
an array in which the keys correspond to column names in the table. If you
omit the MYSQL_ASSOC argument, each column will appear twice in the array:
once with a numerical index (i.e. $row[0], $row[1], etc.), and once with a
string index (i.e. $row['title'], $row['author'], etc.). While this doesn’t
usually cause a problem, specifying the type of array value you want will
speed things up slightly.
Using a while loop, as shown above, is a common way to process each row of
the result set in turn. The loop effectively says, “Keep fetching rows
from MySQL
until I can’t get any more”, with the body of the loop processing
the rows as
they’re fetched.
website designer freelance ASP PHP ecommerce web developer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110