why mysqli one pulling one row with foreach loop in php? - mysqli

Hi friends why mysqli one pulling one row with foreach loop in php ? Shouldn't it fetch arrays of all rows ?
$link = new mysqli(DB_SERVER, DB_USER,DB_PASSWORD,DB_NAME) or die('error connecting');
$mob = query("SELECT mobile FROM members_db");
foreach ($mob as $numbers){
$mob_numbers = $numbers['mobile'];
print_r($mob_numbers); exit();
}

You need to do the following
Change:
foreach ($mob as $numbers)
{
$mob_numbers = $numbers['mobile'];
print_r($mob_numbers); exit();
}
To
$results = array();
foreach ($mob as $numbers)
{
$results[] = $numbers['mobile'];
}
print_r($results); exit();

You have to 'Fetch' the records one by one
$mob = query("SELECT mobile FROM members_db");
while($row = mysqli_fetch_array($link, $mob)){
print_r($row['mobile'])
}
Also, not sure why you're calling exit() inside the loop. Doesn't make sense.

remove your exit(); within the loop

Related

Allowing multiple choices instead of just one

I have a code that is used for adding a channel to an IPTV platform!
Currently we have the option to put a channel in single category/genre... What we want to do is instead of this drop box to a have something like checklist or space to write the genres/categories... I have tried everything I remembered I knew, but nothing worked! Thank you
function get_genres() {
global $tv_genre_id;
$genres = Mysql::getInstance()->from('tv_genre')->get()->all();
$option = '';
foreach ($genres as $arr){
$selected = '';
if ($tv_genre_id == $arr['id']){
$selected = 'selected';
}
$option .= "<option value={$arr['id']} $selected>"._($arr['title'])."\n";
}
return $option;
}

Returning an array using PDO

I'm trying to use PDO using the following code, I want to select the array of values for the context_list, but it returns one record, any ideas where I'm going wrong?
try {
$sql2 = "SELECT area_easting, area_northing, context_number FROM excavation.contexts";
$sth = $conn->prepare($sql2);
$sth->execute();
while ($row = $sth->fetch(PDO::FETCH_ASSOC))
{
$easting_list = $row['area_easting'];
$northing_list = $row['area_northing'];
$context_list = $row['context_number'];
}
}
catch(PDOException $e )
{
echo "Error: ".$e;
}
echo "context list: ";
echo $context_list;
A partial solution:
This worked:
$query = $conn->prepare("SELECT area_easting, area_northing, context_number FROM excavation.contexts");
$query->execute();
while($r = $query->fetch(PDO::FETCH_OBJ)){
echo $r->area_easting, '|';
echo $r->area_northing;
echo '<br/>';
}
But Now I need to make the $r->area_easting accessible to the session, but that's another question.
Your query may return several records (provided they actually exist in the table).
Your code loops through all the records (with the while loop) but the values ($easting_list, $northing_list and $context_list) are overwritten in the loop.
I suggest the following changes to your code:
$easting_list = array();
$northing_list = array();
$context_list = array();
try {
$sql2 = "SELECT area_easting, area_northing, context_number FROM excavation.contexts";
$sth = $conn->prepare($sql2);
$sth->execute();
while ($row = $sth->fetch(PDO::FETCH_ASSOC))
{
$easting_list[] = $row['area_easting'];
$northing_list[] = $row['area_northing'];
$context_list[] = $row['context_number'];
}
}
catch(PDOException $e )
{
echo "Error: ".$e;
}
echo "context list: ";
echo implode(', ', $context_list);
Now all the values are stored in 3 arrays. explode is used to print a comma-separated list of all values in $context_list array.

How to display uploaded csv file into tablular format in drupal 7

I have created a form in drupal 7 and it has a field to upload file (csv file only ) now how to display uploaded csv file into table on form submit ?
Not sure about theme function, but you can do it on your own.
I.e. use:
http://php.net/manual/en/function.file-get-contents.php
To read the file and then:
http://php.net/manual/en/function.str-getcsv.php
to parse CSV.
Or, maybe:
http://php.net/manual/en/function.fgetcsv.php
to read row by row. Anyway, you'll end up with looping trough rows so just print values the way you want, add markup around the values...
After go through various code exercises i have come up with solution which is as follows :
`function display_table($filename, $head=false) {
$handle = fopen($filename, "r");
$all_rows = array();
$header = null;
while ($row = fgetcsv($handle)) {
if ($header === null) {
$header = $row;
continue;
}
$all_rows[] = array_combine($header, $row);
}
$table = theme('table', array('header' => $header, 'rows' => $all_rows));
return $table;
}`
Hope it would be helpful to others as well !

Convert a php script to codeigniter 3

Please i am working on a project and need to generate pincode per request. This is the script i have for php but i need to implement codeigniter 3. Also please can i set it in such a way that if i want 10 pin i input 10 and generate 10 into the database and if want 50 i input 50. i am new to codeigniter 3 and i have read the tutorials but doesnt seems to help on this.
Here is my code on php
Please i want to be able to generate base on the number i want per request.
<?php
$connect = mysqli_connect("localhost","hadassa","root", "")
or die("Cannot connect");
$digits = 5;
function gen_ran($digits) {
$n = "";
for ($x = 1; $x <= $digits; $x++) {
$n.=mt_rand(0,9);
}
return $n;
}
function gen_ran2($digits)
{
$n2 = "";
for($a=1; $a<=$digits; $a++)
{
$n2 .=mt_rand(0,9);
}
return $n2;
}
for($a=1; $a<=20; $a++)
{
$codes = gen_ran($digits) . gen_ran2($digits);
$query = "INSERT INTO pin (pin)
VALUES ('$codes')";
$result = mysqli_query($connect, $query)
or die("Error");
if($result)
echo"$codes<br>";
}
?>
<script type="text/javascript">
alert("PIN Generated Successfully");
window.location = "#";
</script>
codeignater use the MVC So
create a controller in controller folder to write logic
create a model in models folder for write database code
in the viewer, folder create a view for the user view code
https://www.youtube.com/watch?v=K71j-rWXejk&index=2&list=PLUpnKy5Si8zDouvZiUMHwSSyVrowJmH22
https://www.youtube.com/watch?v=IOZqRgOgSu4

How to store MySQLi bind_result value for further processing?

In the below code,I can echo $count in bind_result by using while loop(commented in the code). But instead I want to keep that value in the $count variable for further processing instead of just printing the value. How do I hold the value for $count?
/*......mysqli connection...*/
$stmt =$mysqli->prepare("select count(*) from tab where rtd_id=? ");
$stmt->bind_param('i',$id);
$id = 1;
$stmt->execute();
$stmt->bind_result($count);
/*while($stmt->fetch())
{
echo $count;
}*/
if($count>0)
{
//DO SOMETHING
}
?>
First of all, you don't need a loop here.
$stmt->bind_result($count);
$stmt->fetch();
if($count>0)
{
//DO SOMETHING
}