My query stop working suddently. I know it is dutch but can you detect any errors?
There are no MySQL errors. He just won't write $content to the screen.
I tried numerous things. he just stop working
<?php
$activiteiten= "SELECT
activiteiten.ActiviteitID,
activiteiten.Naam,
activiteiten.plaatsen,
agenda.TijdBegin,
agenda.TijdEind,
agenda.AgendaID,
reserveren.ReserveringTijd
FROM
activiteiten,
agenda,
reserveren
WHERE
agenda.ActiviteitID = activiteiten.ActiviteitID
AND
agenda.AgendaID = reserveren.AgendaID
";
$result = mysql_query($activiteiten) or die(mysql_error());
$content .= '<tr>';
$content .= '<td>'.$record['Naam'].'</td>';
$content .= '<td>'.$record['ReserveringTijd'].'</td>';
$content .='<td>Beschikbare plaatsen:'.$record['plaatsen'].'</td>';
$agendaID= $record['AgendaID'];
$agendaData = agendaData($agendaID);
$content.= '<td>aantal reserveringen:'.$agendaData["reserveringen"].'</td>';
$content.= '</tr>';
}
You do a mysql_query, but you never fill the $record variable. How do you expect it to have content?
Try adding:
$record = mysql_fetch_array( $result );
after the mysql_query.
You need to understand these are two different things:
mysql_query will execute a query on the server
mysql_fetch_array (and friends) will get the results in the desired format.
If you only do 1, you execute the query. That works fine, but if you are interested in the results, you need to fetch them. Sometimes you don't need to (for example, when the query is a not a select).
There are no MySQL errors. He just won't write $content to the screen.
What about adding:
echo $content;
If you don't print it, then you won't see anything :)
Apart from that, aren't you mixing $result and $record?
Your code is missing mysql_fetch_assoc call after mysql_query, it should be something like this:
$result = mysql_query($activiteiten) or die(mysql_error());
while(false != ($record = mysql_fetch_assoc($result))
{
$content .= '<tr>';
$content .= '<td>'.$record['Naam'].'</td>';
$content .= '<td>'.$record['ReserveringTijd'].'</td>';
$content .='<td>Beschikbare plaatsen:'.$record['plaatsen'].'</td>';
$agendaID= $record['AgendaID'];
$agendaData = agendaData($agendaID);
$content.= '<td>aantal reserveringen:'.$agendaData["reserveringen"].'</td>';
$content.= '</tr>';
}
Stopped working and not printing out any error rises the suspicion that the data in the database has somehow changed and the query simply does not get any rows.
Execute your query in a database environment like phpMyAdmin and check if any rows are returned.
Otherwise check the other answers for mysql-php-related errors.
$result = mysql_query($activiteiten) or die(mysql_error());
while ($record = mysql_fetch_array($result, MYSQL_ASSOC))
{
// Your $record now contains the data you want
}
You have the query result stored in $result but you do not then go on to do anything with $result - you need to do something like:
$record = mysql_fetch_assoc($qry_result);
to retrieve the record results from $result. If you expect more than one result you'll need to use a while loop to loop through all the results
Change:
$content .= '<tr>';
To:
$content = '<tr>';
And remember to echo the $content on the page.
Make sure you have error reporting on so you see immediately if the problem lies in uninitialized PHP variable or somewhere else.
Related
Hey usually I use mysqli_fetch_array to display the content of my database in my CMS, but recently someone told me I should use mysqli_fetch_assoc and push the results into an array so that database only runs once instead of running the database for each record.
But I'm not really sure how to display my fields without showing them all, usually I would echo $data['field_name'], but what I've noticed with mysqli_fetch_assoc is I can't just echo $value['field_name'], all I can do is echo $value and it displays all the results.
This is what I've done, hope that makes sense. Thanks in advance for any help!
PHP
$sql = "SELECT * FROM app_categories";
if($result = query($sql)){
$list = array();
while($data = mysqli_fetch_assoc($result)){
array_push($list, $data);
}
foreach($list as $i=>$row){
foreach($row as $column=>$value){
echo $value;
}
}
}
why you don't just
echo $row['field_name'];
I've read through some other posts that were similar but I can't seem to get a good implementation of them. I'm calling a php script from another program that needs the results returned in one variable, space or comma separated. The php connects to a db (no problem there) and runs a query that will return 2 to 6 or so matching rows. I need those results together in one variable but can't seem to get it.
Here's where I'm stuck.
$t = "SELECT user FROM call_times WHERE client='$clientid' AND start <= $date AND end >= $date";
$result = mysql_query($t) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$temp = $row['user'];
}
echo $temp;
The query runs fine, but you can see from the code what I'm trying (and failing) to do in the lower part. I need $temp to hold a list of results (ex: 5567889 57479992 4335780 (each of which is a different entry in user column)).
Thanks so much!
$array = array();
while ($row = mysql_fetch_array($result)) {
$array[] = $row['user'];
}
$string = implode(" ", $array);
Now $string has the space-separated values of the user column.
Good luck!
Try group_concat() and you won't need PHP to manipulate the results.
Is there a clean way (no hardcoding) in which I can dump all the contents of a database directly to HTML using PHP?
I don't want to run queries for every table and step through the results, outputting them. I need this for testing purposes, so the tables aren't that big.
Any hints?
I want this done directly in my php file, where the rest of the test takes place, so that I may compare with the sample. I need to do this automatically, so can't really use tools like PHPMyAdmin.
Something like this ought to work:
<?php
function dump_mysql_results($mysql_table){
$query = mysql_query("SELECT * FROM `$table` WHERE 1",[your connection]) or die(mysql_error());
if (!mysql_num_rows($query)){die("No rows in $table");}
while($r=mysql_fetch_array($query)){
if (!isset($html)){
$keys = array_keys($r);
$html = "<tr>";
foreach($keys as $key){
$html .= "<th>$key</th>";
}
$html .= "</tr>";
}
$html .= "<tr>";
foreach($r as $value){
$html .= "<td>$value</td>";
}
$html .= "</tr>";
}
return "<table>".$html."</table>";
}
//ADDING a loop to dump the whole db:
$tables = mysql_list_tables ( 'database name',$link_identifier) or die(mysql_error());
while($r=mysql_fetch_array($tables)){
echo dump_mysql_results($r[0]);
}
?>
Use SHOW TABLES to get the list of tables, then iterate through them normally to select all the rows and display in HTML.
See http://dev.mysql.com/doc/refman/5.5/en/show-tables.html
What about mysqldump ?
<?php
exec('mysqldump --user=DBuser --password=DBpass --host=localhost --compact --xml DBname > file.xml');
?>
Then use simpleXML to convert xml to HTML
Small problem regarding scope in PHP, I can't seem to call the variable $report outside of the while loop. I have tried various things, including return. This doesn't work, the only two functions that work here are if I echo the variable $report inside the loop, or if I print it. Which I do not want to do, although it solves the problem, but I don't want random gibberish on the user's screen.
I have been looking around for the last 15 or so minutes, and I haven't seen any problems quite like this one on here.
Any help would be appreciated.
<?
require "functions2.php";
require "members.php";
$query = "SELECT MAX(DOCid) as prevDOCid from reports";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$prevDOCid = $row[prevDOCid];
$thisDOCid = $prevDOCid+1;
$report = "a"."b".$thisDOCid;
}
echo $report;
?>
You could try to define the variable before the loop, e.g.
$report = "";
while ($row = mysql_fetch_array($result)) {
$report .= "a"."b".$row["prevDOCid"]+1;
}
echo $report;
I hope this helps you!
Edit Use .= not +=
I have this code
while($row = mysql_fetch_row($result))
{
echo '<tr>';
$pk = $row[0]['ARTICLE_NO'];
foreach($row as $key => $value)
{
echo '<td>' . $value . '</td>';
}
which gets pk. pk is then passed on to the axjax part with this:
function GetAuctionData(pk)
{
.....
var url="get_auction.php?"
url=url+"cmd=GetAuctionData&pk="+pk;
And finally used in a separate php file with:
$pk = $_GET["pk"];
$sql="SELECT * FROM Auctions WHERE ARTICLE_NO ='$pk'";
the second php file works fine when using it by itself and passing parameters. Likewise there are no errors anywhere. The problem seems to be with passing or generating $pk, as the links in the output file result in $pk being incremednted by 2, eg 4, 6, 8 etc
I can not understand why this is happening.
mysql_fetch_row link does not have subarrays. It will return the first field as 0, next as 1, etc.
Try with
$pk = $row[0];
This can easily be used with your foreach
while($row = mysql_fetch_assoc($result))
$pk = $row['ARTICLE_NO'];
or this gives you both associative and numbered array.
while($row = mysql_fetch_array($result, MYSQL_BOTH))
$pk = $row['ARTICLE_NO'];
EDIT:
Based on
$result = mysql_query("SELECT SELLER_ID, ACCESSSTARTS, ARTICLE_NAME FROM {$table}");
You have to include the row you want a value from. ;)
$result = mysql_query("ARTICLE_NO, SELECT SELLER_ID, ACCESSSTARTS, ARTICLE_NAME FROM {$table}");
BTW: Im pretty sure this nested loop will not produce what you want. You'll get 3 links to each article_no. The first with seller_id as text, the second text is accessstarts, and the last link with the same href will have the text article_name.
Maybe something like this?
while($row = mysql_fetch_assoc($result))
{
$pk = $row['ARTICLE_NO'];
echo '<td>' . $row['ARTICLE_NAME'] . '</td>';
}
As troelskn says, it looks like you are a bit confused as to what mysql_fetch_row is returning.
mysql_fetch_row will return $article[0], $article[1] etc
there are also:
mysql_fetch_assoc // return $article['ARTICLE_NO'], $article['otherfield'] etc
mysql_fetch_array // returns an array that is effectively the above two array_merge'd
mysql_fetch_object // returns a stdclass object, as if mysql_fetch_assoc had been passed to get_object_vars()
you need to refactor a bit, in light of this ....
I don't think this does what you expect:
$pk = $row[0]['ARTICLE_NO'];
Try with:
$pk = $row['ARTICLE_NO'];
The other responses here should answer your problem sufficiently, but I just wanted to add an extra debugging tip to could help you should you run into a similar problem in the future.
You can do some very basic debugging by merely printing to the screen information about the variables you're working with. (A PHP IDE will often have more powerful debugging features, but for something like this problem, this method will be fine)
Your original code:
while($row = mysql_fetch_row($result))
{
echo '<tr>';
$pk = $row[0]['ARTICLE_NO'];
foreach($row as $key => $value)
{
echo '<td>' . $value . '</td>';
}
}
You notice that the GetAuctionData function isn't getting its variable properly. Add this line:
$pk = $row[0]['ARTICLE_NO'];
var_dump($pk);
If you look at the output when you run the file, you'd probably see it saying that $pk is null. Hmmm, that didn't work. Let's change it a bit:
$pk = $row[0]['ARTICLE_NO'];
var_dump($row);
You'd now see something like this:
array(2) {
[0]=> string(2) "12"
[1]=> string(7) "myValue"
}
Aha! Now that you can see exactly what you're working with, you'd see that the code you wanted to use in the first place was:
$pk = $row[0];
var_dump() is your friend. If you're working with arrays, print_r() gives you similar information, but formatted nicely.
Good luck.