Reputation: 13
So I want to get some data from mysql and use it to create some conditions. Here is my code, but it only shows the last record from mysql.
$result = mysql_query("SELECT * FROM system") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
$name_system = $row['name'];
$value_system = $row['value'];
}
if($name_system=='website_register' AND $value_system==1)
$register_system = 1;
else
$register_system = 0;
if($name_system=='website_offline' AND $value_system==1)
$offline_system = 1;
else
$offline_system = 0;
Then if I'm trying to echo "$offline_system" or "$register_system" in another page, it dosen't show the true data.
Upvotes: 1
Views: 118
Reputation: 9857
You could make this even simpler. If you are looking for specific values from the database you can just query for them and then assign the values.
e.g.
<?php
$registerResult = mysql_fetch_row(mysql_query("SELECT value FROM system WHERE name = 'website_register'"));
$offlineResult = mysql_fetch_row(mysql_query("SELECT value FROM system WHERE name = 'website_offline'"));
$registerSystem = ($registerResult[0] ? 1 : 0);
$offlineSystem = ($offlineResult[0] ? 1 : 0);
?>
Upvotes: 0
Reputation: 5239
use $_SESSION
variables, like this:
wherever you got $register_system
, it becomes $_SESSION['register_system']
, and it can be accessed on the next page as $_SESSION['register_system']
.
Similarly for $offline_system
, it would be $_SESSION['offline_system']
and accessed as $_SESSION['offline_system']
on the next page.
Upvotes: 0
Reputation: 14532
If you want the data to be available in multiple pages you need to store it somewhere. SESSION if the data si small SQLITE or a flat file if it is bigger In situations like this you need to see how much memory and time each option uses and chose the one least expensive.
Upvotes: 0
Reputation: 5389
You closed the while loop after $value_system
. This means all records will be fetched and stored in $name_system
and $value_system
but the loop will overwrite the previous variable so only the last record is stored in that variable.
This seems to be an incomplete code. What you can do is move that closing brace further down the end of the procedure you are expecting to do for each record.
$result = mysql_query("SELECT * FROM system") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
$name_system = $row['name'];
$value_system = $row['value'];
} // you closed the WHILE loop here.
if($name_system=='website_register' AND $value_system==1)
$register_system = 1;
else
$register_system = 0;
if($name_system=='website_offline' AND $value_system==1)
$offline_system = 1;
else
$offline_system = 0;
I imagine your code to be something like this:
$result = mysql_query("SELECT * FROM system") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
$name_system = $row['name'];
$value_system = $row['value'];
if($name_system=='website_register' AND $value_system==1)
$register_system = 1;
else
$register_system = 0;
if($name_system=='website_offline' AND $value_system==1)
$offline_system = 1;
else
$offline_system = 0;
// my imagination i was talking about:
if ($offline_system) echo 'The system is offline<br />';
else echo 'The system is online<br />';
if ($register_system) echo 'The registration is online<br />';
else echo 'The registration system is offline';
} // curly brace MOVED.
Upvotes: 2