I've some issues with an array fetched from MySQL
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
mysql_fetch_row($result);
echo $row[0]; // doesn't work
echo $row[1]; // doesn't work
but this work
echo $row["FirstFieldName"] //OK
...
how should I change the following code to make it work?
for ( $i = 0; $i < count( $row ); $i++ )
{
echo $row[ $i ];
}
thanks
Do below changes. use mysql_fetch_array instead of mysql_fetch_row()
mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
$row=mysql_fetch_array($result);
for ( $i = 0; $i < count( $row ); $i++ )
{
echo $row[ $i ];
}
Try using mysql_fetch_array() instead.
The three functions you should look into are:
mysql_fetch_row,
mysql_fetch_array, and
mysql_fetch_assoc
Each does things a little differently.
Try it this way:
$sql = "SELECT id,email FROM people WHERE id = '42'";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo $row[0];
}
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
$row=mysql_fetch_array($result);
echo $row[0];
echo $row[1];
it will work
mysql_fetch_row returns one row, so to be able to use it you should do :
while( $row = mysql_fetch_row( $result ) )
{
echo $row["email"];
}
Otherwise, as Mike said you should use mysql_fetch_array()
ie.... this takes all the retrieved keys/fields and intereates them and hands them off to $s which can then be placed into html..
foreach($row as $key=>$val){
$$key = $val;
$s.= "<tr><td>".$key."</td><td>
<input type=text size=88 name='".$key."' value='".$val."'></td></tr>";
}
// end your php then onward to html
<table>
<?php echo $s;?>
</table>
Related
I've got some code which randomly fetches data from a database, but whenever I prints the data in the browser is looks like that - ["data"]. I want it to be a clear string. How can I make that? Thank you!
Here is the code:
<?php
require '../wordbeater/adminpanel/includes/dbh.inc.php';
$result=mysqli_query($con, "SELECT * FROM users ORDER BY RAND() LIMIT 6");
$count = 0;
while($row=mysqli_fetch_row($result)){
$postsarray["one.$count."] = $row[1];
$postsarray["two.$count."] = $row[2];
$count++;
}
$encodedArray = array_map(utf8_encode, $postsarray);
echo $encodedArray;
mysqli_close($con);
?>
Try using:
$result=mysqli_query($con, "SELECT * FROM users ORDER BY RAND() LIMIT 6");
$count = 0;
while ($row = $result->fetch_row()) {
$postsarray["one.$count."] = $row[1];
$postsarray["two.$count."] = $row[2];
$count++;
}
I think you should go for mysqli_fetch_assoc as it will give you the same associative array as like your database.
for e.g.
while($row=mysqli_fetch_assoc($result)){
$postsarray["one.$count."] = $row["column_name_u_want_to_fetch"];
$postsarray["two.$count."] = $row["2_column_name_u_want_to_fetch"];
$count++;
}
You can directly store it in a variable so it will take it as a string.
for e.g.
$stringone="";
$stringtwo="";
while($row=mysqli_fetch_assoc($result)){
$stringone.$count = $row["column_name_u_want_to_fetch"];
$stringtwo.$count = $row["2_column_name_u_want_to_fetch"];
$count++;
}
for($i=0; $i<mysqli_num_rows($result); $i++)
{
echo $stringone.$i;
echo $stringtwo.$i;
}
try it by removing 'ORDER BY RAND() LIMIT 6'
$result = mysql_query("SELECT * FROM Race");
$rows = mysql_num_rows($result);
for ($i = 0 ; $i < $rows ; ++$i)
{
$row = mysql_fetch_row($result);
echo $row[0];
}
above is probably an awkward method but it'll print out all datas stored in first column, which is good but now, I want to store each one of them into an array...
I tried
$array[$i]=$row[0];
and echoed it out, but it just prints"Array"...
I tried
$result = mysql_query("SELECT raceid FROM Race");
while ($row = mysql_fetch_array($result)) {
$array[]=$row[0];
}
...which does the same as code written before, i guess, since it too just print "Array".
Please help! Thank you!!
Do you use simple echo $array;? It's wrong. You can't output array this way.
Use this:
$array = array();
$result = mysql_query("SELECT raceid FROM Race");
while ($row = mysql_fetch_array($result)) {
$array[]=$row[0];
}
foreach($item in $array) {
echo $item."<br>"; // and more format
}
If you want to watch array contents without any format use (e.g. for debugging) print_r or var_dump:
print_r($array);
var_dump($array);
Advice: better to use assoc array.
$array = array();
$result = mysql_query("SELECT raceid FROM Race");
while ($row = mysql_fetch_array($result)) {
$array[]=$row['raceid'];
}
Advanced advice: better to use PDO and object results.
You SQL code will be invulnerable to SQL injections
Code will be more modern and readable.
I have this code below and I am trying to get results from query as array containing array key to be table field name and value to be the result from the field. So far I have this:
$query='select
en_product_name,de_product_name,fr_product_name,ru_product_name
from products where id="'.$pid.'"';
$result=mysql_query($query) or die('Mysql Error:'.mysql_error().'<br /> Query:'.$query);
$num_rows=mysql_num_rows($result);
$row = mysql_fetch_array($result);
$columns = mysql_num_fields($result);
$fields =array();
for($i = 0; $i < $columns; $i++) {
echo $fields = mysql_field_name($result,$i).'<br />';
}
this $fields returnes only the field name.. How can I have the result as:
Array ( [en_product_name] => New en product name, [de_product_name] => New de product name) and etc..
Thank you for any help and suggestions
Try with this
$query='select en_product_name,de_product_name,fr_product_name,ru_product_name
from products where id="'.$pid.'"';
$result=mysql_query($query) or die('Mysql Error:'.mysql_error().'<br /> Query:'.$query);
$num_rows=mysql_num_rows($result);
$row = mysql_fetch_array($result);
$columns = mysql_num_fields($result);
$fields =array();
for($i = 0; $i < $columns; $i++) {
$field = mysql_field_name($result,$i);
$fields[$field] = $row[$field];
}
print_r($fields);
NOTE:
mysql_* has been deprecated. Please use PDO (php.net/manual/en/book.pdo.php) or mysqli_* instead. Otherwise there are security problems.
Consider replacing mysql_fetch_array with mysql_fetch_assoc.
From the documentation for mysql_fetch_assoc:
Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array.
Updated code:
$query='select en_product_name,de_product_name,fr_product_name,ru_product_name
from products where id="'.$pid.'"';
$result=mysql_query($query) or die('Mysql Error:'.mysql_error().'<br /> Query:'.$query);
$num_rows=mysql_num_rows($result);
$row = mysql_fetch_assoc($result);
So you want to access key => value, i am using while statement for clearing the Answer.
You can use these functions and parameters:
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
Or
$row = mysql_fetch_array($result, MYSQL_ASSOC);
It should be solve your problem.
Try to avoid use of mysql_* function as it is deprecated from php 5.5 and will be removed in future.So use mysqli_* function.like this
$con = mysqli_connect("localhost","username","pwd","dbname");
$array = array();
$equery1223 = "SHOW COLUMNS FROM products ";
$eresults1223 = mysqli_query($con,$equery1223) or die(mysqli_error($con));
$i=0;
while($rows3 = mysqli_fetch_array($eresults1223))
{
if($rows3['Field'] ="en_product_name" || $rows3['Field'] ="de_product_name" || $rows3['Field'] ="fr_product_name" || $rows3['Field'] ="ru_product_name" )
{ $array[$i]=$rows3['Field'];$i++; }
}
$array2 = array();$i=0;
$equery1224 = "select * FROM products ";
$eresults1224 = mysqli_query($con,$equery1224) or die(mysqli_error($con));
$i=0;
while($rows4 = mysqli_fetch_array($eresults1224))
{
$array2[$array[$i]]=$rows4[$array[$i]];$i++;
}
I understand this could appear alarming but bear with me.
I need to echo every field in every row in a table.
This is only an example - I have removed the HTML wrapped around it to improve readability.
$a = 1;
while ($a <= $count_rows) {
$query = "SELECT col1, col2 etc.. FROM table WHERE `id`='$id'";
$result = mysqli_query($con, $query);
$i = 1;
while($i <= $count_fields) {
$row = mysqli_fetch_array($result, MYSQL_NUM);
echo "$row[$i]";
$i++;
}
$a++;
$id = $a;
}
This only outputs the first field of every row? Why?
If I echo $row[2] I get nothing!
If I echo $row[2] I get nothing!
because it's actually third item
and there is some strange code interfering with $i variable
Anyway, to get every column from every row ou need a code like this
$query = "SELECT * FROM table";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_row($result)) {
foreach ($row as $index => $value) {
echo "$index => $value, ";
}
echo "<br>\n";
}
I have a problem iterating through an sql query:
$result = mysql_query("SELECT * FROM transactions");
while($row = mysql_fetch_array($result)) {
// this returns 3 rows
foreach ($row as $values)
{
//fputcsv($a_csv, $values;
echo $values;
}
}
The script iterates fine but it appears to be going through each row twice. So what I receive in output is the following:
value1value1value2value2value3value3
I'm not sure why this is - could anyone explain please?
Thankyou
mysql_fetch_array fetches both the named & the numerical keys. Use either mysql_fetch_assoc or mysql_fetch_row.
$result = mysql_query("SELECT * FROM transactions");
//return an associative array
while($row = mysql_fetch_assoc($result)) {
// this returns 3 rows
$values = "{$row["name_of_column1"]}, {$row["name_of_column2"]}, {$row["name_of_column3"]}";
//fputcsv($a_csv, $values;
//print the whole row array
print_r($row);
//echo value in format value1, value2, value3
echo $values;
}
You need to access $row like this $row[0] And $row shouldn't be in a foreach() itself unless it too is some kind of array you need to iterate through.
$result = mysql_query("SELECT * FROM transactions");
while($row = mysql_fetch_row($result))
{
echo $row[0];
echo $row[1];
// ... etc.
}