Jump to content

Mr Chris

Members
  • Posts

    336
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

Mr Chris's Achievements

Advanced Member

Advanced Member (4/5)

0

Reputation

  1. Hello, Hopefully an easy one. Say I want to search two tables for the same thing, where each of the tables has the column 'name' in it: How would I correctly do it, I tried: SELECT * FROM table1, table2 WHERE table1.name = 'John' OR table2.name = 'John' But this brings back results for table1 and table2 in a single row, even though the result returned should just be from table2 as that's the only occurrance of John? Thank you
  2. Hello All, I'm trying to work out how to use array_rand() to output a random value in an array. So i've tested it in a single array, and it works fine: $input1 = array("Link1", "Link2", "Link3", "Link4", "Link5"); $rand_keys = array_rand($input1, 2); echo $input1[$rand_keys[0]] . "\n"; However, I want 'Link1', 'Link2' etc to have their own SPECIFIC URL values, so i've tried declaring another array: $input1 = array("Link1", "Link2", "Link3", "Link4", "Link5"); $input2 = array('page.php','file.php','ends.php','smile.php'); //So 'Link1 = page.php, Link2 = file.php etc... $rand_keys = array_rand($input1, 2); echo 'The value for input 1 is: '.$input1[$rand_keys[0]] . 'and the url is '.??????????????.' "\n"; But can't work out how i'd grab this value - anyone kindly help?
  3. Yep sure. Simply forgot to have session_start() on my opening page - my bad. One of those silly errors.
  4. Thanks, but yes I have error reporting on (did not notice the double session, so thanks for that!) but even if I comment out the redirect and replace it with a "I am not aware of any session" it prints that, even though $_SESSION['password'] exists? <?php ini_set('display_errors', 1); ini_set('log_errors', 1); ini_set('error_log', dirname(__FILE__) . '/error_log.txt'); error_reporting(E_ALL); function checklogin() { session_start(); if(!isset($_SESSION['password'])) { echo "I aint aware of any session!"; } } checkLogin(); Print_r ($_SESSION); echo "Erm, I have reached page.php and the password is ".$_SESSION['password']; ?>
  5. Afternoon, Not sure if i've been staring at the screen for too long or not, but have a log in script which saves the password as a session variable and I can pass that variable across my page like so: <?php session_start(); //Print_r ($_SESSION); echo "Erm, I have reached page.php and the password is ".$_SESSION['password']; ?> However, I want to make this secure, so I added a function to it which when you log in says if $_SESSION['password'] does not exist then send me away, like so: <?php session_start(); function checklogin() { session_start(); if(!isset($_SESSION['password'])) { header("location: /index/"); exit; } } checkLogin(); Print_r ($_SESSION); echo "Erm, I have reached page.php and the password is ".$_SESSION['password']; ?> However, using the function and a valid log-in it (as shown above) it chucks me out of the page even though $_SESSION['password'] exists? Am I doing something really stupid here? Thank you
  6. Hello, Wondering if anyone can help me with a PDO query for outputting a html table. Take this query: function outputSchedule ($dbc) { $QUERY = $dbc->query("SELECT * from tbl"); $QUERY->setFetchMode(PDO::FETCH_ASSOC); $output =' <table> <thead> <tr> <th>Name</th> <th>Weight</th> </tr> </thead> <tbody>'; while($ROW = $QUERY->fetch()) { $output .=' <tr> <td style="width:160px;'.$bg.'">'.$ROW['name'].'</span></td> <td style="width:180px;'.$bg.'">'.$ROW['weight'].'</td> </tr> '; } $output .=' </tbody> </table>'; return $output; } You will see I run through a loop to catch all my records in my database, however my problem is with my table headings and the closing of my table. Say there are no records in the database then the start of the table will automatically print (where the <th>'s are) as well as the end of the table. So I want to basically want to use mysql_num_rows to achieve this. Now if it was in a normal query i'd be fine ($NUM = mysql_num_rows then if $NUM > 0, outputting a message if there are no rows in the database). However, i'm not at all sure how to do it using PDO as what I used threw up errors, can anyone help?
  7. Hello all, This is driving me potty. I'm trying to run a query in PDO that a pass to a function. function myFunction ($dbc,$the_date) { echo $the_date; $QUERY = $dbc->query("SELECT * from table WHERE the_date = ':the_date' ORDER BY id DESC"); $QUERY->bindParam(':the_date', $the_date); $QUERY->setFetchMode(PDO::FETCH_ASSOC); //etc... } Now as you can see I echo $the_date and it shows, but when the query runs the named placeholder does not seem to work. When I take the WHERE clause out it works fine, so I know it's not an issue there. Any thoughts? Thanks
  8. Thanks, but I was thinking maybe in some kind of switch statement. Is that possible within a function?
  9. Hello All, I'm trying to put a lot of what I do into function calls now to make my code a bit tidier and consise. Take this simple if statement: if ($_GET['thedate'] =='') { $var = date('l, jS F, Y',time()); } else { $var = date('l, jS F, Y', strtotime($_GET['thedate'])); } How could I put this into a function and in some kind of switch statement? Thanks
  10. Hello All, Trying to write a log-in script using PDO. But have one question. With the below when I query the database and even run a print_r on $RES nothing is outputted? Any ideas? Thanks if (isset($_POST['Submit'])) { $email = $_POST['email']; $password = $_POST['password']; $QUERY = $dbc->prepare("SELECT email, password FROM tbl WHERE email = :email"); $QUERY->bindParam(':email', $email); $QUERY->execute(); $RES = $QUERY->fetchColumn(); echo "Email is:".$RES['email']; echo "PW is: ".$RES['password']; print_r($RES); }
  11. Say I have an array like so: $var = array ("One","Two","Three","Four","Five","Six","Seven","Eight"); print_r($var); foreach ($var as $newname) { //Do something } and the print_r is recorded as Array ( [0] => One [1] => Two [2] => Three [3] => Four [4] => Five [5] => Six [6] => Seven [7] => Eight ) How in that foreach loop can I run an if statement which says if the element is greater than [5], then do something? Thanks
×
×
  • 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.