Jump to content

[SOLVED] Help writing to a text file


affordit

Recommended Posts

I am trying to pull a random record from my database and write it to a text file but can't get it to work can someone help please thanks.

this is what I have so far

<?php
  include "file_func.php";
include("sharons_dbinfo.inc.php");
mysql_connect(mysql,$name,$password);
mysql_select_db($database) or die( "Unable to select database"); 

$range_result = mysql_query( " SELECT MAX(`id`) AS max_id , MIN(`id`) AS min_id FROM `login` ");
$range_row = mysql_fetch_object( $range_result ); 
$random = mt_rand( $range_row->min_id , $range_row->max_id );
$result = mysql_query( " SELECT * FROM `login` WHERE `id` >= $random LIMIT 0,1 ");


  append_file('test.txt', '$result['id']<br>~<br>');
  $data=read_file('test.txt');

   echo "Data in file : <b>$data</b>";

?>

Link to comment
Share on other sites

Looking at your code I see one error, which is the following line:

append_file('test.txt', '$result['id']<br>~<br>');

Variables do not get parsed when inside single quotes you should use double quotes, or better still use concatenation:

# concatenation:
append_file('test.txt', $result['id'] . '<br>~<br>');

# double qoutes:
append_file('test.txt', "{$result['id']}<br>~<br>");

Link to comment
Share on other sites

I could not reply to your last post I tried the code you posted but all it puts in the file is the ~

I didn't thuroughly read your code. mysql_query doesn't return rows, it only returns the result resource. You'll need to use one of the various mysql_fetch_*() functions (* standing for row, array or assoc or object). I recommend mysql_fetch_assoc. So the following few lines:

$result = mysql_query( " SELECT * FROM `login` WHERE `id` >= $random LIMIT 0,1 ");


  append_file('test.txt', '$result['id']<br>~<br>');

Should be:

$result = mysql_query( " SELECT * FROM `login` WHERE `id` >= $random LIMIT 0,1 "); // run query
$row = mysql_fetch_assoc($result); // return results

append_file('test.txt', $row['id'] . '<br>~<br>');

 

If you query returns more than one row you'll need to use a while loop, eg:

$result = mysql_query( " SELECT * FROM `login` WHERE `id` >= $random LIMIT 0,1 "); // run query

// loop through results
while($row = mysql_fetch_assoc($result))
{
    append_file('test.txt', $row['id'] . '<br>~<br>');
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.