I want retrieve data from mysql database and store in an array. Later I would display the data in html.
Below is my code snippet:
select co_no from faculty_co where course_code='$course1_code';
The output will display a total 5 co_no values. I want to store this values in an array and display as dropdown menu in html using select tag.
I am new to php. Please tell me how do I retrieve and store it.
I'm 'old school', and not much of a programmer, so I tend to do things procedurally.
I do it something like this:
include('path/to/connection/stateme.nts'); // the connection is a variable '$db'
$query = "my query";
$result = mysqli_query($db,$query);
$rows = array(); //initialise array
while($row = mysqli_fetch_assoc($result)){
$rows[] = $row;
}
Now you can store that as a json_encoded string or just retain it as an array for later use.
First off, php.net has a lot of documentation on mysql and other things and is pretty easy to understand.
Check out the documentation and examples here.
Related
I am having a problem.
I know I am using deprecated MySQL, but I am working on a website for school and security is not necessary(as long as it works). I am trying to get all the Id's from a table called Reviews. This should be stored in a array. For some reason it only shows the first one. How to get all of them stored in 1 array? This is my code which is not working:
$sql1 = "SELECT Klanten_id FROM Reviews";
$res1 = mysql_query($sql1);
$r = mysql_fetch_assoc($res1)['Klanten_id'];
1.Don't use outdated mysql_* library.Turn to mysqli_* or PDO.
2.mysql_fetch_assoc: Fetch a result row as an associative array. So you need to apply loop to get all data
Like below:-
$ids = array(); //created an array
while ($row = mysql_fetch_assoc($res1)) {
$ids[] = $row['Klanten_id']; // assign each id to the array
}
print_r($ids);// print array
3.To start working on mysqli/PDO check basic example here:- mysqli/PDO example
So i have this so far..
if(isset($_POST['Decrypt']))
{
$dbinary = strtoupper($_POST['user2']);
$sqlvalue = "SELECT `value` FROM `license` WHERE `binary` = '$dbinary'";
$dvalue = mysql_query($sqlvalue) or die(mysql_error());
$dvalue = mysql_fetch_array($dvalue);
$dvalue['value'];
}
I have a field where the user enters a binary code which was encrypted. (The encrypt part works). This is supposed to retrieve the value from the database. When ever i do it, instead of the value showing up, it says "Array".
Please help me out.
This is because you can't just echo an array. You need to use functions like var_dump(); or print_r();
looks like you have more than one row in your table matching criteria. Try using while loop to retrieve data.
while($row = mysql_fetch_assoc($dvalue)){
//$row['value'];
}
I'm trying to create a while loop in PHP which retrieves data from a database and puts it into an array. This while loop should only work until the array its filling contains a certain value.
Is there a way to scan through the array and look for the value while the loop is still busy?
to put it bluntly;
$array = array();
$sql = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_array($sql)){
//Do stuff
//add it to the array
while($array !=) //<-- I need to check the array here
}
You can use in_array function and break statement to check if value is in array and then stop looping.
First off, I think it'd be easier to check what you're filling the array with instead of checking the array itself. As the filled array grows, searching it will take longer and longer. Insted, consider:
$array = array_merge($array, $row);
if (in_array('ThisisWhatIneed', $row)
{
break;//leaves the while-loop
}
However, if you're query is returning more data, consider changing it to return what you need, only process the data that needs to be processed, otherwise, you might as well end up with code that does something like:
$stmt = $db->query('SELECT * FROM tbl');
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
if ($row['dataField'] === 'username')
{
$user = $row;
break;
}
}
WHERE could help a lot here, don't you think? As well taking advantage of MySQL's specific SELECT syntax, as in SELECT fields, you, need FROM table, which is more efficient.
You may also have noticed that the code above uses PDO, not mysql_*. Why? Simply because the mysql_* extension Is deprecated and should not be used anymore
Read what the red-warning-boxes tell you on every mysql* page. They're not just there to add some colour, and to liven things up. They are genuine wanrings.
Why don't you just check each value when it gets inserted into the array? It is much more efficient than iterating over the whole array each time you want to check.
$array = array();
$stopValue = ...
$sql = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_assoc($sql)){
array_push($array,$row['column']);
if($row['column'] == $stopValue){
// The array now contains the stop value
break;
}
I have a query that is sent to my SQL database from PHP. It looks something like this:
$result = mysql_query("SELECT ...");
while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
print json_encode($rows);
I ultimately return the json_encoded result to my Java application (Android, actually) so that I can pick apart the pieces and put them into a nice Java object.
I have come to a point where I need some more data to come from this query, and I don't know how to get it. Is there a way for me to give myself the results of ANOTHER query, and inject it into this json_encoded result? Like basically run another query, and append it to the $r on each iteration of the loop and have it "seem" as if it was another column returned from the original queries select? I just don't know how to handle this $rows[] array.
Let me know if my question is not clear.
Add mutiple levels to your PHP array:
$data = array();
$data['part1'] = 'data from query 1';
$data['part2'] = 'data from query 2';
echo json_encode($data);
then simply refer to those sub-arrays in your client-side code.
I continue to struggle with array! This is probably easy to answer.
I'm retrieving a data set from MYSQL w/ PHP. I get an array that has the 1st row (ala the mysql_fetch_array). Typically I would just loop through this and get each value, but in this case I'm already in the middle of a loop and I need to find out if a particular value exists in the full data set (which will be more than 1 row).
I figured I could just loop through and put all the values into an array with something like:
$query = "SELECT MapId FROM Map Where GameId = $gl_game_id";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
$map_set = array();
while($row = mysql_fetch_assoc($result_set)) {
$map_set[] = $row;
}
When I print_r this, I do in fact get the full data set (e.g. all rows of MapId from table Map).
So now, when I go to look in there and see if a value (that is coming out of this other loop) exists, it won't find it.
So my code looks like:
if (in_array($i, $map_set)) {
echo "yes, it's there baby!";
}
But it doesn't work. I tried hard coding the array, and that does in fact work. So there is simply something wrong with the way I'm constructing my array that this function doesn't like it.
if (in_array($i, array(40,12,53,65))) {
echo "yes, it's there baby!";
}
arrrg... I do hate being a noobie at this.
Function mysql_fetch_assoc returned array.
If you make print_r($map_set) then you will see that is 2-dimension array. Sure in_array not worked.
Just replace $map_set[] = $row; by $map_set[] = $row["MapId"]; and then try again.