I have this code.
<?php
require('connection/conn.php');
mysql_select_db($db_name,$ligação);
//$rsArticle = mysql_query("CALL get_article(1,518)");
$rsArticle = mysql_query("SELECT * FROM tblarticles WHERE ArticleID = 518");
while($rowArticle = mysql_fetch_array($rsArticle)){
echo $rowArticle;
}
?>
And instead of getting the text that exists in the database I just get the word: Array
The line that is commented its for calling a stored procedure. In a desperate measure I made a simple select, in the next line
Can anyone explain me what I'm doing wrong??
Thanks
The reason that you are getting the word Array is because what you are echoing is an Array. Use something like echo $rowArticle['column-name']; to echo the data from a specific column of your query.
You can't echo an array. try print_r() instead
if you want the values individually, do something like this:
while ($row = mysql_fetch_array($rsArticle, MYSQL_NUM)) {
//echo the first column of the record (index 0)
echo $row[0];
}
look in the php.net documentation for more info
mysql_fetch_array returns an array with elements for every field in your database table.
Either print the whole array with print_r() or use echo $rowArticle[COLOUMN_NAME] to echo certain values from your resultset.
mysql_fetch_array() returns you the array, when you use echo is actually return the object type in case of array.
use print_r($rowArticle); instead of echo.
Related
I´m new to PHP and SQL and it´s the first time I post here, hope I do it right =)
I need to find out the numeric index from an certain field in my database. Thing is I´m not done with the database structure yet, so for this specific field I´d like to have it automated so I won't have to change the code every time I add or remove columns.
I also need it to have BOTH index types so I can call the columns by name in part of the code and by index for automated parts of it.
I´ve managed to achieve the result I wanted trough an very ugly code, because all the array_search and array_keys I´ve tried didn´t work out for some reason.
Here is the snippet of my working but ugly code. Do you have an more elegant solution?
Thanks in advance for your answers, and for all I´ve learned so far from reading other peoples questions!
$dadosRes = mysqli_query($con, "SELECT * FROM Alunos WHERE Id=1");
$student = mysqli_fetch_array($dadosRes) or die(mysql_error());
$c = 0; // This block is to find out where the column "cont1" is and assign it to $cont1
$d=array_keys($student);
$cont1=0;
foreach ($student as $a=>$b){
if ($a==='cont1') {
$cont1=$d[$c-1];
}
$c++;
}
for ($i=$cont1; $i<$cont1+12; $i+=3) { // Displays the students contacts, each in up to 3 rows
if ($student[$i]) {
echo "<p><ul><li>".$student[$i]."</li>";
if ($student[$i+1]) echo "<li>".$students[$i+1]."</li>";
if ($student[$i+2]) echo "<li>".$students[$i+2]."</li>";
echo "</ul></p>";
}
}
Finding the numeric index:
$student = mysqli_fetch_array($dadosRes,MYSQLI_ASSOC) or die(mysql_error());
$numericIndex = array_search("cont1",array_keys($student));
Giving MYSQLI_ASSOC will force the query to fetch only the associative keys; otherwise it will give you both numeric and associative array.
PHP function, array_search in this case, requires the third parameter set to TRUE:
<?php
$dadosRes = mysqli_query($con, "SELECT * FROM Alunos WHERE Id=1");
$student = mysqli_fetch_array($dadosRes, MYSQLI_BOTH) or die(mysql_error());
$student_keys = array_keys($student);
$cont1 = $student_keys[array_search("cont1", $student_keys, true) - 1];
for ($i=$cont1; $i<$cont1+12; $i+=3) { // Displays the students contacts, each in up to 3 rows
if ($student[$i]) {
echo "<p><ul><li>".$student[$i]."</li>";
if ($student[$i+1]) echo "<li>".$student[$i+1]."</li>";
if ($student[$i+2]) echo "<li>".$student[$i+2]."</li>";
echo "</ul></p>";
}
}
?>
MYSQLI_BOTH is used in the mysqli_fetch_array function because first the array is searched for the column name, then later the array is looped over with numeric indexes. This results in the $student array having integer 0 as the first element and therefore causing array_search to choke. Adding true as the third parameter causes a === comparison unchoking the function. :) (Explanation: PHP array_search consistently returns first key of array )
I have the following code, which Im using to try loop through an array, however it is printing nothing:
$getdata = "SELECT TimeDate, Percent FROM UserHistory";
echo $getdata;
$dataresult = mysqli_query($con,$getdata);
while($datarray = mysqli_fetch_array($dataresult));
{
echo $datarray['TimeDate'];
}
If I replace the while loop with $datarray = mysqli_fetch_array($dataresult); and then echo $datarray[0] or echo $datarray[1] it prints out values, but only the first set of results.
Use mysqli_fetch_assoc instead of mysqli_fetch_array and everything will be fine.
mysqli_fetch_array does not assign column names as indexes. Thats why numeric indexes work, but named inexes don't.
There is an error in code too... ; finishes while in a line it starts. Remove it.
Remove ';' from while($datarray = mysqli_fetch_array($dataresult));
i have a table with 5 columns like TABLE(a,b,c,d,e).
now i want to select all the columns from TABLE.but the script is not workling once the while loop starts.
my php code is:
include_once $_SERVER['DOCUMENT_ROOT'].'/include/db.inc.php' ;
$sql="select * from TABLE";
$result = mysqli_query($link,$sql );
if (!$result)
{
include_once "wall.html.php";
echo'<tr><td align="center"> OOOOPPPPPSSS!!!SORRY,UNABLE TO DISPLAY LATEST 25 TOPICS</td></tr>';
exit();
}
while ($row = mysqli_fetch_array($result))
{
$as[]=$row['a'];
$bs[]=$row['b'];
$cs[]=$row['c'];
$ds[]=$row['d'];
$es[]=$row['e'];
}
foreach($as as $x)
{
$name=$x;
}....................and so on
i have checked and debuged in many ways.what i noticed the script is not executing after while loop.
you forgot to write $ before variables:
$as[]=$row['a'];
^------------
bs[]=$row['b'];
cs[]=$row['c'];
ds[]=$row['d'];
es[]=$row['e'];
What about using array_push()? Also, use print_r() for $as[] and all the arrays you got and paste the output here so we get a clearer idea of what the issue exactly is.
Try this and tell what print_r() function is printing so that we can identify the problem.
while ($row = mysqli_fetch_array($result))
{
$as[]=$row['a'];
$bs[]=$row['b'];
$cs[]=$row['c'];
$ds[]=$row['d'];
$es[]=$row['e'];
}
print_r($as);
foreach($as as $x)
{
$name=$x;
}
First remove the brackets from $as[], the result of $row['a'] will not be an array.
And that's the reason the foreach is not working, you're not getting an array back.
If the resulting $as is empty, but there is something in your database, make sure the rownames are valid.
Use $row[0] instead of $row['A'].
Debug your code using print_r($row) to the see the contents of the results.
I'm a bit stuck on this one. In my PHP code, I query for some records which are returned. I loop through the rows, but for each row I want to retrieve a column value using its name as an index.
while ($row=mysql_fetch_row($result))
{
echo $row[7];
}
This prints out what I want, but I wanted to do something like:
echo $row["description"];
Where description is the name of the column whose value I want to print? Can this be done?
Thank you very much.
You could also use this:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
Then you can switch back to numeric with MYSQL_NUM or both with MYSQL_BOTH
You need to use mysql_fetch_assoc to get the associative array.
You want to use the mysql_fetch_assoc function instead.
I have a typical database query:
$query = mysql_query('SELECT titulo,referencia FROM cursos WHERE tipo=1 AND estado=1');
and I can convert it in an array and print the data:
while ($results=mysql_fetch_array($query)): ?>
echo $results['referencia'];
// and so...
endwhile;
but in some cases I need to print the same data in another part of the web page, but the $results array seems to be empty (I use var_dump($results) and I get bool(false)). I plan to use what I learned reading Create PHP array from MySQL column, but not supposed to mysql_fetch_array() creates an array? So, what happen?
Tae, the reason that your $result is false at the end of the while loop is that mysql_fetch_array returns false when it reaches the end of the query set. (See the PHP Docs on the subject) When you reach the end of the query set $results is set to false and the while loop is exited. If you want to save the arrays (database row results) for later, then do as Chacha102 suggests and store each row as it is pulled from the database.
$data = array();
while($results = mysql_fetch_array($query)) {
$data[] = $results;
}
foreach ($data as $result_row) {
echo $result_row['referencia'];
... etc.
}
Try this
while($results = mysql_fetch_array($query))
{
$data[] = $results;
}
Now, all of your results are in $data, and you can do whatever you want from there.
As Anthony said, you might want to make sure that data is actually being retrieved from the query. Check if any results are being returned by echo mysql_num_rows($query). That should give you the number of rows you got